效果图如下
没有用到数据库,只是用 Session 来存储 List。
代码如下
Message.java (JavaBean)
2、MessageServlet.java
index.jsp (视图层)
没有用到数据库,只是用 Session 来存储 List。
代码如下
Message.java (JavaBean)
- package com.liuyanzhao.bean;
- import java.util.Date;
- public class Message {
- private String name;
- private String titile;
- private Date createTime;
- private String content;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getTitile() {
- return titile;
- }
- public void setTitile(String titile) {
- this.titile = titile;
- }
- public Date getCreateTime() {
- return createTime;
- }
- public void setCreateTime(Date createTime) {
- this.createTime = createTime;
- }
- public String getContent() {
- return content;
- }
- public void setContent(String content) {
- this.content = content;
- }
- }
2、MessageServlet.java
- package com.liuyanzhao.servlet;
- import com.liuyanzhao.bean.Message;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- public class MessageServlet extends HttpServlet {
- List<Message> list = new ArrayList();
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //防止中文乱码
- response.setContentType("text/html;charset=UTF-8");
- request.setCharacterEncoding("UTF-8");
- String name = request.getParameter("name");
- String title = request.getParameter("title");
- String content = request.getParameter("content");
- Date createTime = new Date();
- Message message = new Message();
- message.setName(name);
- message.setTitile(title);
- message.setContent(content);
- message.setCreateTime(createTime);
- list.add(message);
- HttpSession session = request.getSession();
- session.setAttribute("messageList",list);
- response.sendRedirect("index.jsp");
- }
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- doPost(req,resp);
- }
- }
index.jsp (视图层)
- <%@ page import="com.liuyanzhao.bean.Message" %>
- <%@ page import="java.util.List" %>
- <%@ page import="java.text.SimpleDateFormat" %>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport"
- content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>Document</title>
- </head>
- <body>
- <table>
- <form action="message" method="post">
- <tr>
- <td>用户名</td>
- <td><input type="text" name="name"></td>
- </tr>
- <tr>
- <td>标题</td>
- <td><input type="text" name="title"></td>
- </tr>
- <tr>
- <td>内容</td>
- <td><textarea name="content" id="" cols="30" rows="10"></textarea></td>
- </tr>
- <tr>
- <td colspan="2"> <input type="submit" value="提交"></td>
- </tr>
- </form>
- </table>
- <%
- SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日hh时mm分ss秒");
- List list = (List) session.getAttribute("messageList");
- if(list != null) {
- for(int i=0;i<list.size();i++) {
- Message message = (Message) list.get(i);
- %>
- 楼层:<%=i+1%> 楼 <br>
- 留言人:<%=message.getName()%> <br>
- 时间:<%=format.format(message.getCreateTime())%> <br>
- 标题:<%=message.getTitile()%><br>
- 内容:<%=message.getContent()%><br>
- <hr>
- <%
- }
- }
- %>
- </body>
- </html>
2021年05月18日 16:56:25
只能评论一楼啊