会话技术
从打开一个浏览器访问某个站点,到关闭这个浏览器的整个过程,成为一次会话。会 话技术就是记录这次会话中客户端态的状与数据的。
会话技术分为Cookie和Session:
Cookie:数据存储在客户端本地,减少服务器端的存储的压力,安全性不好,客户端 可以清除cookie
Session:将数据存储到服务器端,安全性相对好,增加服务器的压力
服务器端向客户端发送一个Cookie
1)创建Cookie:
Cookie cookie = new Cookie(String cookieName,String cookieValue);
示例:
Cookie cookie = new Cookie("username","zhangsan");
该cookie会以响应头的形式发送给客户端:
Set-Cookie:"name=zhangsan"
注意:Cookie中不能存储中文
2)设置Cookie在客户端的持久化时间:
cookie.setMaxAge(int seconds); ---时间秒
注意:如果不设置持久化时间,cookie会存储在浏览器的内存中,浏览器关闭 cookie信息销毁(会话级别的cookie),如果设置持久化时间,cookie信息会被持久化到浏览器的磁盘文件里
示例:
cookie.setMaxAge(10*60);
设置cookie信息在浏览器的磁盘文件中存储的时间是10分钟,过期浏览器 自动删除该cookie信息
3)设置Cookie的携带路径:
cookie.setPath(String path);
注意:如果不设置携带路径,那么该cookie信息会在访问产生该cookie的 web资源所在的路径都携带cookie信息
//示例: cookie.setPath("/WEB16"); //代表访问WEB16应用中的任何资源都携带cookie cookie.setPath("/WEB16/cookieServlet"); //代表访问WEB16中的cookieServlet时才携带cookie信息
4)向客户端发送cookie:
response.addCookie(Cookie cookie);
5)删除客户端的cookie:
如果想删除客户端的已经存储的cookie信息,那么就使用同名同路径的持久化时 间为0的cookie进行覆盖即可
例子:
package com.oracle.demo01; 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 SendCookieServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //创建cookie Cookie cookie=new Cookie("goods","phone"); //设置cookie持久化存储时间,也就是在磁盘中保存时间 cookie.setMaxAge(60*3); //设置cookie携带路径 // cookie.setPath("/Web06/SendCookieServlet"); //cookie.setPath("/WEB16"); //代表访问WEB16应用中的任何资源都携带cookie //cookie.setPath("/WEB16/cookieServlet"); //代表访问WEB16中的cookieServlet时才携带cookie信息 //cookie.setPath("/");代表服务器下所有资源都携带 //删除cookie //发送cookie response.addCookie(cookie); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
清除Cookies
package com.oracle.demo01; 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 removecookieservlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //删除客户端保存的信息 //创建cookie Cookie cookie=new Cookie("goods","phone"); //设置cookie携带路径 cookie.setPath("/Web06/SendCookieServlet"); cookie.setMaxAge(0); //发送cookie response.addCookie(cookie); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
服务器端怎么接受客户端携带的Cookie
cookie信息是以请求头的方式发送到服务器端的:
Cookies:"name=zhangsan"
1)通过request获得所有的Cookie:
Cookie[] cookies = request.getCookies();
2)遍历Cookie数组,通过Cookie的名称获得我们想要的Cookie
for(Cookie cookie : cookies){ if(cookie.getName().equal(cookieName)){ String cookieValue = cookie.getValue(); } }
例子:
package com.oracle.demo01; 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 GETCookieServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获得客户端携带的cookie Cookie[] cookies = request.getCookies(); //遍历,通过cookie名称获得想要的cookie值 for(Cookie c : cookies){ if(c.getName().equals("goods")){ String cookieValue = c.getValue(); System.out.println(cookieValue); } } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
例子:显示用户上次访问时间
第一次访问,你是第一次访问,
非第一次访问,显示上次访问时间
package com.oracle.demo02; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; 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 WelcomeServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取当前时间 Date date=new Date(); SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String time=sdf.format(date); //创建cookie对象 Cookie cookie=new Cookie("lastAccessTime",time); cookie.setMaxAge(60*3); response.addCookie(cookie); //获取cookie值 Cookie[] cookies = request.getCookies(); String lastAccessTime=null; if(cookies!=null){ for(Cookie c : cookies){ if(c.getName().equals("lastAccessTime")){ lastAccessTime = c.getValue(); } } } //解决响应时中文乱码问题 response.setContentType("text/html;charset=utf-8"); if(lastAccessTime==null){ response.getWriter().write("你是第一次访问"); }else{ response.getWriter().write("你上次访问的时间"+lastAccessTime); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
Session技术
Session技术是将数据存储在服务器端的技术,会为每个客户端都创建一块内存空间 存储客户的数据,但客户端需要每次都携带一个标识ID去服务器中寻找属于自己的内存空间。所以说Session的实现是基于Cookie,Session需要借助于Cookie存储客 户的唯一性标识JSESSIONID
获得Session对象
HttpSession session = request.getSession();
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //创建属于该客户端(会话)的私有的session区域 // request.getSession()方法内部会判断 该客户端是否在服务器端已经存在session // 如果该客户端在此服务器不存在session 那么就会创建一个新的session对象 // 如果该客户端在此服务器已经存在session 那么就获得已经存在的该session返回 HttpSession session=request.getSession(); String id=session.getId();//该session对象的编号id response.getWriter().write("JESSIONID:"+id); }
向session中存取数据(session也是一个域对象)
Session也是存储数据的区域对象,所以session对象也具有如下三个方法:
session.setAttribute(String name,Object obj);
session.getAttribute(String name);
session.removeAttribute(String name);
Session对象的生命周期
on对象的生命周期(面试题/笔试题)
创建:第一次执行request.getSession()时创建
销毁:
1)服务器(非正常)关闭时
2)session过期/失效(默认30分钟)
时间的起算点 从不操作服务器端的资源开始计时
可以在工程的web.xml中进行配置
<session-config> <session-timeout>30</session-timeout> </session-config>
3)手动销毁session
session.invalidate();
作用范围:
默认在一次会话中,也就是说在,一次会话中任何资源公用一个session对象
例子:
package com.oracle.demo03; 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; import javax.servlet.http.HttpSession; public class SessionServlet1 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取session对象 HttpSession session = request.getSession(); //向session中存数据 session.setAttribute("goods","手机"); String id=session.getId(); //将JSESSIONID持久化存储 Cookie cookie=new Cookie("JSESSIONID",id); cookie.setPath("/Web06"); cookie.setMaxAge(60*3); response.addCookie(cookie); response.getWriter().write("JSESSIONID:"+id); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package com.oracle.demo03; 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 SessionServlet2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获得session对象 HttpSession session = request.getSession(); //获取值 String goods=(String)session.getAttribute("goods"); response.setContentType("text/html;charset=utf-8"); response.getWriter().write(goods); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
总结:
会话技术:
Cookie技术:存到客户端
发送cookie
Cookie cookie = new Cookie(name,value)
cookie.setMaxAge(秒)
cookie.setPath()
response.addCookie(cookie)
获得cookie
Cookie[] cookies = request.getCookies();
cookie.getName();
cookie.getValue();
Session技术:存到服务器端 借助cookie存储JSESSIONID
HttpSession session = request.getSession();
setAttribute(name,value);
getAttribute(name);
session生命周期
创建:第一次指定request.getSession();
销毁:服务器关闭、session失效/过期、手动session.invalidate();
session作用范围:默认一会话中