一.用Cookie进行Session管理
Cookie是一段键/值文本信息,可以存放在浏览器的内存中或者是硬盘上,Cookie不仅仅用于发送给浏览器,服务器上的JSP/Servlet之间也可以传递Cookie。
下面的例子是服务器上Cookie的使用
login.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <form action="LoginServlet" method="post"> <input type="text" name="name" /> <input type="submit" value="Login"> </form> </body> </html>
@WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); Cookie cookie = new Cookie("name",name); //设置过期时间,以秒为单位 cookie.setMaxAge(3000); response.addCookie(cookie); response.sendRedirect("success.jsp"); } }success.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% String name = ""; Cookie[] cookies = request.getCookies(); if (cookies != null) { for(Cookie cookie : cookies){ if (cookie.getName().equals("name")) { name = cookie.getValue(); } } } %> <h3>Hi <%=name %>, Login successful.</h3> </body> </html>
LoginServlet中根据表单的内容创建一个cookie对象,然后再跳转到success.jsp,success.jsp中遍历所有的cookie,获得LoginServlet传入的cookie对象。Cookie的存活期限取决于setMaxAge传入的参数,负数则浏览器关闭后删除cookie,只在浏览器内存中保存。0则是删除这个cookie。
在浏览器中输入longin.jsp地址,输入内容后点击按钮,跳到success.jsp。这时复制success.jsp的连接,关闭浏览器,再次打开,直接进入success.jsp以后,发现上次输入的用户名显示在页面上。
login.jsp输入:
当用户第一次访问一个网站时servlet容器自动创建HttpSession对象,通过getSession方法可以获得当前用户的HttpSession。
Servlet容器为它创建的每一个HttpSession生成一个唯一的标识符,并且将它返回浏览器,这个标识符一般是一个名为JSESSIONID的Cookie,浏览器保存这个cookie,就是持久型cookie,在用户下一次访问网站时,服务器端根据浏览器发送的这个Cookie找到对应的HttpSession对象,取出里面保存的对象,以完成会话管理,这种技术可以用于自动登录等用途。HttpSession中的值是保存在服务器内存中的,所以要避免将太大的对象放在里面。
下面的例子是服务器上的HttpSession和客户端的Cookie配合进行会话管理。稍微改动一下上面的程序:
LoginServlet.java:
@WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); HttpSession session = request.getSession(); session.setAttribute("name", name); System.out.println("session.getId():" + session.getId()); response.sendRedirect("success.jsp"); } }success.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% String name = ""; name = request.getSession().getAttribute("name") == null?"":request.getSession().getAttribute("name").toString(); Cookie[] cookies = request.getCookies(); if (cookies != null) { for(Cookie cookie : cookies){ System.out.println(cookie.getName() + " : " + cookie.getValue()); } } %> <h3>Hi <%=name %>, Login successful.</h3> </body> </html>页面输出和上面一样,注意后台打印的值:
session.getId():BA956325800E90C5F7A6FBCA71B53733
JSESSIONID : BA956325800E90C5F7A6FBCA71B53733
session.getId的值和Cookie中的JSESSIONID是一样的。会话跟踪时就是根据这个值来匹配HttpSession对象的。