1.会话管理基本原理
1.隐藏域
将表单中的内容在显示页面时隐藏,不显示数据,在JSP 中将input标签type设置为hidden 生成一个隐藏表单域。将会话的唯一标识记录到隐藏域中的value值中,并设定name值。提交给服务器之后,服务器会根据根据会话标识找到会话对象。
缺点:实现比较麻烦,安全性差,不适合隐秘性的数据
2.cookie
服务端保存会话对象中设定会话的唯一标识,客户端将会话标识存在cookie中,当浏览器发送请求时从cookie取得会话标识发给服务端。
3.URL重写
在URL地址末尾添加会话标识,改写原先的URL地址,唯一标识会话的信息以参数的形式添加到URL中,主要使用在浏览器cookie被禁用的情况下。
缺点:整个Web应用中,超链接或者脚本中用到URL需添加会话标识,Web应用中每个页面都需要动态生成,当客户端访问静态页面时,会话标识会丢失,重回动态页面时不能继续此前会话。
禁用cookie
在Web项目中禁用 打开或者创建context.xml文件
<?xml version="1.0" encoding = "utf-8"?>
<Context cookies = "false" path = "/ch06">//这里是对单个项目的Cookie禁用
</Context>
HttpSession会话管理
每个请求者对应一个Session对象,客户端所有状态信息都保存在该对象里,第一次请求服务器时创建Session对象。
对于Servlet中
HttpSession session = request.getSession();//获取session对象
session.setAttribute("","");//设置session中的属性
对于JSP中内置Session对象
HttpSession的生命周期
1.HttpSession对象的创建
客户端第一次访问服务器时,服务器为每个浏览器创建不同的SessionID值
2.HttpSession对象的使用
在创建HttpSession对象后,使用Session对象进行数据的存取和传输
(1).将创建的SessionID值存到cookie中
(2).当客户端再次发起请求时,会将SessionID与request一起发送给服务器
(3).服务器根据请求过来的SessionID与保存在服务器端的Session对应起来
3.HttpSession对象的消亡
(1).将浏览器关闭
(2).调用HttpSession的invalidate()方法
(3).Session超时
HttpSession的有效期
为Session设定一个有效器,当某用户访问session超过这个有效期那么session失效,将它从内存清除。
(只有访问JSP,Servlet时才会创建session,访问静态页面时是不会创建session对象)
设置有效期:
1.在对应的Web服务器配置中设置所有session的有效期
2.调用session中的setMaxInactiveInterval(long interval)进行设定
在web.xml中修改
<session-config>
<!--会话时长为30分钟-->
<session-timeout>30</session-timeout>
</session-config>
HttpSession会话管理实例演示
jsp源代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
request.setCharacterEncoding("utf-8");
String message = "";
String flag = request.getParameter("flag");
if ((flag!=null)&&(flag.equals("small"))){
message = "太小了";
}else if((flag!=null)&&(flag.equals("big"))){
message = "太大了";
}else if ((flag!=null)&&(flag.equals("success"))){
message = "猜对了";
}
%>
<html>
<head>
<title>猜数字</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/test" method="post">
请输入你的数字:<input type="text" name="number"/>
<span style="color: red"><%=message%></span>
<input type="submit" value="提交">
</form>
</body>
</html>
.java源码
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
request.setCharacterEncoding("utf-8");
String gussnumber = request.getParameter("number");
int number = Integer.parseInt(gussnumber);
HttpSession session = request.getSession();
Integer currnumber = (Integer) session.getAttribute("currnumber");
if(currnumber ==null){
currnumber = 1+(int)(Math.random()*50);
session.setAttribute("currnumber",currnumber);
}
if(number>currnumber){
RequestDispatcher re = request.getRequestDispatcher("/until6HttpSession随机数.jsp?flag=big");
re.forward(request,response);
}else if(number<currnumber){
RequestDispatcher re = request.getRequestDispatcher("/until6HttpSession随机数.jsp?flag=small");
re.forward(request,response);
}else {
RequestDispatcher re = request.getRequestDispatcher("/until6HttpSession随机数.jsp?flag=success");
re.forward(request,response);
}
}
主要是使用Math.random方法产生1~50的随机数,并将它保存在session中。判断逻辑之后,利用RequestDispatcher中的forward方法跳转。