留言板管理界面相比留言板界面增加了在每个留言后面拥有了删除的功能。
在验证是否是管理员的时候,是自己提前设置的。在admin_check.jsp中的if(name.equals("管理员用户名")&&psd.equals("密码")){}中设置。
管理员登录界面:admin.html
<html>
<head>
<title>管理员登录</title>
</head>
<body>
<form action="admin_check.jsp" method="post">
<table align="center" border="2" bgcolor="#B3B3FF">
<caption>欢迎访问管理员登录页面</caption>
<tr>
<td>用户名:</td>
<td><input type="text" name="user" size="45" value="lisi"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" size="45" value="123456"></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" value="提交" ><input type="reset" value="重填"></td>
</tr>
</table>
</form>
</body>
</html>
管理员登录检查:admin_check.jsp
<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="java.sql.*"%>
<%@ include file="util.jsp" %>
<%
request.setCharacterEncoding("gb2312");
String name=request.getParameter("user");
String pwd=request.getParameter("password");
if(name==null || pwd==null ){
%>
用户名密码不能为空,请重新<a href="admin.html">登录</a>
<%
return;
}
name=name.trim();
pwd=pwd.trim();
if(name.length()==0 || pwd.length()==0){
%>
<font size="7" color="red">用户名/密码不能为空。3秒后自动返回,请重新输入!</font>
<%
response.setHeader("refresh","3;admin.html");
return;
}
if(name.equals("lisi")&&pwd.equals("123456")){
session.setAttribute("admin","true");
response.sendRedirect("admin_index.jsp");
}else{
%>
<font size="7" color="red">用户名/密码错误。3秒后自动返回,请重新输入!</font>
<%
response.setHeader("refresh","3;admin.html");
return;
}
%>
管理员的留言板界面:admin_index.jsp
<%@ page contentType="text/html;charset=utf-8" import="java.sql.*"%>
<%@ page import="java.sql.*"%>
<%
request.setCharacterEncoding("gb2312");
String admin = (String)session.getAttribute("admin");
if(admin==null||(!(admin.equals("true")))){
%>
对不起,你无权访问页面,请<a href="admin.html">返回</a>
<%
return;
}
%>
<html>
<head><title>留言板主页</title></head>
<body>
<a href="admin.html">退出</a><br>
<%
Class.forName("com.mysql.jdbc.Driver");//加载驱动
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mybase","root","123456");//连接数据库
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=stmt.executeQuery("select * from guestbook order by gst_time desc");
rs.last();
int count= rs.getRow();
if(count==0){
out.print("当前没有留言");
}else{
%>
共有<%= count%>条留言
<%
}
String curPagestr=request.getParameter("page");
int curPage;
if(curPagestr==null){
curPage=1;
}else{
curPage=Integer.parseInt(curPagestr);
}
if(curPage==1){
%>
第一页 上一页
<%
}else{
%>
<a href="admin_index.jsp?page=1">第一页</a>
<a href="admin_index.jsp?page=<%=curPage-1%>">上一页</a>
<%
}
int countPerPage=3;
int pageCount= (count+countPerPage-1)/countPerPage;
if(curPage==pageCount){
%>
下一页 最后页
<%
}else{
%>
<a href="admin_index.jsp?page=<%=curPage+1%>">下一页</a>
<a href="admin_index.jsp?page=<%=pageCount%>">最后页</a>
<%
}
rs.absolute((curPage-1)*countPerPage+1);
rs.previous();
int i=0;
while(rs.next()&i<countPerPage){
out.print("<hr><br>");
out.print("用户名:"+rs.getString("gst_user"));
out.print(" ");
out.print("留言时间:"+rs.getDate("gst_time"));
out.print(" ");
out.print("用户IP:"+rs.getString("gst_ip"));
out.print("<br>");
out.print("标题:"+rs.getString("gst_title"));
out.print("<br>");
out.print("内容:"+rs.getString("gst_content"));
out.print(" ");
out.print("<a href='admin_del.jsp?id="+rs.getInt("gst_id")+"'>删除</a>");
i++;
}
%>
</body>
</html>
实现单击删除:admin_del.jsp
<%@ page contentType="text/html;charset=utf-8" import="java.sql.*"%>
<%@ page import="java.sql.*"%>
<%@ include file="util.jsp" %>
<%
request.setCharacterEncoding("gb2312");
String admin= (String)session.getAttribute("admin");
if(admin==null||(!(admin.equals("true")))){
%>
非法访问,请<a href="admin.html">返回</a>
<%
return;
}
String idStr = request.getParameter("id");
int id = Integer.parseInt(idStr);
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mybase","root","123456");//连接数据库
Statement stmt=conn.createStatement();
String sql = "delete from guestbook where gst_id = " +id;
stmt.executeUpdate(sql);
stmt.close();
conn.close();
response.sendRedirect("admin_index.jsp");
%>
util.jsp文件:
<%!
public String toHtml(String str)
{
if(str==null)
return null;
StringBuffer sb = new StringBuffer();
int len = str.length();
for (int i = 0; i < len; i++)
{
char c = str.charAt(i);
switch(c)
{
case '\'':
sb.append("'");
break;
case '<':
sb.append("<");
break;
case '>':
sb.append(">");
break;
case '&':
sb.append("&");
break;
case '"':
sb.append(""");
break;
default:
sb.append(c);
}
}
return sb.toString();
}
%>