状态管理
Cookie
案例演示:
工程案例目录结构
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>Jsp04</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>
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>Jsp04</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>/aa/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>/aa/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>/aa/modify</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>duang</servlet-name>
<servlet-class>web.Duang</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>duang</servlet-name>
<url-pattern>/bb/duang</url-pattern>
</servlet-mapping>
</web-app>
CreateServlet.java
package web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CreateServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
//创建Cookie
Cookie c1 = new Cookie("user","zhangsan");
//设置Cookie的生存时间
//未设置时间时它存到浏览器内存中
//设置时间后它存到硬盘上
//达到这个时间时浏览器会自动删除它
c1.setMaxAge(6000);
//发送给浏览器
res.addCookie(c1);
}
}
ViewServlet.java
package web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ViewServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
//获取Cookie
Cookie[] cs = req.getCookies();
if(cs != null){
for(Cookie c:cs){
System.out.println(c.getName()+":"+c.getValue());
System.out.println(c.getMaxAge());
}
}
//当前:/Jsp04/aa/view
//目标:/Jsp04/WEB-INF/view.jsp
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.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ModifyServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
Cookie[] cs = req.getCookies();
if(cs !=null){
for(Cookie c:cs){
//找到要修改的这个Cookie
if(c.getName().equals("user")){
//修改
c.setValue("cang");
//将修改后的Cookie发送给浏览器
res.addCookie(c);
}
}
}
}
}
Duang.java
package web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Duang extends HttpServlet{
@Override
protected void service(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException {
//获取Cookie
Cookie[] cs = req.getCookies();
if(cs != null){
for(Cookie c:cs){
System.out.println(c.getName()+":"+c.getValue());
System.out.println(c.getMaxAge());
}
}
//当前:/Jsp04/aa/view
//目标:/Jsp04/WEB-INF/view.jsp
req.getRequestDispatcher("../WEB-INF/view.jsp").forward(req, res);
}
}
view.jsp
<%@page pageEncoding="utf-8"%>
<!doctype html>
<html>
<head>
<title>Cookie</title>
<meta charset="utf-8">
</head>
<body>
<!--
通过EL可以直接获取Cookie。
语法:cookie.key.value
-->
<p>账号:${cookie.user.value }</p>
</body>
</html>
将Jsp04工程部署到Tomcat上,运行Tomcat启动案例工程,
浏览器录入http://localhost:8088/Jsp04//aa/create 创建一个cookie
后再录入http://localhost:8088/Jsp04//aa/create 显示这个cookie
再次录入http://localhost:8088/Jsp04//aa/modify 修改cookie
当录入http://localhost:8088/Jsp04//bb/duang 会发现 bb/路径下无法获取/aa请求时创建的cookie