介绍
- Servlet规范中引入了一种补充的会话管理机制,它允许不支持Cookie的浏览器也可以与WEB服务器保持连续的会话。这种补充机制要求在响应消息的实体内容中必须包含下一次请求的超链接,并将会话标识号作为超链接的URL地址的一个特殊参数。
将会话标识号以参数形式附加在超链接的URL地址后面的技术称为URL重写。如果在浏览器不支持Cookie或者关闭了Cookie功能的情况下,WEB服务器还要能够与浏览器实现有状态的会话,就必须对所有可能被客户端访问的请求路径(包括超链接、form表单的action属性设置和重定向的URL)进行URL重写。
HttpServletResponse接口中定义了两个用于完成URL重写方法:
- encodeURL方法
- encodeRedirectURL方法
测试代码
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<fieldset>
<marquee>
会话ID :
<%= session.getId() %>
<br /> 是新会话? :
<%= session.isNew() %>
<br /> 最大内部闲置:
<%= session.getMaxInactiveInterval() %>
<br /> 创建时间 :
<%= session.getCreationTime() %>
<br /> 最后一次访问时间 :
<%= session.getLastAccessedTime() %>
</marquee>
<%
//2.获取重新登录的username
Object username = session.getAttribute("username") == null ? "" : session.getAttribute("username");
%>
<!-- url重写HttpSession:jsessionid回去以URL一部分的形式附带在url上 -->
<!-- hello.jsp;jsessionid=22A10B2F1E451F767E5D4020A374B7FE -->
<form action="<%=response.encodeUrl("hello.jsp") %>" method="post">
<table>
<tr>
<td><label for="userID">用户名:</label></td>
<!-- 已经登录过,就显示出来 -->
<td><input type="text" name="username" value="<%= username%>"
id="userID" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="登录" /></td>
</tr>
</table>
</form>
</fieldset>
</body>
</html>
hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<fieldset>
<marquee behavior="alternate">
会话ID : <%= session.getId() %>
<br />
是新会话? : <%= session.isNew() %>
<br />
最大内部闲置: <%= session.getMaxInactiveInterval() %>
<br />
创建时间 : <%= session.getCreationTime() %>
<br />
最后一次访问时间 : <%= session.getLastAccessedTime() %>
</marquee>
<details>
<summary>你好!</summary>
<%= request.getParameter("username") %>
</details>
<%
//1.设置会话的属性值,想在重新登录的页面能直接显示上一次的登录名
session.setAttribute("username", request.getParameter("username"));
%>
<a href="<%=response.encodeUrl("index.jsp")%>"> 重新登录 </a>
<a href="<%= response.encodeUrl("logout.jsp")%>">注销</a>
</fieldset>
</body>
</html>
logout.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<fieldset>
<marquee behavior="alternate">
会话ID : <%= session.getId() %>
<br />
是新会话? : <%= session.isNew() %>
<br />
最大内部闲置: <%= session.getMaxInactiveInterval() %>
<br />
创建时间 : <%= session.getCreationTime() %>
<br />
最后一次访问时间 : <%= session.getLastAccessedTime() %>
</marquee>
<details>
<summary>再见</summary>
<%= session.getAttribute("username") %>
</details>
<%
//销毁session
session.invalidate();
%>
</fieldset>
</body>
</html>
其它
- 源码下载
关注下方公众号,回复:javaweb_course.code
欢迎加入交流群:451826376
更多信息:www.itcourse.top