HttpSeisson:
用户端(浏览器)在对服务端发起请求时,服务器都会为每个用户端创建一个独立的Session对象。
Session生命周期:
会话不结束(浏览器未关闭)。
默认生存时间为30分钟。
Session运行原理:
1.用户第一次访问Servlet时,服务器会为其创建一个带有SessionId的独立Session对象。
2.响应时,SessionId会被装进Cookie中,进而被保存到浏览器中。
3.用户再次访问Servlet时,Cookie中的SessionId将会被服务器拿去查询是否存有对应Session对象。
测试Session运行原理:
获取Session对象并添加值:
public class Demo01 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
HttpSession session = request.getSession();
session.setAttribute("username", username);
System.out.println(session.getId());
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
获取Session域数据:
public class Demo02 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
HttpSession session = request.getSession();
String username = (String)session.getAttribute("username");
response.getWriter().write(username + " " + session.getId());
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
域对象作用范围:
Context域:作用于某一个项目的整个工程。
Session域:一个会话的整个过程中。
Request域:只作用于一次请求中。
Context域 > Session域 > Request域
Session域的钝化与活化:
钝化:服务器停止时,系统将Session中实现Serializable(序列化接口)的对象保存为服务器工作空间及work文件夹下文件的过程。
活化:服务器重启时,把work文件夹下钝化的文件读回Session的过程。
简单实现购物车添加及查看功能:
1.存储商品信息的类:包含编号和商品名两个属性,为防止服务器突然停止,丢失Session中的状态,商品类实现序列化。
2.获取商品的工具类(模拟假数据):类有存放商品的容器、获取容器的方法、商品编号获取商品的方法。
3.商品展示的Servlet类:获取商品信息类。
4.添加购物车的Servlet类:根据展示Servlet类,传入商品编号,获取商品存入新容器,并将新容器添加到Session中。
5.显示购物车信息的Servlet类:获取Session中存入容器信息,对容器进行遍历输出显示。
商品信息类:
public class Book implements Serializable {
/**
* 序列化
*/
private static final long serialVersionUID = 1L;
private String id;
private String name;
public Book() {
super();
}
public Book(String id, String name) {
super();
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;
}
@Override
public String toString() {
return "编号:" + id + ", 书名:" + name;
}
}
获取商品的工具类:
public class BookUtil {
private static HashMap books = new HashMap<>();
static {
books.put("1", new Book("1", "葵花宝典"));
books.put("2", new Book("2", "九阴真经"));
books.put("3", new Book("3", "辟邪剑谱"));
books.put("4", new Book("4", "九阳神功"));
}
public static HashMap getBooks() {
return books;
}
public static Book findBookById(String id) {
return books.get(id);
}
}
商品展示Servlet类:
public class ShowAllBooks extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String path = request.getContextPath();
HashMap books = BookUtil.getBooks();
for (String key : books.keySet()) {
Book book = books.get(key);
out.write(""+ book.getName() +"
");
}
out.write("查看购物车");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
添加购物车Servlet类:
public class AddBook extends HttpServlet {
@SuppressWarnings("unchecked")
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
Book book = BookUtil.findBookById(id);
HttpSession session = request.getSession();
ArrayList car = (ArrayList)session.getAttribute("car");
if (car == null) {
car = new ArrayList<>();
}
car.add(book);
session.setAttribute("car", car);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
显示购物车Servlet类:
public class ShowCar extends HttpServlet {
@SuppressWarnings("unchecked")
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
ArrayList car = (ArrayList)session.getAttribute("car");
if (car != null) {
for (Book book : car) {
out.write(book + "
");
}
} else {
out.write("购物车是空的");
response.setHeader("refresh", "3;url=" + request.getContextPath() + "/showAllBooks");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
人类与计算机的图灵测试(验证码):
导入jar包:ValidateCode.jar
public class CodeServlert extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ValidateCode vCode = new ValidateCode(100, 25, 4, 9);
String code = vCode.getCode();
request.getSession().setAttribute("wcode", code);
vCode.write(response.getOutputStream());
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}