会话技术

会话,现实生活中会话的含义:交谈、谈话。

在web访问过程中,会话代表什么意思呢?

浏览器打开一个网站,然后访问当前网站的诸多页面,最后关闭,整个的过程叫做一个会话。

HTTP协议,是一个无状态协议。在服务器看来,任何浏览器发送过来的请求数据都是完全相同的,一模一样,服务器是无法却别该请求来自于哪个浏览器。

比如:http://192.168.2.100/app/index.html

A和B用户同时访问了当前页面,发送的HTTP请求报文是完全相同的,对于服务器来说,它是无法区别该请求来自于a还是b

实际场景下,又有这样的需求,我们需要服务器可以给我们浏览器去保存一些数据,比如浏览记录、购物车

就会涉及到我们今天介绍的技术,会话技术。

会话技术i其实是为了解决HTTP协议无状态性而产生的一个解决方案。

会话技术可以分为两种,一个是客户端技术,一个是服务器技术。

客户端技术Cookie

Cookie是由服务器创建的,然后接下来,Cookie会被发送给客户端(响应报文,响应头 Set-Cookie:name=value),客户端会将cookie信息保存下来,下次再次访问服务器时,它会把整个cookie重新带回去给服务器(请求报文,请求头 Cookie:name=value),服务器接收到cookie(又由于cookie的值是唯一的),所以就可以利用cookie来进行会话管理。

服务器技术Session

Cookie

  1. package com.cskaoyan.cookie;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.Cookie;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. import java.util.Date;
  10. @WebServlet("/cookie1")
  11. public class CookieServlet extends HttpServlet {
  12. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  13. }
  14. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  15. //接收cookie的代码应该写在上面
  16. Cookie[] cookies = request.getCookies();
  17. if(cookies != null){
  18. for (Cookie cookie : cookies) {
  19. if("lastVisit".equals(cookie.getName())){
  20. String value = cookie.getValue();
  21. Date date = new Date(Long.parseLong(value));
  22. response.getWriter().println(date);
  23. }
  24. }
  25. }
  26. //在内部调用了getHeaders(Cookie)
  27. //显示用户的上次访问时间
  28. //生成cookie cookie的value值里面不能有空格
  29. Cookie cookie = new Cookie("lastVisit", System.currentTimeMillis() + "");
  30. // 以Set-Cookie响应头发送给客户端
  31. //response.setHeader("Set-Cookie", "latLogin=xxx");
  32. //EE规范同样给我们封装了一个简便的设置cookie的方法
  33. response.addCookie(cookie);
  34. //addCookie{
  35. // setHeader(Set-cookie, cookie.getName = cookie.getValue)
  36. // }
  37. }
  38. }

设置存活时间

cookie默认情况下只存在于浏览器的内存中,关闭浏览器,则cookie失效,如果希望cookie能够进行持久化保存,那么可以设置一个存活时间

  1. Cookie cookie = new Cookie("username", username);
  2. //单位是秒
  3. cookie.setMaxAge(180);

设置一个以秒为单位的时间(正数),表示它会在硬盘上存活多久,如果设置的是负数,和不设置是一样的

0表示的是删除cookie

  1. package com.cskaoyan.cookie;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.Cookie;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. @WebServlet("/login")
  10. public class LoginServlet extends HttpServlet {
  11. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  12. request.setCharacterEncoding("utf-8");
  13. response.setContentType("text/html;charset=utf-8");
  14. String username = request.getParameter("username");
  15. String password = request.getParameter("password");
  16. //假设登录成功,那么需要将用户名放入到cookie中
  17. Cookie cookie = new Cookie("username", username);
  18. //单位是秒
  19. cookie.setMaxAge(180);
  20. response.addCookie(cookie);
  21. response.getWriter().println("登录成功,即将跳转至个人主页....");
  22. response.setHeader("refresh", "2;url=" + request.getContextPath() + "/info");
  23. }
  24. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  25. }
  26. }
  1. package com.cskaoyan.cookie;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.Cookie;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. @WebServlet("/info")
  10. public class InfoServlet extends HttpServlet {
  11. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  12. }
  13. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14. response.setContentType("text/html;charset=utf-8");
  15. Cookie[] cookies = request.getCookies();
  16. if(cookies != null){
  17. for (Cookie cookie : cookies) {
  18. if("username".equals(cookie.getName())){
  19. response.getWriter().println("欢迎您," + cookie.getValue());
  20. //0表示删除cookie
  21. cookie.setMaxAge(0);
  22. response.addCookie(cookie);
  23. }
  24. }
  25. }
  26. }
  27. }

