会话:用户打开浏览器,点击了很多超链接,访问多个web资源,关闭浏览器,这个过程可以称之为会话
有状态会话:用户访问过的网页,下次再次访问网页时,浏览器知道该用户访问过,不需重新验证
怎么证明某用户访问过某网站
服务端给客户端一个信件,下次客户端带上信件访问即可,即Cookie
服务端登记访问的客户,客户下次访问时服务端自动匹配客户,即Session
从请求中拿到cookie
//从请求中获取Cookie,来检验Cookie //注意:因为客户的Cookie是服务端给的,所以客户在第一次请求时无Cookie Cookie[] cookies = req.getCookies(); //获取并判断cookie的key cookie.getName().equals("name")) //获取cookie的值
服务端响应给客户端一个cookie
//生成Cookie对象 Cookie cookie = new Cookie("name",System.currentTimeMillis()+""); //设置cookie有效期为一天 cookie.setMaxAge(24*24*60); //给客户响应一个Cookie resp.addCookie(cookie);
cookie一般会保存在本地的用户目录下的AppData中
cookie存在上限
删除cookie
不设置有效期,关闭浏览器cookie自动失效
设置cookie的有效期为0,即:
cookie.setMaxAge(0);
解决网络编程中数据乱码问题,如Cooike中的Value为中文;注意:这与请求、响应的乱码解决无关
先进行编码
cookie.getValue() = URLEncoder.encode("张某某","UTF-8")
输出时再解码
URLDecoder.decode(cookie.getValue(),"UTF-8")
cookie的实现
public class CookieDemo_01 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //解决请求响应乱码 req.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8");//设置服务端的编码 resp.setContentType("text/html charset=utf-8");//设置客户端以该编码显示 PrintWriter out = resp.getWriter(); //从请求中获取Cookie,来检验Cookie //注意:因为客户的Cookie是服务端给的,所以客户在第一次请求时无Cookie Cookie[] cookies = req.getCookies(); if(cookies!=null){ out.write("你上次访问本页面的时间是:"); for (int i = 0; i < cookies.length; i++) { Cookie cookie = cookies[i]; if (cookie.getName().equals("name")) { long loginTime = Long.parseLong(cookie.getValue());//将字符串型的时间转换为长整型 Date date = new Date(loginTime);//将长整型时间转换回日期型 out.write(date.toLocaleString()); } } }else { out.write("这是你第一次访问本页面"); } //生成Cookie对象 Cookie cookie = new Cookie("name",System.currentTimeMillis()+""); //cookie有效期为一天 cookie.setMaxAge(24*24*60); //给客户响应一个Cookie resp.addCookie(cookie); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
定义(什么是Session)
服务器给每个用户浏览器创建一个Session对象
一个Session独占一个浏览器
用户登录一个网站后,整个网站他都能访问
Session和Cookie的区别
使用场景
使用Session
获取Session并存入数据
public class SessionDemo_01 extends HttpServlet { /** *给session中存入数据 * */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //解决乱码问题 req.setCharacterEncoding("utf-8"); resp.setCharacterEncoding("utf-8"); resp.setContentType("text/html charset=utf-8"); //获取Session HttpSession session = req.getSession(); //给session中存入数据 session.setAttribute("name",new Person("张某某",16)); //获取session的ID String sessionId = session.getId(); //判断是否为新的session if(session.isNew()){ resp.getWriter().write("session创建成功,ID:"+sessionId); }else{ //session在浏览器打开的瞬间就存在了,关闭浏览器就自动清除 resp.getWriter().write("session已经存在,ID:"+sessionId); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
public class Person { /** * SessionDemo_01中的数据对象 * */ private String name; private int age; //无参构造 public Person() { } //有参构造 public Person(String name, int age) { this.name = name; this.age = age; } //getName public String getName() { return name; } //getAge public int getAge() { return age; } //setName public void setName(String name) { this.name = name; } //setAge public void setAge(int age) { this.age = age; } //重写toString方法,调用该方法打印对象的属性 @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
获取Session并读取控制台输出1中存入的数据
public class SessionDemo_02 extends HttpServlet { /** * 获取session中的数据 * */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //解决乱码问题 req.setCharacterEncoding("utf-8"); resp.setCharacterEncoding("utf-8"); resp.setContentType("text/html charset=utf-8"); //获取Session HttpSession session = req.getSession(); Person person = (Person) session.getAttribute("name"); System.out.println(person.toString()); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
注销Session
手动注销
public class SessionDemo_03 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取session HttpSession session = req.getSession(); //取消掉session中的key(name) session.removeAttribute("name"); //注销session,session中的ID也会被注销,只不过服务端又会立即生成新的ID session.invalidate(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
自动注销,web.xml中配置
<!--设置session默认的失效时间--> <session-config> <!--session在1分钟后失效,以分钟为单位--> <session-timeout>1</session-timeout> </session-config>