2020.8.4英谷实训日志

声明:

此文转自刘峰吉老师的Javaweb的Word文档,内容是刘老师自己整理的,我只是个没有感情的搬运工

session(web_session)

1、Session 概述
Session也是一个域对象,可以在自身的属性域中保存数据,在一定范围内共享。

2、Session的工作机制
1)并不是浏览器一访问服务器就创建Session对象,而是只有在服务器端调用request.getSession()方法时,Tomcat服务器才会“创建”Session对象。

2)request.getSession()方法
(1)请求中没有携带任何有关Session的标识,往往就是浏览器第一次访问的时候。
(2)请求中没有携带任何有关Session的标识:Tomcat会创建一个Session对象,并创建一个特殊的Cookie,这个特殊Cookie的name属性是JSESSIONID,值是一个唯一值,而且是随机生成的。将这个JSESSIONID Cookie作为关联这个Session对象的标识写回浏览器。
(3)请求中携带了JSESSIONID时,Tomcat会在已经创建好的Session对象的集合中,查找匹配的Session对象。实现多个请求识别浏览器的身份的目的。
①能找到:返回找到的Session对象
②找不到:创建一个新的Session对象返回.通常由于服务器超时释放。
即:我们是通过JSESSIONID的值找到的session对象。
3)isNew()返回boolean标识当前Session对象是新创建的,还是原有的
4)getId()返回当前Session对象的JSESSIONID值

3、Session的时效管理
Session代表浏览器和服务器之间的一次会话,本来应该在会话开始时创建,会话结束时释放。但是浏览器关闭时,服务器根本检测不到,那就只能由服务器自己设置一个时间了。
[1]自然超时
<1>Tomcat配置文件中web.xml中有默认的超时时间的配置

30 Session对象默认的有效时间是30分钟,指的并不是Session对象从创建开始30分钟后过期,而是从最后一次访问开始,30分钟后失效。 <2>调用Session对象的方法手动设置超时时间 session.setMaxInactiveInterval(10)。以秒为单位 [2]手动调用invalidate()方法直接失效

4、项目阶段
登录成功,记录浏览器信息
1)显示登录用户
//将用户信息保存到Session对象中,到JSP页面中就可以通过Session隐含对象将数据读取出来
HttpSession session = request.getSession();
session.setAttribute(“loginUser”, userName);

2)退出操作
String method = request.getParameter(“method”);
if(“logout”.equals(method)){
HttpSession session = request.getSession();
session.invalidate();
response.sendRedirect(request.getContextPath()+"/index.jsp");
return;
}

5、code
1)SessionTestServlet

package com.alex.web.session;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SessionTestServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("username");
		//获取并返回之前分配的session
		//如果之前没分配过,就创建一个
		HttpSession session = request.getSession(true);
		//服务器将JSessionId写到客户端Cookie中了
		System.out.println("JSESSIONID" + session.getId());
		session.setAttribute("username", username);
		
		//一般注销时,需要销毁session
		//表示销毁session
		//session.invalidate();
		
		//注销系统,也可以不杀死session,而是将session数据清理掉
		//删除session存的数据
		//session.removeAttribute("username");
	}

}

2)web.xml
<?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">
	<display-name>web_session</display-name>

	<servlet>
		<servlet-name>SessionTestServlet</servlet-name>
		<servlet-class>com.alex.web.session.SessionTestServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>SessionTestServlet</servlet-name>
		<url-pattern>/SessionTestServlet</url-pattern>
	</servlet-mapping>
	
	<!-- 单位是分钟 -->
	<session-config>
		    <session-timeout>30</session-timeout>
	</session-config>

	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		    <welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

3)testsession.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>
	<form action="SessionTestServlet" method="post">
		用户名<input type="text" name="username" value="alex"/>
		<input type="submit" value="发送POST请求" />
	</form>
</body>
</html>

4)a.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>
${sessionScope.username}
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值