1 package com.bjsxt.cookie; 2 3 import java.io.IOException; 4 import java.net.URLEncoder; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.Cookie; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 /** 14 * Servlet implementation class TestSetCookie 15 */ 16 @WebServlet("/TestSetCookie") 17 public class TestSetCookie extends HttpServlet { 18 19 @Override 20 protected void service(HttpServletRequest req, HttpServletResponse resp) 21 throws ServletException, IOException { 22 System.out.println("TestSetCookie.service()"); 23 //创建cookie 24 Cookie cookie01=new Cookie("vivo","vivox5"); 25 Cookie cookie02=new Cookie("vivo","vivox6"); 26 Cookie cookie03=new Cookie("vivo","vivox7"); 27 Cookie cookie04=new Cookie("vivo","vivox21"); 28 //设置中文编码cookie 29 Cookie cookie05=new Cookie("vivo",URLEncoder.encode("步步高五","utf-8")); 30 //设置cookie信息的有效期,setmaxage是以秒为单位 31 cookie03.setMaxAge(3*24*3600); 32 cookie04.setMaxAge(3*24*3600); 33 //设置cookie的访问路径 34 cookie01.setPath(req.getContextPath()+"/abc"); 35 36 //发送给客户端 37 resp.addCookie(cookie01); 38 resp.addCookie(cookie02); 39 resp.addCookie(cookie03); 40 resp.addCookie(cookie04); 41 resp.addCookie(cookie05); 42 } 43 44 45 }
1 package com.bjsxt.cookie; 2 3 import java.io.IOException; 4 import java.net.URLDecoder; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.annotation.WebServlet; 8 import javax.servlet.http.Cookie; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 /** 14 * Servlet implementation class TestGetCookie 15 */ 16 @WebServlet("/TestGetCookie") 17 public class TestGetCookie extends HttpServlet { 18 19 @Override 20 protected void service(HttpServletRequest req, HttpServletResponse resp) 21 throws ServletException, IOException { 22 System.out.println("TestGetCookie.service()"); 23 //获取cookie--获取请求头里的cookie(从浏览器获取) 24 Cookie[] cookies=req.getCookies(); 25 //获取cookie的名称和值 26 for(int i=0;i<cookies.length;i++){ 27 System.out.println("cookie:"+cookies[i].getName()+":"+ URLDecoder.decode(cookies[i].getValue(),"utf-8")); 28 } 29 } 30 31 }
session
1 package com.bjsxt.session; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.annotation.WebServlet; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 import javax.servlet.http.HttpSession; 11 /** 12 * session:将http状态信息保存在服务器端的技术 13 * seesion是客户端和服务器之间的一次会话 14 * 会话-开始:当用户第一访问服务器时候就开始了 15 * 结束:客户端--关闭浏览器(清除cookie) 16 * 服务器--A:超过有效期 在tomcat中web.xml中配置 <session-config> 17 <session-timeout>30</session-timeout> 18 </session-config> 19 * B:在本项目中web.xml配置session-config 20 * C:设置session的最大不活动周期 21 * D:服务器关闭 22 * 23 * 24 * cookie:不安全,效率低,容量小,但是不能舍弃这门技术,因为session的跟踪机制需要cookie来保存JSESSIONID 25 */ 26 @WebServlet("/HelloSession") 27 public class HelloSession extends HttpServlet { 28 29 @Override 30 protected void service(HttpServletRequest req, HttpServletResponse resp) 31 throws ServletException, IOException { 32 System.out.println("HelloSession.service()"); 33 34 //获取session 35 HttpSession hs = req.getSession(); 36 37 //设置sesssion的最大不活动周期 38 //hs.setMaxInactiveInterval(10); 39 40 //强制sesssion失效 41 //hs.setMaxInactiveInterval(0); 42 //hs.invalidate(); 43 44 //获取sessionid 45 String hsid = hs.getId(); 46 System.out.println("HelloSession.service()sessionid:"+hsid); 47 48 } 49 50 51 }