一、会话跟踪:
不管操作多少功能,都是与当前登录用户相关的信息,当前的登录用户始终没有改变,也就是用户名和密码都没有丢失。但HTTP协议是一个无状态的协议,当一个客户向服务器发出请求(request),在服务器返回响应(response)后,连接就关闭了。这时,在服务器端不保留连接相关的信息,因此当下一次请求连接时,服务器已没有以前连接的信息了,也就无法判断这一次连接和以前的连接是否属于同一客户,也就是说Web服务器无法跟踪客户状态。在 Servlet 规范中,常用以下两种机制完成会话跟踪:
a)使用持续的Cookie
b)使用Session(会话)机制
二、Cookie:Cookie以“名-值”对的形式保存数据
1.创建Cookie对象: new Cookie(name,value);
2.设置最大时效 :setMaxAge(int age);
3.将Cookie放入到HTTP响应报头: addCookie(Cookie cookie);
【注意】如果创建了一个cookie,并将它发送到浏览器,默认情况下它是一个会话级别的cookie,他将被存储在浏览器的内存中,用户退出浏览器之后被删除。若希望浏览器将该cookie存储在磁盘上,则需要使用setMaxAge,并给出一个以秒为单位的时间。将最大时效设为0则是命令浏览器删除该cookie。
1)、将数据写入Cookie中:
1 package com.st.user; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.http.Cookie; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 @SuppressWarnings("serial") 12 public class UserLoginTest extends HttpServlet{ 13 @Override 14 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 15 throws ServletException, IOException { 16 // TODO Auto-generated method stub 17 } 18 @Override 19 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 20 throws ServletException, IOException { 21 // TODO Auto-generated method stub 22 req.setCharacterEncoding("UTF-8"); //post乱码专用解决方式 23 String userName = req.getParameter("userName"); 24 String passWord = req.getParameter("passWord"); 25 System.out.println(userName); 26 System.out.println(passWord); 27 Cookie cookieByName = new Cookie("userName",userName); 28 Cookie cookieByPassWord = new Cookie("passWord", passWord); 29 /*cookieByName.setMaxAge(24 * 60 * 60); //设置在硬盘上保存的时间,以秒为单位。 30 cookieByPassWord.setMaxAge(24 * 60 * 60);因为保存在硬盘上,所以即便是服务器重启了依然可以访问*/ 31 resp.addCookie(cookieByName); 32 resp.addCookie(cookieByPassWord); 33 //转发 34 // req.getRequestDispatcher("/home.jsp").forward(req, resp); 35 //重定向 36 resp.sendRedirect("home.jsp"); 37 } 38 }
2)、读取cookie中的值:
1 package com.st.user; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.Cookie; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest; 10 import javax.servlet.http.HttpServletResponse; 11 12 @SuppressWarnings("serial") 13 public class BookServletTest extends HttpServlet { 14 15 public void doGet(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 } 18 public void doPost(HttpServletRequest request, HttpServletResponse response) 19 throws ServletException, IOException { 20 request.setCharacterEncoding("UTF-8"); 21 System.out.println(request.getParameter("bookName")); 22 Cookie cookie []= request.getCookies(); //返回的是cookie的一个数组 23 for(Cookie c: cookie){ 24 if("userName".equals(c.getName())) 25 System.out.println("userName="+c.getValue()); 26 else if("passWord".equals(c.getName())) 27 System.out.println("password="+c.getValue()); 28 } 29 response.setContentType("text/html"); 30 PrintWriter out = response.getWriter(); 31 out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); 32 out.println("<HTML>"); 33 out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); 34 out.println(" <BODY>"); 35 out.print(" This is "); 36 out.print(this.getClass()); 37 out.println(", using the POST method"); 38 out.println(" </BODY>"); 39 out.println("</HTML>"); 40 out.flush(); 41 out.close(); 42 } 43 }
时间: 2024-12-29 07:06:41