会话概述:
在日常生活中,从拨通电话到挂断电话之间的一连串的你问我答的过程就是一个会话。在web应用中即是指一个客户端与web服务器之间连续发生的一系列请求和响应过程。在Servlet技术中,为其提供了Cookie和Session用于保存会话数据的对象。
Cookie会话技术:
通俗来说,它相当于日常生活中的会员卡,当用户通过浏览器访问服务器时,它会将服务器给客户端发送的信息,备份保存在其中,当下次再度使用该浏览器访问时,都会在请求中将Cookie发送给服务器,方便服务器对浏览器做出正确的反应。
Cookie在浏览器和服务器之间传输的过程:
Cookie类的常用方法:
Session会话技术:
通俗来说,它相当于医院的就诊卡。当客户端后续访问服务器时,,只要将标识号传递给服务器,服务器就能判断出该请求是哪个客户端发送的,从而选择与之对应的Session对象为其服务。
Session保存用户信息的过程:
HttpSession接口中的常用方法:
Session超时管理:
session对象是有生命时长的,它的默认存活时间是30分钟。除了时间超时外,还可以通过invalidate()方法强制使会话失效。
package session;
public class Book {
private String id;
private String name;
public Book() {
}
public Book(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package session;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
public class BookDB {
private static Map<String,Book> map = new LinkedHashMap<String,Book>();
static{
map.put("1", new Book("1","javaWeb"));
map.put("2", new Book("2","jdbc入门"));
map.put("3", new Book("3","java基础"));
map.put("4", new Book("4","struts框架"));
map.put("5", new Book("5","hibernate框架"));
}
//1.得到所有图书
public static Collection<Book> getAll(){
return map.values();
}
//2.根据map的key,也就是图书的id,得到某本图书
public static Book getBook(String id){
return map.get(id);
}
}
package session;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "Servlet3", value = "/Servlet3")
public class Servlet3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
// Collection<Book> books=BookDB.getAll();
Collection<Book> books=BookDB.getAll();
PrintWriter out=response.getWriter();
out.print("20200806050032 官联川"+"<br/>");
out.print("本网站售卖的书籍如下");
for (Book b:books){
String name = b.getName();
String id = b.getId();
String url="<a href='/day6_cookie_war_exploded/Servlet4?id="+id+"'>点击购买<a/>";
out.print("图书名称:"+name+" "+url+"<br/><br/>");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
package session;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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;
@WebServlet(name = "Servlet4", value = "/Servlet4")
public class Servlet4 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String id = request.getParameter("id");
if (id==null){
response.sendRedirect("/chapter05/Servlet");
return;
}
session.Book book =BookDB.getBook(id);
// Book book = BookDB.getBook(id);
//用session来实现购物车
HttpSession session = request.getSession();
List<session.Book> list=(List<session.Book>) session.getAttribute("cart");
// List<Book> list=(List<Book>) session.getAttribute("cart");
if (list==null){
list=new ArrayList<Book>();
session.setAttribute("cart",list);
}
// if (list=null){
// list=new ArrayList<Book>();
// session.setAttribute("cart",list);
//
// }
list.add(book);
Cookie cookie = new Cookie("JESSIONID",session.getId());
response.addCookie(cookie);
response.sendRedirect("/chapter05/Servlet");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
package session;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
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;
@WebServlet(name = "Servlet5", value = "/Servlet5")
public class Cart extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
List<Book> cart=null;
HttpSession session= request.getSession(false);
boolean cartflag=true;
if (session==null){
cartflag=false;
}else {
cart=(List<Book>) session.getAttribute("cart");
if (cart==null){
cartflag=false;
}
}
if (!cartflag){
out.print("对不起,请前往购买图书");
}else {
out.print("购买图书如下"+"<br/>");
for (session.Book b:cart){
out.print("购买图书:"+b.getName()+"<br/>");
}
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
注意,若购物车功能无法实现,请检查浏览器Cookie是否正常。