用Jsp + Servlet + JavaBean 做的一个简单的留言板,可以提交留言和查看留言。
注意,Servlet的配置问题。
<servlet>标签表示:这是一个servlet的声明
<display-name>标签表示:该servlet的显示名称,不重要,可以不用配置
<servlet-name>标签表示:该servlet在web容器中的名称,必需配置
<servlet-class>标签表示:该servlet所对应java类的路径,必需配置
<servlet-mapping>标签表示:该servlet在web容器中的映射,必需配置
<servlet-name>标签表示:这个servlet映射所对应的是哪一个servlet,它就是需要填入上面的那个<servlet-name>标签中的值,必需配置
<url-pattern>标签表示:这个servlet在web容器中的请求路径,必需配置
-
追问:
-
谢谢在麻烦您一下
我有一个页面newTopic.jsp
<form name="form1" action="newtopicservlet" method="post">
然后servlet包里有个NewTopicServlet.java的servlet我要怎么写配置呢?
-
回答:
-
<servlet>
<servlet-name>NewTopicServlet</servlet-name>
<servlet-class>servlet.NewTopicServlet</servlet-class>
</servlet><servlet-mapping>
<servlet-name>NewTopicServlet</servlet-name>
<url-pattern>/NewTopicServlet</url-pattern>
</servlet-mapping>
下面是一个例子:
<%@ page language="java" contentType="text/html" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>留言板页面</title>
</head>
<body background="image/abc.jpg">
<form action="AddMessageServlet" method="post">
<h1>留言板</h1>
<h3>留 言 者 :<input type="text" name="author" size="30"></h3>
<h3>留言标题 :<input type="text" name="title" size="30"></h3>
<h3>留言内容 :</h3>
<textarea rows="15" cols="100" name="content"></textarea>
<p>
<h3>
<input type="submit" name="submit" value="提交" style="height:30px;width:80px">
<input type="reset" name="reset" value="重置" style="height:30px;width:80px">
<a href="showmessage.jsp">查看留言</a>
</h3>
</form>
</body>
</html>
<%@page import="bean.MessageBean"%>
<%@ page language="java" contentType="text/html" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>显示留言内容</title>
</head>
<body background="image/abc.jpg">
<%
ArrayList wordlist = (ArrayList)application.getAttribute("wordlist");
if(wordlist == null || wordlist.size()==0){
out.print("目前还没有留言!");
%>
<a href="index.jsp"><h3>我要留言</h3></a>
<%
}
else{
for(int i=wordlist.size()-1;i>=0;i--){
MessageBean mm = (MessageBean)wordlist.get(i);
%>
<h2> 留言板内容 </h2>
<h4> 留 言 者:<%=mm.getAuthor() %></h4>
<h4> 留言时间:<%=mm.getTime() %></h4>
<h4> 留言标题:<%=mm.getTitle() %></h4>
<h4> 留言内容:<p><textarea rows="15" cols="100" readonly><%=mm.getContent() %></textarea></h4>
<a href="index.jsp"><h3>我要留言</h3></a>
<hr width ="90">
<%
}
}
%>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>AddMessageServlet</servlet-name>
<servlet-class>servlet.AddMessageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddMessageServlet</servlet-name>
<url-pattern>/AddMessageServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import bean.MessageBean;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;
import com.sun.xml.internal.messaging.saaj.soap.ver1_1.Message1_1Impl;
public class AddMessageServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public AddMessageServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* 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 {
doPost(request,response);
}
/**
* 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 {
String author = new String(request.getParameter("author").getBytes("ISO-8859-1"),"UTF-8");
String title = new String(request.getParameter("title").getBytes("ISO-8859-1"),"UTF-8");
String content = new String(request.getParameter("content").getBytes("ISO-8859-1"),"UTF-8");
//获取当前时间并格式化时间为指定格式
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String today = format.format(new Date());
MessageBean mm = new MessageBean();
mm.setAuthor(author);
mm.setTitle(title);
mm.setContent(content);
mm.setTime(today);
//获取session对象
HttpSession session = request.getSession();
//通过session对象获取应用上下文
ServletContext scx = session.getServletContext();
//获取存储在应用上下文中的集合对象
ArrayList wordlist = (ArrayList)scx.getAttribute("wordlist");
if(wordlist == null)
wordlist = new ArrayList();
//将封装了信息的JavaBean值存储到集合对象中
wordlist.add(mm);
//将集合对象保存到应用上下文中
scx.setAttribute("wordlist", wordlist);
response.sendRedirect("showmessage.jsp");
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
package bean;
public class MessageBean {
private String author;
private String title;
private String content;
private String time;
public MessageBean(){
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}