（1）所使用的数据库的表如下：

字 段 名 称 | 类 型 | 说 明 | 是 否 可 以 为 空
title | varchar(100) | 留言的标题 | 否
name | varchar(20) | 留言者的姓名 | 否
email | varchar(50) | 留言者的 E-mail | 是
content | varchar(2000) | 留言的具体内容 | 否
data | datatime | 留言的时间 | 否

（2）将 SQLServer 的 JDBC 驱动程序拷贝到%TOMCAT_HOME%\

common\lib 目录下，在 SQLServer 数据库中建立一个数据库，名称为 jspdev，然后建立一个帐号（用户名：bn 密码：bn），并把对 jspdev 数据库的访问、修改权限分配给 bn 帐号。

（3）创建一个用于保存留言的 Servlet，用来保存留言的 Servlet 的 AddMessageServlet.java 文件代码如下：

package com.jspdev;

import javax.servlet.*;

import javax.servlet.http.*;

import com.microsoft.jdbc.sqlserver.SQLServerDriver;

import java.sql.*;

import java.util.*;

import java.io.*;

public class AddMessageServlet extends HttpServlet

{

private Connection con;

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException

{

String name=request.getParameter("name");

String mail=request.getParameter("email");

String title=request.getParameter("title");

String content=request.getParameter("content");

if(name==null)

name="";

if(title==null)

title="";

if(content==null)

content="";

if(mail==null)

mail="";

try

{

PreparedStatement stm=con.prepareStatement("insert into message values(?,?,?,?,?)");

stm.setString(1,title);

stm.setString(2,name);

if(mail.length()==0)

stm.setString(5,null);

else stm.setString(5,mail);

stm.setDate(3,new java.sql.Date(new java.util.Date().getTime()));

stm.setString(4,content);

try

{

stm.executeQuery();

}

catch(Exception e)

{

}

RequestDispatcher requestDispatcher = request.getRequestDispatcher("/viewMessages_servlet");

requestDispatcher.forward(request,response);

}

catch(Exception e)

{

e.printStackTrace();

}

}

public void doPost(HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException

{

doGet(request,response);

}

Public AddMessageServlet()

{

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();

}

}

}

（4）用于获得留言版信息的 Servlet 的 ViewMessageServlet.java 文件代码如下：

package com.jspdev;

import javax.servlet.*;

import javax.servlet.http.*;

import com.microsoft.jdbc.sqlserver.SQLServerDriver;

import java.sql.*;

import java.util.*;

import java.io.*;

public class ViewMessageServlet extends HttpServlet

{

private Connection con;

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException

{

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())

{

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);

ret.add(message);

}

result.close();

stm.close();

}

request.setAttribute("messages",ret);

RequestDispatcher requestDispatcher = request.getRequestDispatcher("/jsp_servlet_javabean/viewMessages.jsp");

requestDispatcher.forward(request,response);

}

catch(Exception e)

{

e.printStackTrace();

}

}

public ViewMessageServlet()

{

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();

}

}

public void doPost(HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException

{

doGet(request,response);

}

}

（5）Servlet 的配置：web.xml 文件代码如下：

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"

version="2.4">

<display-name>Welcome to Tomcat</display-name>

<description>

MVC 实验二

</description>

<servlet>

<servlet-name>ViewMessageServlet</servlet-name> <servlet-class>com.jspdev.ch9.ViewMessageServlet</servlet-class>

</servlet>

<servlet>

<servlet-name>AddMessageServlet</servlet-name> <servlet-class>com.jspdev.ch9.AddMessageServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>ViewMessageServlet</servlet-name>

<url-pattern>/viewMessages_servlet</url-pattern>

</servlet-mapping>

<servlet-mapping>

<servlet-name>AddMessageServlet</servlet-name>

<url-pattern>/addMessages_servlet</url-pattern>

</servlet-mapping>

</web-app>

（6）创建填写留言的界面

<HTML>

<HEAD>

<TITLE> message board </TITLE>

</HEAD>

<LINK href="..\..\hellking.css" type=text/css rel=stylesheet>

<BODY>

<TABLE width="90%" align="center" border=1 cellspacing="0" cellpadding="0" bordercolordark="#CCCC99" bordercolorlight="#CCCC99">

<TR>

<TD align="center" bgcolor="#CCCC99">访客留言板</TD>

</TR>

<TR>

<TD><TABLE align="center" border=1 cellspacing="0" cellpadding="0" bordercolordark="#CCCC99" bordercolorlight="#CCCC99">

<FORM ACTION="/addMessages_servlet">

<TR>

<TD>姓名：</TD>

<TD bgcolor="#CCCC99"><input type="text" name="name" size=25></TD>

</TR>

<TR>

<TD bgcolor="#CCCC99">E-mail：</TD>

<TD bgcolor="#CCCC99"><input type="text" name="email" size=25></TD>

</TR>

<TR>

<TD>主题：</TD>

<TD bgcolor="#CCCC99"><input type="text" name="title" size=25></TD>

</TR>

<TR>

<TD valign="top" bgcolor="#CCCC99">留言：</TD>

<TD><textarea name="content" rows=7 cols=25></textarea></TD>

</TR>

<TR>

<TD colspan=3><TABLE align="center" width="100%" cellspacing="0" cellpadding="0" bordercolordark="#CCCC99" bordercolorlight="#CCCC99">

<TR>

<TD align="center" bgcolor="#CCCC99"><input type="submit" value="提交留言"></TD>

<TD align="center"><a href="/ch9/viewMessages_servlet"><font size=2>查看留言</font></a></TD>

<TD align="center" bgcolor="#CCCC99"><input type="reset" value="重新填写"></TD>

</TR>

</TABLE></TD>

</TR>

</FORM>

</TABLE></TD>

</TR>

</TABLE>

</BODY>

</HTML>

（7）创建显示留言的 JSP 页面：viewMessage.jsp 的代码如下：

<%@ page import="java.sql.*,com.jspdev.*,java.util.*,java.sql.*"%>

<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 align="center">

<%

out.println("("+message_count+")");

%>

</td>

<td colspan=3><%=message.getContent()%>

</td></tr>

</table>

<%

out.println("<hr>");

message_count++;

}

%>

<p align="center"><a href="index.html">我要留言</a></p>

</body>

</html>