码迷,mamicode.com
首页 > Web开发 > 详细

使用JSP、Servlet实现一个简单的留言板

时间:2015-06-23 23:02:55      阅读:1000      评论:0      收藏:0      [点我收藏+]

标签:

1 留言板的页面 messageBoard.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>留言板页面</title>
</head>
<body bgcolor="CCCFFF">
    <center>
        <form action="AddMessageServlet" method="post">
            留言者:<input type="text" name="author" size="30">
            <br>
            留言标题:<input type="text" name="title" size="30">
            <br>
            留言内容:<textarea rows="10" cols="30" name="content"></textarea>
            <p>
            <input type="submit" value="提交">
            <input type="reset" value="取消">
            <a href="showMessage.jsp">查看留言</a>
        </form>
    </center>
</body>
</html>

2 留言板信息处理Servlet程序 AddMessageServlet.java

package servlet;

import java.io.IOException;
import JavaBean.MessageBean;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class AddMessageServlet
 */
@WebServlet("/AddMessageServlet")
public class AddMessageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddMessageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String author = request.getParameter("author");
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		//获取当前时间并格式化时间
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String today = format.format(new Date());
		MessageBean message = new MessageBean();
		message.setAuthor(author);
		message.setTitle(title);
		message.setContent(content);
		message.setTime(today);
		//获取session对象
		HttpSession session = request.getSession();
		//获取ServletContext
		ServletContext context = session.getServletContext();
		//获取存储在context中的集合对象
		ArrayList wordlist = (ArrayList)context.getAttribute("wordlist");
		if(wordlist == null)
			wordlist = new ArrayList();
		//将封装了信息的值JavaBean存储到集合对象中
		wordlist.add(message);
		//将集合对象保存到应用上下文中
		context.setAttribute("wordlist", wordlist);
		response.sendRedirect("showMessage.jsp");
	}

}

3 用于保存数据的JavaBean  MessageBean.java

package JavaBean;

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

4  显示留言板信息页面  showMessage.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="JavaBean.MessageBean" %>
<%@ page import="java.util.ArrayList" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>显示留言信息</title>
</head>
<body bgcolor="CCCFFF">
    <center>
        <%
            ArrayList wordlist = (ArrayList)application.getAttribute("wordlist");
            if(wordlist==null||wordlist.size() == 0)
            	out.print("no message!");
            else {
            	for(int i = wordlist.size()-1; i >= 0; i--){
            		MessageBean message = (MessageBean) wordlist.get(i);
        %>
        <p>author:<%=message.getAuthor() %></p>
        <p>time:<%=message.getTime() %></p>
        <p>title:<%=message.getTitle() %></p>
        <p>
            content:
            <textarea rows="10" cols="30" readonly><%=message.getContent() %></textarea>
        </p>
        <a href="messageBoard.jsp">我要留言</a>
        <%
            	}
            }
        %>
    </center>
</body>
</html>

5 Servlet的配置文件  web.xml

此处省略

  

使用JSP、Servlet实现一个简单的留言板

标签:

原文地址:http://www.cnblogs.com/darrensun/p/4596387.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!