zoukankan      html  css  js  c++  java
  • 2020Session学习笔记

    package com.demo.test;

    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.*;
    import java.io.IOException;
    import java.util.Enumeration;

    public class SessionServlet extends HttpServlet {
    /**
    * Session简介
    * 1、Session是一个接口(HttpSession)。
    * Session就是回会。它是一个用来维护客户端和服务器之间关联的技术。
    * 每个客户端都会有自己的一个Session会话。
    * Session会话中,我们经常用来保存用户登录之后的信息。
    */


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    setAtrribute(req,resp);
    getAtrribute(req,resp);
    }

    /**
    * 创建Session和获取(id是否更新)
    * 1、如何创建和获取Session。它们的API是一样的。request.getSession()
    * 第一次调用:创建Session会话
    * 之后调用都是:获取创建号的Session会话对象
    *
    *判断到底是不是新创新创建的Session(isNew():返回一个布尔值,true即new,false则old)
    *
    * 每个会话都有一个身份证号。也就是ID值。而且这个ID值时唯一的(getId():获取id)。
    *
    */



    protected void createGetSession (HttpServletRequest req, HttpServletResponse resp) throws IOException {
    /**
    * 创建、获取Session、判断是不是共才创建的
    */

    resp.setContentType("text/html;charset=UTF-8");
    //创建和获取Session
    HttpSession session = req.getSession();
    // 判断当前Session是不是新创建的
    boolean isNew = session.isNew();
    //获取Session的唯一标识(id)
    String sessionId = session.getId();

    resp.getWriter().write("得到它的Id是:"+sessionId+"<br/>");
    resp.getWriter().write("这个Session是不是新创建的:"+isNew);
    }


    protected void setAtrribute(HttpServletRequest req,HttpServletResponse resp) throws IOException{
    /**
    * 往Session域中存储数据
    *
    */
    resp.setContentType("text/html;charset=UTF-8");
    req.getSession().setAttribute("key1","value1");
    resp.getWriter().write("Session的值存储成功!"+"<br/>");
    }

    protected void getAtrribute(HttpServletRequest req,HttpServletResponse resp) throws IOException{
    /**
    * 往Session域中存储数据
    *
    */
    resp.setContentType("text/html;charset=UTF-8");
    Object key1 = req.getSession().getAttribute("key1");
    resp.getWriter().write("key1的值为:"+key1);
    }
    /**
    * Session生命周期控制
    * Session超时指的是客户端两次请求间隔的最大时长;
    * public void invalidate() 让当前Session会话马上销毁;
    * public void setMaxInactiveInterval(int interval) 设置Session的超时时间(秒),超过指定时长,session就会被销毁
    * interval:值为正数的时候,设置Session的超时时长;值为负数时,表示Session永不超时;
    * public int getMaxInactiveInterval() 获取Session的超时时间(极少使用);
    *
    * Session默认的超时时长是1800秒,半个小时,因为在Tomcat服务器的配置文件web.xm中默认有以下的配置:他就表示配置了当前Tomcat
    * 服务器下所有Session超时配置时长默认为30分钟。
    * <session-config>
    * <session-timeout>30</session-timeout>
    * </session-config>
    *
    * 如果不要这个配置的Session默认超时时长,可以在自己项目的web工程web.xml中做以上的相同配置。
    *
    * 只修改个别的Session默认超时时长,直接使用setMaxInactiveInterval(int interval);来进行相同的设置
    *
    */

    /**
    * 浏览器和Session之间的关联;
    * 客户端(浏览器)在没有任何的Cookie信息情况下,发送请求,服务器(Tomcat)request.getSession();创建会话对象(并储存在服务器的内存中)。
    *
    * 服务器每次创建Session会话的时候,都会创建一个Cookie对象,JSESSIONID,值是新创建出来的Session的id值。
    *
    * 通过响应把新创建的Session的id值返回给客户端(Set-Cookie:JSESSIONID=......;)
    *
    * 客户端(浏览器),解析收到数据,就马上创建一个Cookie对象。后面有了Cookie之后,每次请求,都会把Session的id以Cookie的形式发送给
    *
    * 服务器,服务器还是调用request.getSession();通过Cookie中的id值,找到之前自己创建好的Session对象,并返回
    *
    * 删除掉Session的Cookie。(此时服务器内存中的Session并没有超时)
    *再次访问服务器,服务器又会创建一个新的Session,因为服务器发送请求时,没有带参数,服务器只能重新创建新Session
    *
    *
    *
    */

    }
  • 相关阅读:
    Leetcode.11 Container with Most Water
    Leetcode.19 Remove Nth Node From End of List
    Leetcode23. Merge K sorted List
    leetcode287. Find the duplicate Number
    LeetCode234. Palindrome Linked List
    leetcode.142 LinkedList Cycle II
    UINavigationController
    UITableView的性能优化1
    iOS触摸事件
    UITableView的性能优化
  • 原文地址:https://www.cnblogs.com/youlingdada-top/p/13677062.html
Copyright © 2011-2022 走看看