@WebServlet("/reply") public class ReplyServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // set the encoding req.setCharacterEncoding("UTF-8"); resp.setContentType("text/html;charset=UTF-8"); // Create a session object HttpSession session = req.getSession(); List<Reply> list = (List<Reply>) session.getAttribute("list"); // judging session there is no data available directly jump did not check if the database if (list==null){ // layer connection service QuaryServiceImp quaryServiceImp = new QuaryServiceImp(); List<Reply> replies = quaryServiceImp.quaryAll(); session.setAttribute("list", replies); } resp.sendRedirect("reply.jsp"); } }
<%@ page import="java.util.List" %> <%@ page import="com.bjsxt.entiy.Reply" %><%-- Created by IntelliJ IDEA. User: 60590 Date: 2019/11/28 Time: 19:36 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <base href=<%= request.getContextPath()%>/> <script src="jq/jquery-1.9.1.js"></script> <script> $(function () { $("td:even").css("background","pink"); }) </script> </head> <body> <% HttpSession session1 = request.getSession(); List<Reply> list = (List<Reply>) session1.getAttribute("list"); %> <table border="1px"width="50%"> <tr> <Th> Comment Number </ th> <Th> message number </ th> <Th> add comment </ th> <Th> Review </ th> <Th> Comments Time </ th> </tr> <% for (Reply reply:list) { %> <tr> <td><%=reply.getReplyid()%></td> <td><%=reply.getTopicid()%></td> <td><%=reply.getAuthor()%>></td> <td><%=reply.getContent()%></td> <td><%=reply.getCreatedate()%></td> </tr> <% } %> </table> </body> </html>
<%-- Created by IntelliJ IDEA. User: 60590 Date: 2019/11/28 Time: 19:29 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <base href=<%= request.getContextPath()%>/> </head> <body> << a href = "reply"> Click Queries all posts </a> </body> </html>
Cookie and Session of difference
Thing in common: the same data sharing different user's request
difference: Cookie is the client's data storage technology,
the Session is a server-side data storage technology
cookie data is stored in the client, session data on the server (sessionid cookie can be stored in the client, URL rewrite mode can also be used)
• cookie is not very secure (encrypt), others can be analyzed at a local store and COOKIE COOKIE cheat, taking into account the security should be used session
• session is saved on the server for a certain time. When accessing the increase will compare the performance of your server footprint, taking into account mitigating server performance, you should use COOKIE
• A single client's cookie limit is 3K, a site that is stored in the client COOKIE not 3K.
• Cookie data is stored as a string. Session object information can be saved.
• Typical use
• Cookie: Remember me recently viewed a product page skin
• session: login cart (you can also use Cookie)