Web开发基础_Servlet学习_0027_状态管理

状态管理

Web开发基础_Servlet学习_0027_状态管理_第1张图片

Cookie

Web开发基础_Servlet学习_0027_状态管理_第2张图片

案例演示:

工程案例目录结构

Web开发基础_Servlet学习_0027_状态管理_第3张图片

pom.xml


  4.0.0
  com.study
  Jsp04
  0.0.1-SNAPSHOT
  war
  
  
  	
  		javaee
  		javaee-api
  		5
  	
  

web.xml



  Jsp04
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
  
  	create
  	web.CreateServlet
  
  
  	create
  	/aa/create
  
  
  
  	view
  	web.ViewServlet
  
  
  	view
  	/aa/view
  
  
  
  	modify
  	web.ModifyServlet
  
  
  	modify
  	/aa/modify
  
  
    
  	duang
  	web.Duang
  
  
  	duang
  	/bb/duang
  

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"%>


	
		Cookie
		
	
	
		
		 

账号:${cookie.user.value }

将Jsp04工程部署到Tomcat上,运行Tomcat启动案例工程,

浏览器录入http://localhost:8088/Jsp04//aa/create 创建一个cookie

后再录入http://localhost:8088/Jsp04//aa/create  显示这个cookie

Web开发基础_Servlet学习_0027_状态管理_第4张图片

 再次录入http://localhost:8088/Jsp04//aa/modify 修改cookie

Web开发基础_Servlet学习_0027_状态管理_第5张图片

当录入http://localhost:8088/Jsp04//bb/duang 会发现 bb/路径下无法获取/aa请求时创建的cookie

Web开发基础_Servlet学习_0027_状态管理_第6张图片

 

你可能感兴趣的:(Web开发,Servlet)