HttpSession 服务端的技术
服务器会为每一个用户 创建一个独立的HttpSession
HttpSession原理
当用户第一次访问Servlet时,服务器端会给用户创建一个独立的Session
并且生成一个SessionID,这个SessionID在响应浏览器的时候会被装进cookie中,从而被保存到浏览器中
当用户再一次访问Servlet时,请求中会携带着cookie中的SessionID去访问
服务器会根据这个SessionID去查看是否有对应的Session对象
有就拿出来使用;没有就创建一个Session(相当于用户第一次访问)
域的范围:
Context域 > Session域 > Request域
Session域 只要会话不结束就会存在 但是Session有默认的存活时间(30分钟)
使用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");
// 获取session对象
HttpSession session = request.getSession();
// 保存数据
session.setAttribute("username", username);
// 打印SessionID
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);
}
}
Session
钝化:服务器停止时,如果你的Session中保存了对象 并且该对象实现了序列化接口Serializable
系统会把你保存的Session序列化到 服务器的work文件夹你的工程下的一个文件
活化:当你重新启动了服务器 系统会重新把钝化下来的那个文件加载回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;
}
// 按id查找对应的书
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();
// 获取全部图书
HashMap books = BookUtil.getBooks();
// 遍历
Set keySet = books.keySet();
for (String key : keySet) {
Book book = books.get(key);
out.write(""+book.getName()+"
");
}
// 添加查看购物车的链接
out.write("查看购物车");
// 设置session有效时间
// request.getSession().setMaxInactiveInterval(5);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
添加图书(Servlet)
public class AddBook extends HttpServlet {
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);
// 创建一个容器(相当于购物车 保存买的书)
// 两种情况 1.已经存过书了 2.没存过
HttpSession session = request.getSession();
ArrayList car = (ArrayList)session.getAttribute("car");
if (car == null) {
// 相当于第一次访问 创建容器
car = new ArrayList<>();
}
// 把书添加到容器中
car.add(book);
// 把这个容器添加到session中
session.setAttribute("car", car);
// 显示添加成功
response.getWriter().write("添加成功");
// 一秒后返回商品列表页
response.setHeader("refresh", "1;url=" + request.getContextPath() + "/showAllBooks");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
查看购物车(Servlet)
public class ShowCar 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();
HttpSession session = request.getSession();
ArrayList car = (ArrayList)session.getAttribute("car");
if (car != null) {
// 遍历输出
for (Book book : car) {
out.write(book.getName() + "
");
}
} else {
out.write("购物车是空的");
// 两秒跳转图书列表
response.setHeader("refresh", "2;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 {
// 使用jar生成验证码
ValidateCode vCode = new ValidateCode(100, 25, 4, 9);
// 通过响应中字节流写回网页
vCode.write(response.getOutputStream());
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}