基础4——会话技术
1.简介
2.Cookie
流程:
案例:返回上一次进入会话的时间
原理:
代码:
3.Session
案例:
1)输入框html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <center> <h1>商品列表</h1> <a href ="/download/asd?name=杯子">杯子</a><br/> <a href ="/download/asd?name=书包">书包</a><br/> <a href ="/download/asd?name=笔记本">笔记本</a> </center> </body> </html>
2)添加数据的servlet
package com.anli; import java.awt.List; import java.io.IOException; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class AddCartServlet */ @WebServlet("/asd") public class AddCartServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); //1.获取到商品的信息 String name=request.getParameter("name"); //2.需要保存商品信息到session HttpSession session=request.getSession(); ArrayList list=(ArrayList)session.getAttribute("list"); if(list==null) { list=new ArrayList(); } list.add(name); session.setAttribute("list", list); Cookie cookie=new Cookie("JSESSIONID",session.getId()); cookie.setMaxAge(60*60*24); cookie.setPath("/download"); response.addCookie(cookie); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
3)显示挑选的商品的购物车servlet
package com.anli; import java.io.IOException; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class GetCartServlet */ @WebServlet("/GetCartServlet") public class GetCartServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //下面那个方法内部已经封装了获取cookie从而在服务器端获取对应的session的逻辑 HttpSession session=request.getSession(); ArrayList<String> list=(ArrayList<String>)session.getAttribute("list"); response.setContentType("text/html;charset=utf-8"); for(String s:list) { response.getWriter().write(s+"<br/>"); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
每一章内容都是心血,希望大家不要抄袭~