源代码以及执行结果截图:
ExpressWish_Bean.java
package web;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
public class ExpressWish_Bean {
public HashMap<String,ExpressWish> wishList;
ArrayList<ExpressWish> wishes;
public ExpressWish_Bean(){
wishList = new HashMap<String,ExpressWish>();
wishes = new ArrayList<ExpressWish>();
}
public void addExpressWish(String id,ExpressWish expressWish){
wishList.put(id,expressWish);
putToArrays(wishList);
}
public void removeExpressWish(String id){
wishList.remove(id);
putToArrays(wishList);
}
public String getId(int index) {
return wishes.get(index).getId();
}
public String getPeopleName(int index) {
return wishes.get(index).getPeopleName();
}
public String getTitle(int index){
return wishes.get(index).getTitle();
}
public String getContent(int index){
return wishes.get(index).getContent();
}
public String getDateTime(int index){
return wishes.get(index).getDateTime();
}
public int size() {
return wishes.size();
}
void putToArrays(HashMap<String,ExpressWish> list){
wishes.clear();
Iterator<ExpressWish> iterator = list.values().iterator();
while(iterator.hasNext()){
ExpressWish wish = iterator.next();
wishes.add(wish);
}
}
}
ExpressWish.java
package web;
public class ExpressWish {
String contents;
String title;
String dateTime;
String peopleName;
String id;
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setPeopleName(String s) {
peopleName = s;
}
public String getPeopleName() {
return peopleName;
}
public void setContent(String s) {
contents = s;
}
public String getContent() {
return contents;
}
public void setTitle(String s) {
title = s;
}
public String getTitle() {
return title;
}
public void setDateTime(String s) {
dateTime = s;
}
public String getDateTime() {
return dateTime;
}
}
ExpressWish_Servlet.java
package text;
import web.ExpressWish;
import web.ExpressWish_Bean;
import java.util.*;
import java.io.*;
import java.time.LocalDateTime;
import javax.servlet.*;
import javax.servlet.http.*;
@SuppressWarnings("unused")
public class ExpressWish_Servlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
int index;
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
synchronized long getIndex() {
index = index+1;
return index;
}
public void service(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
request.setCharacterEncoding("utf-8");
ExpressWish_Bean wishWallBean = null;
ServletContext application = getServletContext();
wishWallBean = (ExpressWish_Bean)application.getAttribute("wishWallBean");
if(wishWallBean == null ){
wishWallBean = new ExpressWish_Bean();
application.setAttribute("wishWallBean",wishWallBean);
}
String peopleName = request.getParameter("peopleName");
String title = request.getParameter("title");
String content = request.getParameter("contents");
ExpressWish wish = new ExpressWish();
if(peopleName.length()==0||title.length()==0||content.length()==0){
response.sendRedirect("example7-2.jsp");
return;
}
wish.setPeopleName(peopleName);
wish.setTitle(title);
wish.setContent(content);
LocalDateTime dateTime = LocalDateTime.now();
String str = dateTime.toString();
String time =str.substring(0,str.lastIndexOf("."));
wish.setDateTime(time);
long number = getIndex();
wish.setId(""+number);
wishWallBean.addExpressWish(""+number,wish);
response.sendRedirect("example7-2-show.jsp");
}
}
example7-2.jsp
<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %>
<HTML>
<style>
#tom{
font-family:宋体;font-size:18;color:blue
}
</style>
<body bgcolor = #ffccff>
<form action="handleExpress" id="tom" method="post" >
表白者:<input type="text" id = "tom" name="peopleName" size = 28/>
<br>标题:<input type="text" id = "tom" name="title" size = 30/>
<br>内容:<br>
<textArea name="contents" id = "tom" rows="10" cols=36 >
</textArea>
<br><input type="submit" id="tom" value="提交表白" name="submit"/>
</form>
<p id="tom">
<a href="example7-2-show.jsp">查看表白墙</a>
</p></body></HTML>
example7-2-show.jsp
<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %>
<jsp:useBean id="wishWallBean" class ="web.ExpressWish_Bean" scope="application"/>
<style>
#tom{
font-family:宋体;font-size:26;color:blue
}
</style>
<HTML><body bgcolor=white>
<table border=1>
<tr><th id=tom>id</th><th id=tom>表白人</th><th id=tom>标题</th>
<th id=tom>时间</th><th id=tom>表白内容</th>
<% for(int i=0;i<wishWallBean.size();i++){
out.print("<tr>");
out.print("<td id=tom>"+wishWallBean.getId(i)+"</td>");
out.print("<td id=tom>"+wishWallBean.getPeopleName(i)+"</td>");
out.print("<td id=tom>"+wishWallBean.getTitle(i)+"</td>");
out.print("<td id=tom>"+wishWallBean.getDateTime(i)+"</td>");
out.print("<td ><textArea rows=5 cols=20 id=tom>"+wishWallBean.getContent(i)+
"</textArea></td>");
out.print("</tr>");
}
%> </table>
<a id =tom href="example7-2.jsp ">去表白</a>
</body></HTML>
example7-2-delete.jsp
<%@ page contentType="text/html" %>
<%@ page pageEncoding = "utf-8" %>
<jsp:useBean id="wishWallBean" class ="web.ExpressWish_Bean" scope="application"/>
<HTML><body bgcolor = pink>
<p style="font-family:宋体;font-size:18;color:blue">
管理员删除表白的页面。
<form action="" method=post >
输入密码:<input type="password" name="password" size=12 /><br>
输入表白id:<input type="text" name="peopleId" size=6 />
<br><input type="submit" name="submit" value="删除"/>
</form>
<% request.setCharacterEncoding("utf-8");
String password=request.getParameter("password");
String id=request.getParameter("peopleId");
if(password == null ) password = "";
if(id == null ) id = "";
if(password.equals("123456")){
wishWallBean.removeExpressWish(id);
}
%>
<a href="example7-2-show.jsp">查看表白墙</a>
</p></body></HTML>
web.xml
<?xml version="1.0" encoding="utf-8"?>
<web-app>
<!-- 以下是web.xml文件新添加的内容 -->
<servlet>
<servlet-name>handleExpress</servlet-name>
<servlet-class>web.ExpressWish_Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>handleExpress</servlet-name>
<url-pattern>/handleExpress</url-pattern>
</servlet-mapping>
</web-app>