6.写个留言本,挺垃圾的,关键看过程。
1。eclipse里建立工程,然后建立package,package名为test.guestbook。 然后把java文件放到src\test\guestbook下,刷新。 然后Project属性--Java Build Path里加入servlet.jar包和mssqlserver.jar包,还有msutil.jar和msbase.jar,后面3个就是SQL Server的JDBC驱动,正常应该没有错了。
然后写java代码就行了。 2。文件说明:
DBConnection.java:写一个方法,返回Connection。 MessageVO.java:写若干Get和Set方法,对应要用到的字段。 AddMessageServlet.java:补写doGet方法,接收画面输入,存储到数据库。 ViewMessageServlet.java:查询数据库,返回每条记录对应一个MessageVO对象,将这些对象放到一个ArrayList里。 3.DB说明:连接你的DB的IP,数据库为GuestBook,表名为message。(这里都是举例,具体根据你的情况)
message表结构: ID 自动标识,1,1 title nvarchar 100 UserName nvarchar 40 AddTime datetime 8 content ntext 16 Email nvarchar 100 4。工程的部署。 任意一个目录,按以下结构放置所有文件。我这是放在war目录下的。 └─War │ viewMessages.jsp │ index.jsp │ └─WEB-INF │ web.xml │ ├─classes │ └─test │ └─guestbook │ AddMessageServlet.class │ MessageVO.class │ ViewMessageServlet.class │ DBConnection.class │ └─lib servlet.jar mssqlserver.jar msutil.jar msbase.jar
然后命令行,进入war目录,输入以下命令: jar -cvf GuestBook.war *.* 如果没有错,就会生成一个GuestBook.war文件。 复制到Bea的user_projects\domains\mydomain\applications目录下。 然后访问http://localhost:7001/GuestBook/index.jsp 测试即可,别忘了启动weblogic。
<5>文件内容 首先看web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>GuestBook</display-name> <description>Test GuestBook</description> <servlet> <servlet-name>ViewMessageServlet</servlet-name> <servlet-class>test.guestbook.ViewMessageServlet</servlet-class> </servlet> <servlet> <servlet-name>AddMessageServlet</servlet-name> <servlet-class>test.guestbook.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>
<servlet-mapping> <servlet-name>AddMessageServlet</servlet-name> <url-pattern>/addMessages_servlet</url-pattern> </servlet-mapping> 上面这段,说明url的映射, 一切http://xxxx/GuestBook/addMessages_servlet?xadf之类的URL都会提交到 AddMessageServlet这个名字的Servlet,然后 <servlet> <servlet-name>AddMessageServlet</servlet-name> <servlet-class>test.guestbook.AddMessageServlet</servlet-class> </servlet> 这段说明AddMessageServlet这个Servlet对应的就是test.guestbook.AddMessageServlet这个类,明白了吧。实际上Servlet里面要写的就是doPost和doGet两个方法。
ViewMessageServlet.java
package test.guestbook;
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 {
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException { //TODO //request.setAttribute("messages",resultArrayList); RequestDispatcher requestDispatcher = request.getRequestDispatcher("/viewMessages.jsp"); requestDispatcher.forward(request,response); } public ViewMessageServlet() {
} public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException { doGet(request,response); } }
AddMessageServlet.java
package test.guestbook;
import javax.servlet.*; import javax.servlet.http.*; import com.microsoft.jdbc.sqlserver.SQLServerDriver;
import java.nio.charset.CharsetEncoder; import java.sql.*; import java.util.*; import java.io.*; public class AddMessageServlet extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException { request.setCharacterEncoding("UTF-8"); //TODO
//Forward RequestDispatcher requestDispatcher = request.getRequestDispatcher("/viewMessages_servlet"); requestDispatcher.forward(request,response); }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doGet(request,response); } public AddMessageServlet() {
} }
DBConnection.java
package test.guestbook;
public class DBConnection {
}
MessageVO.java
package test.guestbook; public class MessageVO implements java.io.Serializable {
}
viewMessage.jsp
<%@ page contentType="text/html;charset=UTF-8"%> <%@ page import="java.sql.*,test.guestbook.*,java.util.*"%>
<HTML> <HEAD> <meta http-equiv="Content-Type" Content="text/html; charset=UTF-8"> <TITLE> 所有访客留言 </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" width="15%"><font size=2>留言人:</font></td> <td width="15%"><%=message.getName()%></td> <td bgcolor="#CCCC99" width="15%"><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> <% message_count++; } %> <p align="center"><a href="index.jsp">我要留言</a></p> </body> </html>
index.jsp
<%@ page contentType="text/html;charset=UTF-8"%> <%@ page import="java.sql.*,test.guestbook.*,java.util.*"%>
<!-- index.html --> <HTML> <HEAD> <meta http-equiv="Content-Type" Content="text/html; charset=UTF-8"> <TITLE> message board </TITLE> </HEAD> <LINK href="..\..\hellking.css" type=text/css rel=stylesheet> <BODY> <p> <% String errorMessage=(String)request.getAttribute("errorMessage"); if(errorMessage!=null && errorMessage.length()>0){ out.println(errorMessage); } %> <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" method="post"> <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="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>
|