//首页
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>index.jsp</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<BODY>
<TR>
<TD align="center" bgcolor="#CCCC99">
访客留言板
</TD>
</TR>
<TR>
<FORM method="get" action="addMessage.jsp">
<table>
<tr>
<TD bgcolor="#CCCC99">
姓名:
</TD>
<TD bgcolor="#CCCC99">
<INPUT type="text" name="name" size="25">
</TD>
<TD bgcolor="#CCCC99">
E-mail:
</TD>
<TD bgcolor="#CCCC99">
<INPUT type="text" name="email" size="25">
</TD>
<TD bgcolor="#CCCC99">
主题:
</TD>
<TD bgcolor="#CCCC99">
<INPUT type="text" name="title" size="25">
</TD>
</tr>
<br>
<tr>
<TD valign="top" bgcolor="#CCCC99">
留言:
</TD>
<TD>
<TEXTAREA name="content" rows="7" cols="25"></TEXTAREA>
</TD>
</tr>
<TD colspan="3">
<tr>
<TD align="center" bgcolor="#CCCC99">
<INPUT type="submit" value="提交留言">
</TD>
<TD align="center" bgcolor="#CCCC99">
<A href="viewyh.jsp"><FONT size="2">查看留言</FONT></A>
</TD>
<TD align="center" bgcolor="#CCCC99">
<INPUT type="reset" value="重新填写">
</TD>
</tr>
<hr>
</table>
</form>
<hr>
<br>
<form method="GET" action="login.jsp">
<TABLE>
<P align="center" bgcolor="#CCCC99">
管理员登陆
</P>
<form method="post" action="login.jsp">
<TD>
用户名
</TD>
<TD bgcolor="#cccc99">
<input type="text" name="userId" size=25>
</TD>
<TD bgcolor="#cccc99">
密码:
</TD>
<TD bgcolor="#cccc99">
<input type="password" name="password" size=25>
</TD>
<TD align="center" bgcolor="#cccc99">
<input type="submit" value="登陆">
</TD>
<TD align="center" bgcolor="#cccc99">
<input type="reset" value="重填">
</TD>
</TABLE>
</form>
</body>
</html>
//验证用户名和密码
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="com.microsoft.jdbc.sqlserver.SQLServerDriver"%>
<%@ page import="java.lang.*"%>
<%@ page contentType="text/html; charset=gb2312" %>
<%
request.setCharacterEncoding("gb2312");
String CLASSFORNAME="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String SERVANDDB="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jspdev";
String USER="sa";
String PWD="123";
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%
//????????
String id=request.getParameter("userId");
String psw=request.getParameter("password");
//?????
Class.forName(CLASSFORNAME);
Connection con = DriverManager.getConnection(SERVANDDB,USER,PWD);
Statement statement=con.createStatement();
String isCorrect="select * from user_info where userId='" + id + "' and password='" + psw + "'";
ResultSet result=statement.executeQuery(isCorrect);
session.setAttribute("isLog",new String("0"));
if(!result.next())
{
response.sendRedirect("index.jsp");//??????????????
result.close();
statement.close();
con.close();
}
//??????????????????
else
{
//???????session????
session.setAttribute("name",result.getString("name"));
session.setAttribute("id",result.getString("userId"));
session.setAttribute("email",result.getString("email"));
session.setAttribute("fav",result.getString("fanvcategory"));
session.setAttribute("userType",new Integer(result.getInt("userType")));
session.setAttribute("re","0");
System.out.println(session.getAttribute("userType"));
session.setAttribute("isLog",new String("1"));
int count=result.getInt("userLogCount");
session.setAttribute("userLogCount",new Integer(count));
count++;
session.setAttribute("userLastLogTime",result.getString("userLastLogTime"));
java.util.Date time1=new java.util.Date();//????????
String sqltime=new Timestamp(time1.getTime()).toString();//????????sql server???????
//???????
statement.execute("update user_info set userLogCount="+count+",userLastLogTime='"+sqltime+"' where userId='" + id +"'");
//?????????????
statement.close();
con.close();
//????????
response.sendRedirect("viewMessages.jsp");
}
%>
//增加留言
<%@ page import="java.sql.*,com.wen.*,java.util.*,java.sql.*" errorPage="../error.jsp"%>
<jsp:useBean id="messageVo" class="com.wen.MessageVO" scope="page">
<jsp:setProperty name="messageVo" property="*"/>
</jsp:useBean>
<jsp:useBean id="messageBean" class="com.wen.MessageBean" scope="page"/>
<HTML>
<HEAD>
<TITLE>add message into table </TITLE>
</HEAD>
<BODY>
<%
try
{
messageBean.setMessage(messageVo);
messageBean.addMessage();
}
catch(Exception e)
{
e.printStackTrace();
}
%>
<jsp:forward page="view.jsp" />
</body>
</html>
//查看留言 过客查看的 只能查看已经被管理员 审查了的留言
<%@ page import="java.sql.*,com.wen.*,java.util.*,java.sql.*"%>
<jsp:useBean id="messageBean" class="com.wen.MessageBean" scope="page" />
<HTML>
<HEAD>
<TITLE>show the message in the table</TITLE>
</HEAD>
<BODY>
<p align="center">
所有访客留言
</p>
<hr>
<form action="/liuyanban/search" method="get">
查询关键字:
<input type="text" name="neirong" onfocus="if(value=='请输入查询关键字'){value=" }" onblur="if(value==" ){
value='请输入查询关键字'}">
选择查询的类别:
<select name="op" size="1">
<option value="title">
标题
</option>
<option value="conten">
留言内容
</option>
<option value="add">
标题及留言内容
</option>
</select>
<input type="submit" name="wen" value="GO">
</form>
<hr>
<%int message_count = 0;
Collection messages = messageBean.getMessage();
Iterator it = messages.iterator();
while (it.hasNext()) {
MessageVO message = (MessageVO) it.next();
if (message.getflog() == 1) {
%>
<form name="form1" method="post" action="">
<TABLE width="100%" align="center" border=1 brcolor=99ccff cellspacing="0" cellpadding="0" bordercolordark="#000000" bordercolorlight="#CCCC99">
<tr>
<td bgcolor="#CCCC99">
<font size=2>主题:</font>
</td>
<td colspan=3>
<%=message.getTitle()%>
</td>
</tr>
<tr>
<td bgcolor="#CCCC99">
<font size=2>留言人:</font>
</td>
<td>
<%=message.getName()%>
</td>
<td bgcolor="#CCCC99">
<font size=2>E-mail:</font>
</td>
<td>
<%out.println("<a href=mailto:" + message.getEmail() + ">"
+ message.getEmail() + "</a>");
%>
</td>
</tr>
<tr>
<td bgcolor="#CCCC99">
<font size=2>留言时间:</font>
</td>
<td colspan=3>
<%out.println("<font size=2>"
+ message.getDate().toLocaleString() + "</font>");
%>
</td>
</tr>
<tr>
<td align="center">
<%out.println("(" + message_count + ")");
%>
</td>
<td colspan=3>
<%=message.getContent()%>
</td>
</tr>
</table>
<%out.println("<hr>");
message_count++;
}
}
%>
</form>
<p align="center">
<a href="index.jsp">我要留言</a>
</p>
</body>
</html>
//管理员 审查界面 可以删除 和通过审查
<%@ page import="java.sql.*,com.wen.*,java.util.*,java.sql.*"%>
<jsp:useBean id="messageBean" class="com.wen.MessageBean" scope="page" />
<HTML>
<HEAD>
<TITLE>show the message in the table</TITLE>
</HEAD>
<BODY>
<p align="center">
等待审查的访客留言
</p>
<hr>
<%int message_count = 0;
Collection messages = messageBean.getMessage();
Iterator it = messages.iterator();
while (it.hasNext()) {
MessageVO message = (MessageVO) it.next();
if (message.getflog() == 0) {
%>
<TABLE width="100%" align="center" border=1 brcolor=99ccff cellspacing="0" cellpadding="0" bordercolordark="#000000" bordercolorlight="#CCCC99">
<tr>
<td bgcolor="#CCCC99">
<font size=2>主题:</font>
</td>
<td colspan=3>
<%=message.getTitle()%>
</td>
</tr>
<tr>
<td bgcolor="#CCCC99">
<font size=2>留言人:</font>
</td>
<td>
<%=message.getName()%>
</td>
<td bgcolor="#CCCC99">
<font size=2>E-mail:</font>
</td>
<td>
<%out.println("<a href=mailto:" + message.getEmail() + ">"
+ message.getEmail() + "</a>");
%>
</td>
</tr>
<tr>
<td bgcolor="#CCCC99">
<font size=2>留言时间:</font>
</td>
<td colspan=3>
<%out.println("<font size=2>"
+ message.getDate().toLocaleString() + "</font>");
%>
</td>
</tr>
<tr>
<td align="center">
<%out.println("(" + message_count + ")");
%>
</td>
<td colspan=3>
<%=message.getContent()%>
</td>
</tr>
<tr>
<td>
<%out.println("<td><a href='del.jsp?id=" + message.getID()
+ "'>删除</a>");%>
</td>
</tr>
<tr>
<td>
<%out.println("<td><a href='shencha.jsp'>审查</a>");%>
</td>
</tr>
</table>
<%out.println("<hr>");
message_count++;
}
}
%>
</body>
</html>
//用户提交留言后的友好界面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>view</title>
<meta HTTP-EQUIV="REFRESH" CONTENT="1; URL=index.jsp">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
留言已经提交,请等待管理员审核! <br><hr>
非常感谢!<hr>
<script language="LiveScript">
alert("确定返回");
</script>
</body>
</html>
//删除功能
<%@ page language="java" import="com.wen.*,java.util.*" pageEncoding="UTF-8"%>
<%@ page import="javax.swing.JApplet" %>
<%@ page import="java.sql.Statement" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'del.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<jsp:useBean id="messagebean" class="com.wen.MessageBean" scope="page" />
<body>
<%
messagebean.deleteID((String)request.getParameter("id"));
response.sendRedirect("delcg.jsp");
%>
</body>
</html>
//删除后的友好界面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'delcg.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
删除成功!
<a href='viewMessages.jsp' >返回</a>
</body>
</html>
//显示用户查询后的页面
<%@ page import="java.sql.*,com.wen.*,java.util.*,java.sql.*" language="java" pageEncoding="UTF-8"%>
<HTML>
<HEAD>
<TITLE> show the message in the table </TITLE>
</HEAD>
<LINK href="../../hellking.css" type=text/css rel=stylesheet>
<BODY>
<p align="center">所有访客留言</p>
<hr>
<%
int message_count=0;
Collection messages=(Collection)request.getAttribute("messages");
Iterator it=messages.iterator();
while(it.hasNext())
{
MessageVO message=(MessageVO)it.next();
%>
<TABLE width="100%" align="center" border=1 brcolor=99ccff cellspacing="0" cellpadding="0" bordercolordark="#000000" bordercolorlight="#CCCC99">
<tr><td bgcolor="#CCCC99"><font size=2>主题:</font></td>
<td colspan=3><%=message.getTitle()%></td></tr>
<tr><td bgcolor="#CCCC99"><font size=2>留言人:</font></td>
<td><%=message.getName()%></td><td bgcolor="#CCCC99"><font size=2>E-mail:</font></td>
<td>
<%
out.println("<a href=mailto:"+message.getEmail()+">"+message.getEmail()+"</a>");
%>
</td></tr>
<tr><td bgcolor="#CCCC99"><font size=2>留言时间:</font></td><td colspan=3>
<%
out.println("<font size=2>"+message.getDate().toLocaleString()+"</font>");
%>
</td></tr>
<tr><td bgcolor="#CCCC99" align="center">
<%
out.println("("+message_count+")");
%>
</td>
<td colspan=3><%=message.getContent()%>
</td></tr>
</table>
<%
out.println("<hr>");
message_count++;
}
%>
<form method="post" action="/liuyanban/search">
<!--webbot bot="SaveResults" U-File="fpweb:///_private/form_results.csv" S-Format="TEXT/CSV" S-Label-Fields="TRUE" -->
<p align="center"><select size="1" name="op">
<option value="title">按标题</option>
<option value="content">按留言内容</option>
<option value="name">按留言人</option>
</select><input type="text" name="neirong" size="20"></p>
<p align="center"><input type="submit" value="搜索" name="B1"></p>
</form>
<p align="center"><a href="liu.html">我要留言</a></p>
</body>
</html>
//MessageBean.java
/*
*@31wen 文建国,创建日期:Apr 10, 2006
*Blog: http://blog.csdn.net/wen31
*
*/
package com.wen;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
public class MessageBean
{
private Connection con;
MessageVO msg;
//获得数据库连接。
public MessageBean()
{
String CLASSFORNAME="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String SERVANDDB="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jspdev";
String USER="sa";
String PWD="123";
try
{
Class.forName(CLASSFORNAME);
con = DriverManager.getConnection(SERVANDDB,USER,PWD);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void setMessage(MessageVO msg)
{
this.msg=msg;
}
public void addMessage()throws Exception
{
try
{
PreparedStatement stm=con.prepareStatement("insert into message values(?,?,?,?,?,?)");
stm.setString(1,msg.getTitle());
stm.setString(2,msg.getName());
stm.setDate(3,new java.sql.Date(new java.util.Date().getTime()));
if((msg.getEmail()).length()==0)
stm.setString(5,null);
else stm.setString(5,msg.getEmail());
stm.setString(4,msg.getContent());
stm.setInt(6,0);
try
{
stm.executeQuery();
}
catch(Exception e)
{
}
con.close();
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
public Collection getMessage()throws Exception
{
Collection ret=new ArrayList();
try
{
Statement stm=con.createStatement();
ResultSet result=stm.executeQuery("select count(*) from message");
int message_count=0;
if(result.next())
{
message_count=result.getInt(1);
result.close();
}
if(message_count>0)
{
result=stm.executeQuery("select * from message order by time desc");
while(result.next())
{ int flog=result.getInt("flog");
int id=result.getInt("id");
String title=result.getString("title");
String name=result.getString("name");
String mail=result.getString("mail");
String content=result.getString("content");
java.sql.Date date=result.getDate("time");
MessageVO message=new MessageVO();
message.setName(name);
message.setTitle(title);
message.setContent(content);
message.setDate(date);
message.setEmail(mail);
message.setID(id);
message.setflog(flog);
ret.add(message);
}
result.close();
stm.close();
}
con.close();
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
return ret;
}
public void deleteID(String id)throws Exception
{
Statement stmt=con.createStatement();
stmt.execute("delete from message where id="+id+"");
}
}
//MessageVO.java
/*
*@31wen 文建国,创建日期:Apr 10, 2006
*Blog: http://blog.csdn.net/wen31
*
*/
package com.wen;
public class MessageVO implements java.io.Serializable
{
private String name,email,title,content;
private int id,flog;
private java.sql.Date date;
public void setName(String name)
{
this.name=name;
}
public void setEmail(String email)
{
this.email=email;
}
public void setTitle(String title)
{
this.title=title;
}
public void setContent(String content)
{
this.content=content;
}
public String getName()
{
return this.name;
}
public String getContent()
{
return this.content;
}
public String getTitle()
{
return this.title;
}
public String getEmail()
{
return this.email;
}
public java.sql.Date getDate()
{
return this.date;
}
public void setDate(java.sql.Date date)
{
this.date=date;
}
public void setID(int id)
{
this.id=id;
}
public int getID()
{
return this.id;
}
public void setflog(int flog)
{
this.flog=flog;
}
public int getflog()
{
return this.flog;
}
}
//search.java
/*
*@31wen 文建国,创建日期:Apr 12, 2006
*Blog: http://blog.csdn.net/wen31
*
*/
package com.wen;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class search extends HttpServlet {
private Connection con;
public search() {
String CLASSFORNAME = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String SERVANDDB = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jspdev";
String USER = "bn";
String PWD = "bn";
try {
Class.forName(CLASSFORNAME);
con = DriverManager.getConnection(SERVANDDB, USER, PWD);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Collection ret = new ArrayList();
try {
String op1 = (String) request.getParameter("op").trim();
String neirong1 = request.getParameter("neirong").trim();
response.setContentType("text/html;charset=gb2312");
Statement stm = con.createStatement();
System.out.println(op1);
ResultSet result = stm.executeQuery("select count(*) from message");
int message_count = 0;
if (result.next()) {
message_count = result.getInt(1);
result.close();
}
System.out.println(message_count);
if (message_count > 0) {
result = stm.executeQuery("select * from message where " + op1
+ " like '%" + neirong1 + "%'");
while (result.next()) {
int flog = result.getInt("flog");
int id = result.getInt("id");
String title = result.getString("title");
String name = result.getString("name");
String mail = result.getString("mail");
String content = result.getString("content");
java.sql.Date date = result.getDate("time");
MessageVO message = new MessageVO();
message.setName(name);
message.setTitle(title);
message.setContent(content);
message.setDate(date);
message.setEmail(mail);
message.setID(id);
message.setflog(flog);
ret.add(message);
}
result.close();
stm.close();
}
request.setAttribute("messages", ret);
RequestDispatcher requestDispatcher = request
.getRequestDispatcher("sh.jsp");
requestDispatcher.forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
//shencha.java
/*
*@31wen 文建国,创建日期:Apr 12, 2006
*Blog: http://blog.csdn.net/wen31
*
*/
package com.wen;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class shencha extends HttpServlet {
/**
*
*/
private Connection con;
/**
* Constructor of the object.
*/
public shencha() {
String CLASSFORNAME="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String SERVANDDB="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jspdev";
String USER="sa";
String PWD="123";
try
{
Class.forName(CLASSFORNAME);
con = DriverManager.getConnection(SERVANDDB,USER,PWD);
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int flog1=Integer.parseInt(request.getParameter("flog1"));
String id1 =request.getParameter("id");
try {
if(flog1==1){
PreparedStatement stm=con.prepareStatement("update message set flog=? where id='"+id1+"'");
stm.setInt(1,flog1);
stm.execute();}
//把视图派发到目标
RequestDispatcher requestDispatcher = request.getRequestDispatcher("viewMessages.jsp");
requestDispatcher.forward(request,response);
} catch(Exception e) {
e.printStackTrace();
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
}