1.简介
- 由javaWeb提供,用来进行会话(用户打开浏览器到关闭浏览器之间的操作)跟踪的。服务器为每一个用户都生成一个session对象,用于保存用户信息以及追踪用户的操作状态。
- session对象的内部是用map来保存数据的。
- 是Servlet的三大域之一,因此也有getAttribute、setAttribute、removeAttribute方法
2.相关API
- request.getSession()
- setAttribute()
- getAttribute()
- removeAttribute()
- getId()
- inValidate()
3.案例1:演示数据共享
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession httpSession = req.getSession();
httpSession.setAttribute("aaa","AAA");
resp.sendRedirect("http://localhost:8080/javaWeb/index.jsp");
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession httpSession = req.getSession();
System.out.println(httpSession.getAttribute("aaa"));
}
<html>
<body>
<a href="http://localhost:8080/javaWeb/B">A</a>
</body>
</html>
4.案例2:登录案例
//index.jsp
<%
String user = (String)session.getAttribute("username");
if(user == null){
response.sendRedirect("/javaWeb/login.jsp");
}
%>
<html>
<body>
<h1>home</h1>
welcome,<%=user%>
</body>
</html>
//login.jsp
<%
String message = "";
String msg = (String)request.getAttribute("msg");
if(msg !=null){
message = msg;
}
%>
<%
String username = "";
Cookie [] cookies = request.getCookies();
if(cookies != null){
for(Cookie demo:cookies){
if(demo.getName().equals("username")){
username = demo.getValue();
break;
}
}
}
%>
<html>
<body>
<h1>login</h1>
<%=message%>
<form method="post" action="http://localhost:8080/javaWeb/Login">
username:<input type="text" name="username" value="<%=username %>"/>
password:<input type="text" name="password" />
<input type="submit" name="submit" value="submit"/>
</form>
</body>
</html>
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
if(!username.isEmpty()&&!password.isEmpty()){
HttpSession httpSession = req.getSession();
httpSession.setAttribute("username",username);
resp.addCookie(new Cookie("username",username));
resp.sendRedirect("/javaWeb/index.jsp");
}else{
resp.setCharacterEncoding("utf-8");
req.setAttribute("msg","用户密码错误");
req.getRequestDispatcher("/login.jsp").forward(req,resp);
}
}
5.设置session最大不活动时间(登录成功后,在多长时间内没有进行页面跳转或者超链接等操作,session失效,默认为30min)
- session.setMaxInactiveInterval()
- web.xml中设置(单位时间)
<session-config>
<session-timeout>30</session-timeout>
</session-config>
6.session原理
当首次使用session时,服务器端要创建session,session是保存在服务器端,而给客户端的session的id(一个cookie中保存了sessionId)。客户端带走的是sessionId,而数据是保存在session中。当客户端再次访问服务器时,在请求中会带上sessionId,而服务器会通过sessionId找到对应的session,而无需再创建新的session。
7.session的url重写
我们知道session依赖Cookie,那么session为什么依赖Cookie呢?因为服务器需要在每次请求中获取sessionId,然后找到客户端的session对象。那么如果客户端浏览器关闭了Cookie呢?那么session是不是就会不存在了呢?
其实还有一种方法让服务器收到的每个请求中都带有sessioinId,那就是URL重写!在每个页面中的每个链接和表单中都添加名为JSessionId的参数,值为当前sessionid。当用户点击链接或提交表单时服务器可以通过获取JSessionId这个参数来得到客户端的sessionId,找到sessoin对象。
使用response.encodeURL()对每个请求的URL处理,这个方法会自动追加JSessionId参数,使用response.encodeURL()更加“智能”,它会判断客户端浏览器是否禁用了Cookie,如果禁用了,那么这个方法在URL后面追加JSessionId,否则不会追加。