一、后台模糊查询
① 运行admin.jsp之后,默认显示所有新闻。
② 在输入框中输入关键词点击搜索 跳转到本页面
③ 通过request内置对象获取当前模糊查询表单提交的关键词
④ 将指定编码的字符串进行解码,通过getBytes("编码"),构造函数new String()编码
<!-- 实现admin.jsp页面中模糊查询的搜索功能(根据标题进行模糊查询) -->
<div style = "width:100%;height:40px;text-align: center;line-height:40px; ">
<form action = "admin.jsp" method = "post">
<label>新闻标题</label>
<!-- autocomplete 关闭自动提示 -->
<input type = "text" name = "strName" autocomplete="off"/>
<input type = "submit" value = "搜索"/>
</form>
</div>
<%
//编码设置
request.setCharacterEncoding("utf-8");
String strName = request.getParameter("strName");
//搜索的关键词为中文 获取时会出现乱码 通过String类进行编码和解码
System.out.println("模糊查询的关键词为:[解决前] "+strName);
//sql语句
String sql = "select * from tb_news";
//第一次运行admin.jsp strName为null 不能调用任何方法
if(null!=strName){
strName = new String(strName.getBytes("ISO-8859-1"),"utf-8");
sql+=" where ntitle like '%"+strName+"%'";
}
%>
二、include指令
include用来包含jsp文件和html文件
① 先封装脚部代码
② 通过jsp中的include指令进行引入第三方页面
<%@ include file="foot.jsp" %><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>后台-尾部封装页面</title>
</head>
<body>
<div id="site_link"> <a href="#">关于我们</a><span>|</span> <a href="#">Aboue Us</a><span>|</span> <a href="#">联系我们</a><span>|</span>
<a href="#">广告服务</a><span>|</span> <a href="#">供稿服务</a><span>|</span> <a href="#">法律声明</a><span>|</span> <a href="#">招聘信息</a><span>|</span>
<a href="#">网站地图</a><span>|</span> <a href="#">留言反馈</a> </div>
<div id="footer">
<p class="">24小时客户服务热线:010-68988888 <a href="#">常见问题解答</a>
新闻热线:010-627488888<br />
文明办网文明上网举报电话:010-627488888 举报邮箱:<a href="#">jubao@jb-aptech.com.cn</a></p>
<p class="copyright">Copyright © 1999-2009 News China gov, All Right Reserver<br />
新闻中国 版权所有</p>
</div>
</body>
</html>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>新闻发布系统-后台主页</title>
<link rel="stylesheet" type="text/css" href="css/admin.css" />
</head>
<body>
<div id="header">
<div id="welcom">欢迎使用新闻管理系统!</div>
<ul class="classlist">
<%
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//建立连接
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott","123");
//将sql语句传入方法返回执行对象
PreparedStatement ps = conn.prepareStatement(sql);
//根据执行对象调用方法返回结果集对象
ResultSet rs = ps.executeQuery();
//遍历结果集
while(rs.next()){
out.println("<li> <a href='admin_newsDetail.jsp?nid="+rs.getInt(1)+"'>"+rs.getString(3)+"</a> <span> 作者:"+rs.getString(4)+" <a href='admin_editNews.jsp?nid="+rs.getInt(1)+"'>修改</a> <a href='javascript:void(0)' onclick='clickdel(\""+rs.getInt(1)+"\")'>删除</a></span> </li>");
}
%>
<li class='space'></li>
<p align="right"> 当前页数:[1/3] <a href="#">下一页</a> <a href="#">末页</a> </p>
</ul>
</div>
</div>
<!-- 通过jsp中的include指令进行引入第三方页面 -->
<%@ include file="foot.jsp" %>
</body>
</html>
三、 首页数据绑定
① 与数据库进行交互显示所有主题(进行数据绑定)
② 为通过点击相应主题出现相应文章
③ 显示新闻
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>新闻首页</title>
<link rel="stylesheet" type="text/css" href="admin/css/main.css" />
</head>
<body>
<div id="container">
<div class="sidebar">
<h1> <img src="admin/images/title_1.gif" alt="国内新闻" /> </h1>
<div class="side_list">
</div>
<h1> <img src="admin/images/title_2.gif" alt="国际新闻" /> </h1>
<div class="side_list">
</div>
<h1> <img src="admin/images/title_3.gif" alt="娱乐新闻" /> </h1>
</div>
<div class="main">
<div class="class_type"> <img src="admin/images/class_type.gif" alt="新闻中心" /> </div>
<div class="content">
<!-- 3.index.jsp用户首页的新闻分类显示(数据绑定) -->
<ul class="class_date">
<li id='class_month'>
<a href = "index.jsp"><b> 全部 </b></a>
<%
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//建立连接
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
Connection conn = DriverManager.getConnection(url, "scott", "123");
//sql
String sql = "select * from tb_news_theme";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
//sql select count(*) from tb_news_theme
int count = 1;
while(rs.next()){
count++;
out.println("<a href='index.jsp?username="+username+"&ntid="+rs.getInt(1)+"'><b> "+rs.getString(2)+" </b></a> ");
//11 就是总记录数
if(count%11==0){
out.println("<br/>");
}
}
%>
</li>
</ul>
<!-- 当进入index.jsp页面后,手动点击对应的主题名称 跳转到本页面 获取当前点击的主题名称 -->
<%
username = request.getParameter("username");
String tid = request.getParameter("ntid");
if(null!=tid){//说明点击了主题分类
//根据点击的主题分类进行查询 where 条件
sql = "select * from tb_news where ntid = "+Integer.valueOf(tid);
}else{//第一次进来,没有点击主题分类
sql = "select * from tb_news";//查询所有新闻
}
%>
<!-- 新闻显示 start-->
<ul class="classlist">
<%
//sql
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
out.println("<li><a href='newRead.jsp?username="+username+"&nid="+rs.getInt(1)+"'> "+rs.getString(3)+" </a><span> "+rs.getString(8)+" </span></li>");
}
%>
</ul>
</div>
<div class="picnews">
</div>
</div>
</div>
<div id="friend">
<h1 class="friend_t"> <img src="admin/images/friend_ico.gif" alt="合作伙伴" /> </h1>
<div class="friend_list">
</div>
</div>
<!-- 通过jsp中的include指令进行引入第三方页面 -->
<%@ include file="foot.jsp" %>
</html>
四、前端登录
① 登录布局
② 登录的js验证
③ 数据库交互
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>新闻发布前台-用户登录</title>
<link type="text/css" rel="stylesheet" href="css/style.css" />
<!-- 修改成本地 -->
<script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var height=$(document).height();
$('body').css('height',height);
var mart=height-500;
mart = mart/2;
$('.loginBox').css('marginTop',mart);
})
</script>
</head>
<body>
<div class="loginBox">
<h1>欢迎登陆</h1>
<form action = "doLogin.jsp" method = "post">
<div class="item">
<div class="icon"><img src="images/icon1.png" /></div>
<div class="txt"><input name="username" type="text" placeholder="请输入您的用户名"/></div>
</div>
<div class="item">
<div class="icon"><img src="images/icon2.png" /></div>
<div class="txt"><input name="password" type="password" placeholder="请输入您的密码"/></div>
</div>
<div class="item_2">
<input name="" type="checkbox" value="" />
<span>记住密码</span>
<a href="psd.html">忘记密码?</a>
</div>
<div class="item_3">
<input name="" type="submit" value="安全登录" class="btn"/>
</div>
</form>
<div class="item_4">
还没有账号?<a href="register.html">立即注册</a>
</div>
</div>
</body>
</html>
中转站(数据库交互)
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//前端登录处理
//1.设置编码
request.setCharacterEncoding("utf-8");
//2.获取信息
String username = request.getParameter("username");
String password = request.getParameter("password");
//3.数据库交互
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//建立连接
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
Connection conn = DriverManager.getConnection(url, "scott", "123");
//sql
String sql = "select * from tb_news_users where username = ? and password = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,password);
ResultSet rs = ps.executeQuery();
//4.判断页面跳转
if(rs.next()){
//跳转到前端的首页
request.getRequestDispatcher("index.jsp").forward(request, response);
}else{
out.println("<script>alert('账户或密码错误');location.href='login.jsp'</script>");
}
%>
五、评论管理
(1)显示评论以及增加评论
① 先与数据库交互显示文章
② 再与数据量交互显示评论
③ 再布局增加评论
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 获取 当前传递的参数用户以及新闻编号 -->
<%
//设置编码
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");//用户名
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="admin/css/read.css" />
<script type="text/javascript">
function check() {
var cauthor = document.getElementById("cauthor");
var content = document.getElementById("ccontent");
if (cauthor.value == "") {
alert("用户名不能为空!!");
return false;
} else if (content.value == "") {
alert("评论内容不能为空!!");
return false;
}
return true;
}
</script>
</head>
<body>
<div id="header">
</div>
<div id="container">
<div class="sidebar">
<h1> <img src="admin/images/title_1.gif" alt="国内新闻" /> </h1>
<div class="side_list">
</div>
<h1> <img src="admin/images/title_2.gif" alt="国际新闻" /> </h1>
<div class="side_list">
</div>
<h1> <img src="admin/images/title_3.gif" alt="娱乐新闻" /> </h1>
<div class="side_list">
</div>
</div>
<div class="main">
<div class="class_type"> <img src="admin/images/class_type.gif" alt="新闻中心" /> </div>
<div class="content">
<!-- 该区域实现新闻详情显示 start -->
<%
//定义所谓变量(新闻标题,发布时间,点击量,新闻内容等等)
String ntitle = "";//标题
String ndate = "";//发布时间
String nauthor = "";//作者
String ncontent = "";//内容
int ncount = 0;//点击量
//2.获取id
String id = request.getParameter("nid");
//转换
int nid = 0;
if(null!=id){
nid = Integer.valueOf(id);
}
//3.获取数据
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//建立连接
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott","123");
//sql语句
String sql = "select * from tb_news where nid = ?";
//将sql语句传入方法返回执行对象
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, nid);
//返回结果集对象
ResultSet rs = ps.executeQuery();
//if改造
if(rs.next()){
ntitle = rs.getString(3);
ndate = rs.getString(8);
nauthor = rs.getString(4);
ncontent = rs.getString(6);
ncount = rs.getInt(9);
}
%>
<ul class="classlist">
<table width="80%" align="center">
<tr width="100%">
<td colspan="2" align="center"><%=ntitle %>
点击量 <%=ncount %></td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td align="center"><%=ndate %></td>
<td align="left"><%=nauthor %> </td>
</tr>
<tr>
<td colspan="2" align="center"></td>
</tr>
<tr>
<td colspan="2"> <%=ncontent %> </td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
</table>
</ul>
<ul class="classlist">
<table width="80%" align="center">
<!-- 从index.jsp页面中跳转到详情页面,如果有评论 此处要显示出来,如果没有
评论 显示 "暂无评论"
-->
<%
//定义一个变量存储指定新闻的评论数
int commentCount = 0;
//1.求指定新闻的评论数判断
sql ="select count(*) from tb_comment where cnid = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, nid);
rs = ps.executeQuery();
if(rs.next()){
commentCount = rs.getInt(1);
}
if(commentCount!=0){
//遍历所有的评论
sql ="select * from tb_comment where cnid = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, nid);
rs = ps.executeQuery();
while(rs.next()){
sql = "select * from tb_news_users where userid = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, rs.getInt(2));
ResultSet rs2 = ps.executeQuery();
String uname = "";
if(rs2.next()){
uname = rs2.getString(2);
}
out.println("<tr>");
out.println("<td>");
out.println("用户名:"+uname+"<br/>");
out.println("评论内容:"+rs.getString(5));
out.println(" <a href = 'doDelComment.jsp?username="+username+"&cid="+rs.getInt(1)+"'>删除</a><br/>");
out.println("评论时间:"+rs.getString(6)+"<br/>");
out.println("</td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td>");
out.println(" ");
out.println("</td>");
out.println("</tr>");
}
}else{
out.println("<td colspan='6'> 暂无评论! </td>");
}
%>
<tr>
<td colspan="6">
<hr />
</td>
</tr>
</table>
</ul>
<ul class="classlist">
<form action="doComment.jsp" method="post" onsubmit="return check()">
<!-- 通过隐藏域标签传递新闻编号 -->
<input type = "hidden" name = "nid" value = "<%=nid %>"/>
<table width="80%" align="center">
<tr>
<td> 评 论 </td>
</tr>
<tr>
<td> 用户名: </td>
<td><input id="cauthor" name="username" value="<%=username %>" />
IP:
<input name="cip" value="127.0.0.1" readonly="readonly" />
</td>
</tr>
<tr>
<td colspan="2"><textarea name="ccontent" cols="70" rows="10"></textarea>
</td>
</tr>
<td><input name="submit" value="发 表" type="submit" />
</td>
</table>
</form>
</ul>
</div>
</div>
</div>
<div id="friend">
<h1 class="friend_t"> <img src="admin/images/friend_ico.gif" alt="合作伙伴" /> </h1>
<div class="friend_list">
</div>
</div>
<
</body>
</html>
(2)增加评论的中转站
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//1.设置编码
request.setCharacterEncoding("utf-8");
//获取数据
//评论编号
int cid = 0;//定义一个变量保存最终的id
//链接数据库查询到最大的id 最后+1
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//建立连接
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
Connection conn = DriverManager.getConnection(url, "scott", "123");
String sql = "select nvl(max(cid),0) from tb_comment";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if(rs.next()){
cid = rs.getInt(1)+1;
}
//out.println("cid = "+cid);
//用户编号
int cuserid = 0;
String username = request.getParameter("username");
//根据用户名查找到该用户的编号
sql = "select * from tb_news_users where username = ?";
ps = conn.prepareStatement(sql);
ps.setString(1,username);
rs = ps.executeQuery();
if(rs.next()){
cuserid = rs.getInt(1);
}
//新闻编号
int nid = Integer.valueOf(request.getParameter("nid"));
//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_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='newRead.jsp?username="+username+"&nid="+nid+"'</script>");
}else{
out.println("<script>alert('评论失败');location.href='newRead.jsp?username="+username+"&nid="+nid+"'</script>");
}
%>
(3)删除评论
每个用户只能删除自己发布的评论
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//删除评论
//设置编码
request.setCharacterEncoding("utf-8");
//获取用户名
String username = request.getParameter("username");
//加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//建立连接
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
Connection conn = DriverManager.getConnection(url, "scott", "123");
int cuserid = 0;
String sql = "select * from tb_news_users where username = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
if(rs.next()){
cuserid = rs.getInt(1);
}
//获取cid
String id = request.getParameter("cid");
int cid = 0;
if(null!=id){
cid = Integer.valueOf(id);
}
int cid_cuserid = 0;
//根据cid进行查找 cuserid
int nid = 0;//保存新闻编号
//sql
sql = "select * from tb_comment where cid = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, cid);
rs = ps.executeQuery();
if(rs.next()){
cid_cuserid = rs.getInt(2);
nid = rs.getInt(3);
}
if(cuserid!=cid_cuserid){//不是当前登录用户所评论的内容
//out.println("不是当前登录用户的评论内容");
out.println("<script>alert('此评论不是当前用户所评,无法删除');location.href='newRead.jsp?username="+username+"&nid="+nid+"'</script>");
}else{
//out.println("是当前登录用户的评论内容");
//根据当前登录的用户以及对应的评论编号进行删除
sql = "delete from tb_comment where cuserid = ? and cid = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, cid_cuserid);
ps.setInt(2, cid);
int n = ps.executeUpdate();
if(n>0){
out.println("<script>alert('删除成功');location.href='newRead.jsp?username="+username+"&nid="+nid+"'</script>");
}else{
out.println("<script>alert('删除失败');location.href='newRead.jsp?username="+username+"&nid="+nid+"'</script>");
}
}
%>