设置路径

默认情况下,当访问当前服务器下面所有的资源时,默认都会携带cookie,如果希望仅某些页面去携带cookie,那么可以设置一个path

  1. package com.cskaoyan.cookie.path;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.Cookie;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. @WebServlet("/path1")
  10. public class PathServlet1 extends HttpServlet {
  11. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  12. }
  13. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14. Cookie cookie = new Cookie("key", "path");
  15. //直接写/应用名开头的路径
  16. cookie.setPath(request.getContextPath() + "/path2");
  17. response.addCookie(cookie);
  18. }
  19. }
  1. package com.cskaoyan.cookie.path;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.Cookie;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. @WebServlet("/path2")
  10. public class PathServlet2 extends HttpServlet {
  11. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  12. }
  13. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14. //如果cookie设置了path,这个时候希望将cookie删除,那么可以怎么做呢
  15. //需要特别注意一点,在删除cookie的时候,有一个注意事项,如果当前cookie设置了path
  16. //那么删除cookie的时候,需要再将path再写一遍
  17. //注意:删除cookie时浏览器删除
  18. Cookie[] cookies = request.getCookies();
  19. if(cookies != null){
  20. for (Cookie cookie : cookies) {
  21. if("key".equals(cookie.getName())){
  22. response.getWriter().println(cookie.getValue());
  23. cookie.setMaxAge(0);
  24. cookie.setPath(request.getContextPath() + "/path2");
  25. response.addCookie(cookie);
  26. }
  27. }
  28. }
  29. }
  30. }

设置域名

注意:cookie有一个大的原则,不可以设置和当前域名无关的cookie,比如当前域名localhost,然后你设置了一个域名叫做cskaoyan.com的cookie,那么其实时设置不成功的。

aaa.com ——— 你申请的

sub.aaa.com ———- 这个域名是属于你的

third.sub.aaa.com————这个域名也是属于你的

父子域名

如果你设置了一个aaa.com域名的cookie,那么默认情况下,下面所有的子域名都可以共享该cookie

  1. package com.cskaoyan.cookie.domain;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.Cookie;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.IOException;
  9. @WebServlet("/domain2")
  10. public class DomainServlet2 extends HttpServlet {
  11. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  12. }
  13. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14. Cookie cookie = new Cookie("user", "aaa.com");
  15. cookie.setDomain("aaa.com");
  16. response.addCookie(cookie);
  17. }
  18. }

cookie的优缺点

优点:小、轻便、放在浏览器,服务器没有压力

缺点:存储数据比较少,value值只能是字符串类型,有限制;安全问题;

cookie其实可以用来存放一些非敏感性数据,

Session

服务器技术。session对象是在服务器产生的,但是呢,保存也是在服务器上面。

session你可以认为是服务器给每个浏览器所开辟的一块内存空间,将这块内存空间和浏览器做一个绑定,如果今后当前浏览器再次访问服务器,如果需要进行数据的存取,那么就放置在当前内存空间中。

浏览器在访问session的时候,session会给浏览器创建一个独一无二的session对象,session对象有一个id,会把session的id加上响应报文中发送给客户端,客户端接收到之后将其保存,下次再次访问服务器时,将该id再携带回来,服务器就可以取出session的id值,那么就可以拿到原先的session对象。

只需要记住一点,每个浏览器拿到的都是不同的session对象。

创建session

会话技术 - 图1

本质:请求头中是否有Cookie:JSESSIONID=XXXX

第一次访问,请求头中肯定没有Cookie:JSESSIONID=xxxx,所以执行到request.getSession的时候,那么就会创建一个session对象,同时以set-Cookie:JSESSIONID=xxxx将session的id返回给浏览器,浏览器会将id保存下来

