文章目录
前言
为了练习jsp和servlet写的一个简易表白墙项目,整体采用MVC设计,UI设计略渣
视图:index.jsp、ExpressWish_show.jsp
控制器:ExpressWish_servlet.java
模型:ExpressWish.java、ExpressWish_Bean.java
代码部分
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>表白信息页面</title>
</head>
<style>
.tom{
font-family: 宋体;
font-size: 18px;
color: blue;
}
</style>
<body>
<form action="handleExpress" class="tom" method="post">
表白者:<input type="text" class="tom" name="peopleName" size="28"/><br>
标题:<input type="text" class="tom" name="title" size="30"/><br>
内容:<br>
<textarea name="contents" class="tom" rows="10" cols="36"></textarea><br>
<input type="submit" class="tom" value="提交表白" name="submit">
</form>
<p class="tom"><a href="ExpressWish_show.jsp">查看表白墙</a></p>
</body>
</html>
ExpressWish_show.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="wishWallBean" class="save.data.ExpressWish_Bean" scope="application"/>
<html>
<head>
<title>表白墙</title>
</head>
<style>
.tom{
font-family: 宋体;
font-size: 26px;
color: blue;
}
</style>
<body bgcolor="#f5deb3">
<table border="1">
<tr>
<th class="tom">id</th>
<th class="tom">表白人</th>
<th class="tom">标题</th>
<th class="tom">时间</th>
<th class="tom">表白内容</th>
<%
for (int i=0;i< wishWallBean.size();i++){
out.print("<tr>");
out.print("<td class=tom>"+wishWallBean.getId(i)+"</td>");
out.print("<td class=tom>"+wishWallBean.getPeopleName(i)+"</td>");
out.print("<td class=tom>"+wishWallBean.getTitle(i)+"</td>");
out.print("<td class=tom>"+wishWallBean.getDateTime(i)+"</td>");
out.print("<td><textArea rows=5 clos=20 class=tom>"+wishWallBean.getContent(i)+"</textArea></td>");
out.print("</tr>");
}
%>
</table>
<a class="tom" href="index.jsp">去表白</a>
</body>
</html>
ExpressWish_servlet.java
package handle.data;
import save.data.ExpressWish;
import save.data.ExpressWish_Bean;
import sun.util.resources.LocaleData;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
public class ExpressWish_Servlet extends HttpServlet {
int index;
synchronized long getIndex(){
index++;
return index;
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.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=req.getParameter("peopleName");
String title=req.getParameter("title");
String content=req.getParameter("contents");
ExpressWish wish=new ExpressWish();
if(peopleName.length() == 0||title.length() == 0||content.length() == 0){
resp.sendRedirect("index.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);
resp.sendRedirect("ExpressWish_show.jsp");
}
}
ExpressWish.java
package save.data;
public class ExpressWish {
String content;
String title;
String dateTime;
String peopleName;
String id;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getDateTime() {
return dateTime;
}
public void setDateTime(String dateTime) {
this.dateTime = dateTime;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPeopleName() {
return peopleName;
}
public void setPeopleName(String peopleName) {
this.peopleName = peopleName;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
ExpressWish_show.jsp
package save.data;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
public class ExpressWish_Bean {
public HashMap<String,ExpressWish> wishList;
ArrayList<ExpressWish> wishes; //存放wishList中的表白信息
public ExpressWish_Bean() {
wishList=new HashMap<String,ExpressWish>();
wishes=new ArrayList<ExpressWish>();
}
//把表白放到wishes
void putToArrays(HashMap<String,ExpressWish> list){
wishes.clear();
Iterator<ExpressWish> iterator=list.values().iterator();
while(iterator.hasNext()){
ExpressWish wish=iterator.next();
wishes.add(wish);
}
}
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();
}
}
ExpressWish_Bean.java
package save.data;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
public class ExpressWish_Bean {
public HashMap<String,ExpressWish> wishList;
ArrayList<ExpressWish> wishes; //存放wishList中的表白信息
public ExpressWish_Bean() {
wishList=new HashMap<String,ExpressWish>();
wishes=new ArrayList<ExpressWish>();
}
//把表白放到wishes
void putToArrays(HashMap<String,ExpressWish> list){
wishes.clear();
Iterator<ExpressWish> iterator=list.values().iterator();
while(iterator.hasNext()){
ExpressWish wish=iterator.next();
wishes.add(wish);
}
}
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();
}
}
结果展示