当前位置 : 主页 > 编程语言 > java >

基于jsp的留言板(session)

来源:互联网 收集:自由互联 发布时间:2021-06-28
model package com.message.user;public class User {private String userName;private String userMessage;private String userLeaveMessageTime;public User(){}public String getUserName() {return userName;}public void setUserName(String userName) {
model
package com.message.user;

public class User {
	private String userName;
	private String userMessage;
	private String userLeaveMessageTime;
	public User(){}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserMessage() {
		return userMessage;
	}
	public void setUserMessage(String userMessage) {
		this.userMessage = userMessage;
	}
	public String getUserLeaveMessageTime() {
		return userLeaveMessageTime;
	}
	public void setUserLeaveMessageTime(String userLeaveMessageTime) {
		this.userLeaveMessageTime = userLeaveMessageTime;
	}
	public User(String userName, String userMessage, String userLeaveMessageTime) {
		super();
		this.userName = userName;
		this.userMessage = userMessage;
		this.userLeaveMessageTime = userLeaveMessageTime;
	}
	
}
controller
package com.message.user;


import java.io.IOException;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="MessageServlet",
			urlPatterns="/messageservlet")


public class MessageServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
		User user=new User();
		
		user.setUserMessage(request.getParameter("usermessage"));
		user.setUserName(request.getParameter("username"));
		user.setUserLeaveMessageTime(new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
									.format(new Date()).toString());
		
		List
 
   userList=(List
  
   )request.getSession().getAttribute("userlist"); if(userList==null) { userList=new ArrayList
   
    (); } userList.add(user); request.getSession().setAttribute("userlist", userList); request.getRequestDispatcher("Message.jsp").forward(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ doGet(request,response); } }
   
  
 
view0
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" language="java"%>





 
Insert title here



 
姓名:

信息:
view1
<%@ page language="java" contentType="text/html; UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.util.*"%>
<%@ page import="com.message.user.User" %>




 
Insert title here


	继续留言
	

<% @SuppressWarnings("unchecked") ArrayList userList=(ArrayList )session.getAttribute("userlist"); for(int i=0;i "+userList.get(i).getUserLeaveMessageTime()+""+""+"" +userList.get(i).getUserName()+":" +userList.get(i).getUserMessage()+""+""+""); %>
网友评论