第二次访问,请求头中会携带Cookie:JSESSIONID=xxxx,所以执行到request.getSession的时候,(根据session的id去找到对应的session对象),就i把当前的session对象返回。

关闭服务器,session对象会销毁吗

肯定会。

关闭浏览器,session对象会销毁吗

没有销毁。此时session对象类似不可达。不可达的原因在于cookie请求头默认情况下只存在浏览器的内存中,关闭浏览器则失效,

JSESSIONID就会丢失。

session的使用

存取数据。

session域。

关闭服务器,重启服务器,还可以访问到原先session中的数据吗

注意:不要使用idea的tomcat关闭来验证,否则你将得不到正确的结论。

还可以进一步验证,比如使用本地的tomcat来部署我们的应用。

需要使用tomcat自带的管理系统来将应用给卸载。

在本地安装的tomcat conf/tomcat-users.xml文件中增加如下设置

  1. package com.cskaoyan.session;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8. import java.io.IOException;
  9. @WebServlet("/session1")
  10. public class SessionServlet extends HttpServlet {
  11. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  12. }
  13. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14. //如何去创建一个session呢?
  15. HttpSession session = request.getSession();
  16. session.setAttribute("username", "zhangsan");
  17. System.out.println(session.getId());
  18. System.out.println(session);
  19. }
  20. }
  1. package com.cskaoyan.session;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8. import java.io.IOException;
  9. @WebServlet("/session2")
  10. public class SessionServlet2 extends HttpServlet {
  11. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  12. }
  13. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14. //如何去创建一个session呢?
  15. HttpSession session = request.getSession();
  16. String username = (String) session.getAttribute("username");
  17. System.out.println(session.getId());
  18. System.out.println(session);
  19. System.out.println(username);
  20. }
  21. }

会话技术 - 图2

最终session里面的数据还是存在的,还可以访问到。session的地址发生了变化,但是session的id没有变。

秽土转生。

session在临死之前,它会序列化到本地硬盘上面(把里面的id、attributes属性全部序列化到本地硬盘上面),形成一个文件,当应用重新被启动的时候,读取文件,将里面的数据重新读取到内存中,会创建新的session,将这些id、属性值全部赋值给新的session对象。

关闭浏览器,如果我希望还可以访问到原先session中的数据,应该怎么办

  1. package com.cskaoyan.session;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.*;
  5. import java.io.IOException;
  6. @WebServlet("/session3")
  7. public class SessionServlet3 extends HttpServlet {
  8. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  9. }
  10. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  11. HttpSession session = request.getSession();
  12. session.setAttribute("username", "sssss");
  13. Cookie cookie = new Cookie("JSESSIONID", session.getId());
  14. cookie.setMaxAge(180);
  15. response.addCookie(cookie);
  16. }
  17. }
  1. package com.cskaoyan.session;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8. import java.io.IOException;
  9. @WebServlet("/session4")
  10. public class SessionServlet4 extends HttpServlet {
  11. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  12. }
  13. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14. HttpSession session = request.getSession();
  15. String username = (String) session.getAttribute("username");
  16. System.out.println(username);
  17. }
  18. }

session域

Context、Request、Session

Context > Session > Request

使用:

Context域:范围最大的一个域。和用户无关的。所以可以用来存放一些和用户无关的,所有用户都相同的数据,比如商城的商品分类

Session域:可以用来存放用户相关的数据。比如用户的购物车数据、用户的浏览记录、用户名等

Request域:非常的小,如果希望在一次请求中用到该数据,那么就可以使用request域,让转发的两个组件之间进行共享该数据。

session的生命周期

创建:对象的创建(request.getSession())

销毁:对象的销毁(应用被卸载、服务器关闭)

数据赋值:setAttribute、getAttribute、removeAttribute

对象的销毁并不意味着数据的销毁,如果希望数据销毁只有两种方式:

1.主动调用session.invalidate();

