文章目录
-
-
- HttpSession会话管理
- HttpSession的生命周期
- 实践
-
HttpSession会话管理
HTTP协议是无意识、单向协议。服务端不能主动连接客户端,只能等待并答复客户端请求。客户端连接服务端,发出一个HTTP请求,服务端处理请求,并返回一个HTTP响应给客户端,至此,本次会话结束。HTTP协议本身不支持服务端保存客户端的状态等信息。于是,Web服务器引入了session的概念,用来保存客户端的信息。
方法:HttpSession session = request.getSession(); //获取Session对象
Session.setAttribute(“username”,”John”); //设置Session中的属性
原理:
利用服务器来管理会话的机制,当程序为某个客户端的请求创建了一个session的时候,服务器会检查客户端的请求是否已经包含了一个session标识。
URL重写前面提到过,如果客户端支持Cookie,那么生成的URL不变,如果不支持,生成的URL中就会带有jsessionid字符串的地址。
HttpSession的生命周期
1.创建HttpSession对象
服务器为每个浏览器创建不同Session的ID值。使用request.getSession()或request.getSession(true)方法来获得HttpSession对象。
2.使用HttpSession对象
将产生的sessionID存入到Cookie中;
当客户端再次发送请求时,会将sessionID与request一起传送给服务端;
服务器根据请求过来的sessionID与保存在服务器端的session对应起来判断是否是同一session。
3.HttpSession对象的消亡
将浏览器关闭;
调用HttpSession的invalidate()方法;
session超时。
有效期设定
调用Session的setMaxInactiveInterval(long interval)设定;
在web.xml中修改,例如:
< s e s s i o n − c o n f i g > <session-config> <session−config>
< ! − − 会 话 超 时 时 长 为 30 分 钟 − − > <!-- 会话超时时长为30分钟 --> <!−−会话超时时长为30分钟−−>
< s e s s i o n − t i m e o u t > 30 < / s e s s i o n − t i m e o u t > <session-timeout>30</session-timeout> <session−timeout>30</session−timeout>
< / s e s s i o n − c o n f i g > </session-config> </session−config>
实践
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML>
<html>
<head>
<title>用户登录</title>
</head>
<body>
<p>用户登录</p>
<form action="<%=path%>/CheckUser" method="post">
<table border="1" width="250px;">
<tr>
<td width="75px;">用户名:</td>
<td ><input name="userId"/></td>
</tr>
<tr>
<td width="75px;">密 码:</td>
<td ><input name="passwd" type="password"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
</body>
</html>
package com.eshore;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.annotation.WebServlet;
//给当前文件的路径设置为根目录下的GetReaderBody2
@WebServlet(
urlPatterns = { "/CheckUser" },
name = "checkUser"
)
public class CheckUser extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String userId = request.getParameter("userId");
String passwd = request.getParameter("passwd");
//判断是否是linl用户且密码相符
if(userId!=null&&"linl".equals(userId)
&&passwd!=null&&"123456".equals(passwd)){
//获得sessioin对象
HttpSession session = request.getSession();
//设置user参数
session.setAttribute("user", userId);
//跳转页面
RequestDispatcher dispatcher = request.
getRequestDispatcher("/welcome.jsp");
dispatcher.forward(request, response);
}else{
RequestDispatcher dispatcher = request.
getRequestDispatcher("/login.jsp");
dispatcher.forward(request, response);
}
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<title>欢迎页面</title>
</head>
<%
String user = (String)session.getAttribute("user");
if(user==null){
%>
<jsp:forward page="login.jsp"/>
<%} %>
<body>
<a href="<%=response.encodeURL(" target="_blank" rel="external nofollow" login.jsp?username=john") %>"></a>
欢迎您:<%=user%>。
</body>
</html>
运行结果: