效果如图:
messageBoard.jsp页面提供表单,实现用户输入数据:
messageBoard.jsp:
<%@ 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 'messageBoard.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>
<form action="addmessageservlet" method="post">
留言者:<input type="text" name="author" size="30"/>
<br/>
留言标题:<input type="text" name="title" size="30"/>
<br>
留言内容:<textarea name="content" rows="8" cols="30"></textarea>
<br/>
<input type="submit" value="提交" >
<input type="reset" value="重置">
</form>
<a href="showmessage.jsp">查看留言</a>
</body>
</html>
MessageBean.java实现存储数据:
MessageBean.java:
package ex1;
public class MessageBean {
private String author;
private String title;
private String content;
private String time;
public MessageBean(){
}
public void setAuthor(String author) {
this.author = author;
}
public String getAuthor() {
return author;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setContent(String content) {
this.content = content;
}
public String getContent() {
return content;
}
public void setTime(String time) {
this.time = time;
}
public String getTime() {
return time;
}
}
addmessageservlet.java此servlet文件用于对数据进行处理以及显示在jsp页面上:
addmessageservlet.java:
package ex1;
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;
public class addmessageservlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
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);
HttpSession session=request.getSession();
ServletContext scx=session.getServletContext();
ArrayList wordlist=(ArrayList)scx.getAttribute("wordlist");
if(wordlist==null)
wordlist=new ArrayList();
wordlist.add(mm);
scx.setAttribute("wordlist", wordlist);
session.setAttribute("wordlist", wordlist);
response.sendRedirect("showmessage.jsp");
}
}
showmessage.jsp用于显示之前所有用户提交的信息,实现留言板效果:
showmessage.jsp:
<%@ page language="java" import="java.util.*,ex1.*" 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 'showmessage.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>
<%
ArrayList wordlist=(ArrayList)session.getAttribute("wordlist");
if(wordlist==null||wordlist.size()==0){
out.print("没有留言");
}else{
for(int i=wordlist.size()-1;i>=0;i--){
MessageBean mm =(MessageBean)wordlist.get(i);
%>
留言者:<%=mm.getAuthor() %>
<p>留言时间:<%=mm.getTime() %></p>
<p>留言标题:<%=mm.getTitle() %></p>
<p>留言内容:
<textarea rows="8" cols="30" readonly>
<%=mm.getContent() %>
</textarea>
</p>
<a href="messageBoard.jsp">我要留言</a>
<% }
} %>
</body>
</html>