2.session默认有效期到达,session也会失效 如果30min内没有访问当前session,那么session失效,如果在30min内访问了一次session,那么重新开始计时

  1. package com.cskaoyan.session;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8. @WebServlet("/login")
  9. public class LoginServlet extends HttpServlet {
  10. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  11. response.setContentType("text/html;charset=utf-8");
  12. String username = request.getParameter("username");
  13. String password = request.getParameter("password");
  14. request.getSession().setAttribute("username", username);
  15. response.getWriter().println("登录成功,跳转中......");
  16. response.setHeader("refresh", "2;url=" + request.getContextPath() + "/info");
  17. }
  18. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  19. }
  20. }
  1. package com.cskaoyan.session;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8. @WebServlet("/info")
  9. public class InfoServlet extends HttpServlet {
  10. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  11. }
  12. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  13. response.setContentType("text/html;charset=utf-8");
  14. String username = (String) request.getSession().getAttribute("username");
  15. response.getWriter().println("欢迎您 " + username + "<a href='" + request.getContextPath() + "/logout" + "'>点我注销</a>");
  16. }
  17. }
  1. package com.cskaoyan.session;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8. @WebServlet("/logout")
  9. public class LogoutServlet extends HttpServlet {
  10. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  11. }
  12. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  13. //注销的逻辑应该怎么写?
  14. request.getSession().invalidate();
  15. }
  16. }

重构到一个servlet中。因为我们发现这些servlet都是在说用户相关,所以可以放到一个servlet中。

  1. package com.cskaoyan.session;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8. @WebServlet("/user")
  9. public class UserServlet extends HttpServlet {
  10. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  11. String op = request.getParameter("op");
  12. if("login".equals(op)){
  13. login(request, response);
  14. }
  15. }
  16. private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
  17. response.setContentType("text/html;charset=utf-8");
  18. String username = request.getParameter("username");
  19. String password = request.getParameter("password");
  20. request.getSession().setAttribute("username", username);
  21. response.getWriter().println("登录成功,跳转中......");
  22. response.setHeader("refresh", "2;url=" + request.getContextPath() + "/user?op=info");
  23. }
  24. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  25. String op = request.getParameter("op");
  26. if("info".equals(op)){
  27. info(request, response);
  28. }else if("logout".equals(op)){
  29. logout(request, response);
  30. }
  31. }
  32. private void logout(HttpServletRequest request, HttpServletResponse response) {
  33. request.getSession().invalidate();
  34. //跳转
  35. }
  36. private void info(HttpServletRequest request, HttpServletResponse response) throws IOException {
  37. response.setContentType("text/html;charset=utf-8");
  38. String username = (String) request.getSession().getAttribute("username");
  39. response.getWriter().println("欢迎您 " + username + "<a href='" + request.getContextPath() + "/user?op=logout" + "'>点我注销</a>");
  40. }
  41. }

