Session
案例演示思路:
案例演示:
工程案例目录结构
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>Jsp05</display-name>
<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>
<servlet>
<servlet-name>create</servlet-name>
<servlet-class>web.CreateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>create</servlet-name>
<url-pattern>/create</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>view</servlet-name>
<servlet-class>web.ViewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>view</servlet-name>
<url-pattern>/view</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>modify</servlet-name>
<servlet-class>web.ModifyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>modify</servlet-name>
<url-pattern>/modify</url-pattern>
</servlet-mapping>
<!--
配置session超时时间(分钟)
当用户的空闲(不操作)时间超出该时间时,
服务器将销毁此session。
-->
<session-config>
<session-timeout>1</session-timeout>
</session-config>
</web-app>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.study</groupId>
<artifactId>Jsp05</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javaee</groupId>
<artifactId>javaee-api</artifactId>
<version>5</version>
</dependency>
</dependencies>
</project>
CreateServlet.java
package web;
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 CreateServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
//首次访问此项目时,服务器会给这个
//浏览器创建一个session,并存入request
HttpSession session = req.getSession();
//session中可以存储任何类型的数据
session.setAttribute("user", "caocao");
//session中可以直接存储中文
session.setAttribute("city", "北京");
}
}
ViewServlet.java
package web;
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 ViewServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
HttpSession session = req.getSession();
String user = (String)session.getAttribute("user");
System.out.println(user);
String city = (String)session.getAttribute("city");
System.out.println(city);
req.getRequestDispatcher("WEB-INF/view.jsp").forward(req, res);
}
}
ModifyServlet.java
package web;
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 ModifyServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
HttpSession session = req.getSession();
//修改数据(覆盖)
session.setAttribute("user", "LiuBei");
//删除数据
session.removeAttribute("city");
}
}
view.jsp
<%@page pageEncoding="utf-8"%>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>session</title>
</head>
<body>
<!--
EL的取值范围:{处理字符串null-。不显示}
page->request->session->application
-->
<p>${user }</p>
<p>${city }</p>
</body>
</html>
将Jsp05工程部署到Tomcat上,运行Tomcat启动案例工程,
浏览器录入http://localhost:8080/Jsp05/create 创建一个session
浏览器再录入http://localhost:8080/Jsp05/view 显示这个一个session
页面显示:
浏览器录入http://localhost:8080/Jsp05/modify 显示这个一个session
浏览器再录入http://localhost:8080/Jsp05/view 显示这个一个session
页面显示:
<!--
配置session超时时间(分钟)
当用户的空闲(不操作)时间超出该时间时,
服务器将销毁此session。
-->
<session-config>
<session-timeout>1</session-timeout>
</session-config>
对应session超时设置:
我们不操作浏览器等待1分钟后再次 请求 http://localhost:8080/Jsp05/view
浏览器页面显示及后台输出:
A.
B.