[Servlet&JSP] Cookie会话管理
web应用程序的请求与响应是基于HTTP的,为无状态的通信协议,服务器不会记得这次请求与下一次请求的关系。,然而有些功能必须由多次请求来完成,例如购物车,用户可能在多个购物网页之间采购商品,web应用程序必须有个方式来得知用户在这些网页中采购了哪些商品,这种记得此次请求与之后请求间关系的方式,就称为会话管理(Session Management)。
使用隐藏字段,可以将上一次请求的参数用隐藏字段的方式返回到下一页窗体中,如此发送下一页窗体时,就可以一并发送这些隐藏字段,每一页的参数就可以保留下来。
使用隐藏字段的方式,在关掉网页之后,就会遗失先前请求的信息,所以这种方式仅适合用于一些简单的状态管理,像是在线问卷。由于在查看页面源代码时,就可以看到隐藏字段的值,因此这个方法不适合于隐秘性较高的数据。隐藏字段不是Servlet/JSP实际会话管理时的机制,这里主要是为了说明由浏览器主动告知必要的信息,为实现web应用程序会话管理的基本原理。
web应用程序会话管理的基本方式,就是在此次请求中,将下一次请求时服务器所应知道的信息先响应给浏览器,由浏览器在之后的请求中再一并发送给应用程序,如此应用程序就可以得知多次请求间的相关资料。
Cookie是在浏览器中保存信息的一种方式,服务器可以响应浏览器set-cookie标头,浏览器收到这个标头与数值后,会将之保存为计算机上的一个文件,这个文件就成为Cookie。可以给Cookie设置一个存活期限,保留一些有用的信息在客户端。如果关闭浏览器之后,再度打开浏览器并连接服务器时Cookie仍在有效期限内,浏览器会使用cookie标头自动将Cookie发送给浏览器,服务器就可以得知先前浏览器请求的相关信息。
Servlet本身体用了创建、设置与读取Cookie的API。如果要创建Cookie,则可以使用Cookie类,创建时指定Cookie中的名称与数值,并使用HttpServletResponse的addCookie()方法在响应中添加Cookie。例如:
HTTP中Cookie的设置时通过set-cookie标头来实现的,所以必须在实际响应浏览器之前只是用addCookie()来添加Cookie实例,在浏览器输出HTML响应之后再执行addCookie()是没有作用的。
可以使用setMaxAge()来设置Cookie的有限期限,设置的单位是秒。默认情况下,关闭浏览器之后Cookie就失效。如果要取得浏览器上保存的Cookie,则可以用从HttpServletRequest的getCookies()来取得,它可以取得该网页所有域(domain)的所有Cookie,所以返回值时Cookie[]数组。取得Cookie对象后,可以使用Cookie的getName()和getValue()方法,分别取得Cookie的名称与数值。例如:
Cookie的一个常见应用是实现用户自动登录功能,若用户勾选自动登录选项,则下次再访问同一网页就不需要再输入用户名和密码,而可以直接登录该网页。主要示例代码如下:
index.java(检查Cookie):
public class Index extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Cookie[] cookies = request.getCookies(); if (cookies != null) { for (int i = 0; i cookies.length; i++) { String name = cookies[i].getName(); String value = cookies[i].getValue(); if ("username".equals(name) "abc".equals(value)) { request.setAttribute(name, value); request.getRequestDispatcher("user.jsp") .forward(request, response); return; response.sendRedirect("login.jsp"); }
User.java(处理表单submit,设置Cookie):
public class User extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); if ("abc".equals(username) "123".equals(password)) { //添加Cookie Cookie cookie = new Cookie("username", username); cookie.setMaxAge(7 * 24 * 60 * 60); response.addCookie(cookie); request.setAttribute("username", username); request.getRequestDispatcher("user.jsp") .forward(request, response); } else { response.sendRedirect("login.jsp"); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); }
login.jsp(登录窗口):
form action="user.do" method="post" username: input type="text" name="username" / br / password: input type="password" name="password" / br / Auto login: input type="checkbox" name="auto" / br / input type="submit" value="Sign in" / /form /body
User.jsp(登录成功后的用户界面):
?xml version="1.0" encoding="UTF-8"? web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1" servlet servlet-name Index /servlet-name servlet-class club.chuxing.Index /servlet-class /servlet servlet servlet-name User /servlet-name servlet-class club.chuxing.User /servlet-class /servlet servlet-mapping servlet-name Index /servlet-name url-pattern /index.do /url-pattern /servlet-mapping servlet-mapping servlet-name User /servlet-name url-pattern /user.do /url-pattern /servlet-mapping /web-app
所谓URL重写(URL Rewriting),其实就是Get请求参数的应用,当服务器响应浏览器的上一次请求时,将某些相关信息以超链接的方式响应给浏览器,超链接中包含请求参数信息。
以下用一个简单的范例模拟搜索分页结果:
Search.java(生成搜索结果编号):
public class Search extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String page = request.getParameter("page"); if (page == null) { page = "1"; int pageNum = Integer.parseInt(page); int begin = 10 * pageNum - 9; int[] indices = new int[10]; for (int i = 0; i 10; i++) { indices[i] = begin + i; request.setAttribute("indices", indices); request.getRequestDispatcher("result.jsp") .forward(request, response); }
result.jsp:
%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"% %@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" % !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=utf-8" title Insert title here /title /head body 第${indices[0]}到第${indices[9]}条搜索结果 br/ c:forEach var="index" items="${indices}" li 搜索结果${index} /li /c:forEach /ul c:forEach var="index" items="${indices}" a href="search.do?page=${index%10==0?10:index%10}" ${index%10==0?10:index%10} /a /c:forEach /body /html
效果图:
URL重写是在超链接之后以附加信息的方式进行的,所以必须以GET方式发送请求,再加上GET本身可以携带的请求参数长度有限,因此,大量的客户端信息保留并不适合使用URL重写。通常URL重写用于一些简单的客户端信息保留,或者是辅助会话管理之中。
Servlet_输出流jsp注意事项 订阅专栏 Servlet输出流两种方法以及注意事项 HTTP 响应码 以及 各自对应解释 关于 Web API 技术标准的开发文档网站(https://developer.mozilla.org)
相关文章
- Servlet & JSP系列文章总结
- [Servlet&JSP] 表达式语言EL
- [Servlet&JSP] 部署描述设置
- [Servlet&JSP] 监听器的使用
- [Servlet&JSP] HttpServletRequest的使用
- [HTML 5] Understanding DOM loading event & 'async', 'defer' keyword
- [Servlet&JSP] 封装器的使用
- [Servlet&JSP] 初识ServletContext
- [Servlet&JSP] 初识ServletConfig
- 华为OD机试 - 字符串匹配(Java & JS & Python)
- 华为OD机试 - 按索引范围翻转文章片段(Java & JS & Python)
- ML之LiR&DNN&EL:基于skflow的LiR、DNN、sklearn的RF对Boston(波士顿房价)数据集进行回归预测(房价)
- 【大数据&AI人工智能】变革人类社会的第四次工业革命——AI人工智能革命已到来
- TCP回放攻击 & DDoS脉冲攻击Hit and Run IoT僵尸网络 在DDoS攻击黑产领域最活跃
- 6-3Table API & SQL简介
- [Git & GitHub] eclipse中使用Egit 撤销commit提交