购物车案例

  1. package com.cskaoyan.cart;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. @WebServlet(value = "/index",loadOnStartup = 1)
  11. public class IndexServlet extends HttpServlet {
  12. @Override
  13. public void init() throws ServletException {
  14. //初始化商品信息
  15. Product iphone12 = new Product("1", "iphone12", 6999.0);
  16. Product mate40 = new Product("2", "huawei mate40", 6699.0);
  17. Product product = new Product("3", "mi 11 pro", 5999.0);
  18. List<Product> products = new ArrayList<>();
  19. products.add(iphone12);
  20. products.add(mate40);
  21. products.add(product);
  22. getServletContext().setAttribute("products", products);
  23. }
  24. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  25. }
  26. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  27. //初始化商品信息
  28. List<Product> products = (List<Product>) getServletContext().getAttribute("products");
  29. for (Product product : products) {
  30. response.getWriter().println("<a href='" + request.getContextPath() + "/detail?id=" + product.getId() + "'>" + product.getName() +"</a>");
  31. }
  32. }
  33. }
  1. package com.cskaoyan.cart;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8. import java.util.List;
  9. @WebServlet("/detail")
  10. public class DetailServlet extends HttpServlet {
  11. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  12. }
  13. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14. response.setContentType("text/html;charset=utf-8");
  15. String id = request.getParameter("id");
  16. if(id == null || id.isEmpty()){
  17. response.getWriter().println("invalid parameter");
  18. return;
  19. }
  20. response.getWriter().println("<!DOCTYPE html>\n" +
  21. "<html lang=\"en\">\n" +
  22. "<head>\n" +
  23. " <meta charset=\"UTF-8\">\n" +
  24. " <title>Title</title>\n" +
  25. "</head>\n" +
  26. "<body>");
  27. List<Product> products = (List<Product>) getServletContext().getAttribute("products");
  28. for (Product product : products) {
  29. if(product.getId().equals(id)){
  30. response.getWriter().println(product);
  31. }
  32. }
  33. String index = request.getContextPath() + "/index";
  34. String addCart = request.getContextPath() + "/addCart?id=" + id;
  35. String viewCart = request.getContextPath() + "/viewCart";
  36. response.getWriter().println("<a href='" + index + "'>返回首页</a>");
  37. response.getWriter().println("<a href='" + addCart + "'>加入购物车</a>");
  38. response.getWriter().println("<a href='" + viewCart + "'>查看购物车</a>");
  39. response.getWriter().println("</body>\n" +
  40. "</html>");
  41. }
  42. }
  1. package com.cskaoyan.cart;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8. import java.io.IOException;
  9. import java.util.List;
  10. @WebServlet("/viewCart")
  11. public class ViewCartServlet extends HttpServlet {
  12. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  13. }
  14. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  15. response.setContentType("text/html;charset=utf-8");
  16. HttpSession session = request.getSession();
  17. List<String> cart = (List<String>) session.getAttribute("cart");
  18. if(cart == null){
  19. response.getWriter().println("购物车为空,去选购吧.....");
  20. String index = request.getContextPath() + "/index";
  21. response.setHeader("refresh", "2;url=" + index);
  22. return;
  23. }
  24. List<Product> products = (List<Product>) getServletContext().getAttribute("products");
  25. for (Product product : products) {
  26. for (String id : cart) {
  27. if(id.equals(product.getId())){
  28. response.getWriter().println(product);
  29. }
  30. }
  31. }
  32. String index = request.getContextPath() + "/index";
  33. response.getWriter().println("<a href='" + index + "'>返回首页</a>");
  34. }
  35. }
  1. package com.cskaoyan.cart;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import javax.servlet.http.HttpSession;
  8. import java.io.IOException;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. @WebServlet("/addCart")
  12. public class AddCartServlet extends HttpServlet {
  13. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  14. }
  15. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  16. response.setContentType("text/html;charset=utf-8");
  17. String id = request.getParameter("id");
  18. if(id == null || id.isEmpty()){
  19. response.getWriter().println("invalid parameter");
  20. return;
  21. }
  22. HttpSession session = request.getSession();
  23. List<String> cart = (List<String>) session.getAttribute("cart");
  24. if(cart == null){
  25. cart = new ArrayList<>();
  26. session.setAttribute("cart", cart);
  27. }
  28. cart.add(id);
  29. response.getWriter().println("添加到购物车成功....");
  30. String index = request.getContextPath() + "/index";
  31. response.setHeader("refresh", "2;url=" + index);
  32. // List<String> list = new ArrayList<>();
  33. // list.add(id);
  34. //不能用set,因为永远只有一个 session.setAttribute("id", id);
  35. }
  36. }
  1. package com.cskaoyan.cart;
  2. public class Product {
  3. private String id;
  4. private String name;
  5. private Double price;
  6. public String getId() {
  7. return id;
  8. }
  9. public void setId(String id) {
  10. this.id = id;
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public Double getPrice() {
  19. return price;
  20. }
  21. public void setPrice(Double price) {
  22. this.price = price;
  23. }
  24. public Product(String id, String name, Double price) {
  25. this.id = id;
  26. this.name = name;
  27. this.price = price;
  28. }
  29. public Product() {
  30. }
  31. @Override
  32. public String toString() {
  33. return "Product{" +
  34. "id='" + id + '\'' +
  35. ", name='" + name + '\'' +
  36. ", price=" + price +
  37. '}';
  38. }
  39. }

session依赖于cookie,如果cookie禁用会怎么样

EE规范提供了一个URL重写方法,此时JSESSIONID会附着在地址栏的后面

今晚可以做的

1.提到了数量问题

2.实现历史足迹。