1.Cookie session
历史记录 分页
include指令
1.include指令属与jsp的三大指令之一
2.三大指令:page/include/taglib
3.include指令作用:引入页面
语法:<%@ include file = "指定的页面名称"%>
2.评论管理
<%
//设置编码
request.setCharacterEncoding("utf-8");
//获取form表单提交的评论字段
//编号
int cid = 0;
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//建立连接
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "123");
//调用方法返回执行对象
String sql = "select nvl(max(cid),0) from tb_t281_news_comment";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
cid = rs.getInt(1) + 1;
}
//用户编号
int cuserid = 0;
String username = request.getParameter("cauthor");
//交互 根据username获取该用户的编号
sql = "select * from tb_t281_news_users where username = ?";
ps = conn.prepareStatement(sql);
ps.setString(1,username);
rs = ps.executeQuery();
if(rs.next()){
cuserid = rs.getInt(1);
}
//新闻编号
int nid = 0;
String id = request.getParameter("nid");
if(null!=id){
nid = Integer.valueOf(id);
}
//ip地址
String cip = request.getParameter("cip");
//内容
String ccontent = request.getParameter("ccontent");
//时间
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String cdate = sdf.format(date);
//交互---插入评论
sql = "insert into tb_t281_news_comment values(?,?,?,?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'))";
ps = conn.prepareStatement(sql);
ps.setInt(1,cid);
ps.setInt(2,cuserid);
ps.setInt(3,nid);
ps.setString(4,cip);
ps.setString(5,ccontent);
ps.setString(6,cdate);
int n = ps.executeUpdate();
if(n>0){
out.println("<script>alert('评论成功');location.href='news_read.jsp?username="+username+"&nid="+nid+"'</script>");
}else{
out.println("<script>alert('评论失败');location.href='news_read.jsp?username="+username+"&nid="+nid+"'</script>");
}
%>
3.首页数据绑定
<%
//默认查看所有。
sql = "select * from tb_t281_news";
//获取点击指定主题的编号 超链接跳转
String tid = request.getParameter("ntid");
int ntid = 0;
if(null!=tid){
ntid = Integer.valueOf(tid);
}
//tid获取的结果为null 说明没有点击主题名称 查看所有
//tid获取的结果不为null 说明点击了主题名称 根据主题编号查找对应的新闻
if(tid==null){//查看所有
System.out.println("没有点击主题,查看所有新闻");
}else{//不为空
sql+=" where ntid = "+ntid;
}
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
out.println("<li><a href='news_read.jsp?nid="+rs.getInt(1)+"&username="+username+"'> "+rs.getString(3)+"</a><span> "+rs.getString(8)+" </span></li>");
}
%>