• JavaWeb Session的使用


    关于Cookie的详细信息,可以看一下这篇文章—— 转载:https://blog.csdn.net/rubulai/article/details/91873075

    Session技术,依赖于Cookie

    简述一下Cookie和Session的区别:

    • Cookie是把用户的数据写给用户的浏览器,浏览器保存
    • Session把用户的数据写到用户独占的Session中,服务器端保存(保存重要的信息,减少服务器资源的浪费)

    Session标识传递的流程图

    Session目前支持的方法

    方法 返回值 作用
    getCreateTime(): 
    long
    获取创建时间
     
    getID(): 
    String
     获取id
     
    getLastAccessedTime(): 
     
    long
     获取最后一次访问时间
     
    getServletContext():
     
    ServletContext
     ServletContext对象
     
    setMaxInactiveInterval(int): 
     
    void
     设置最大时间
     
    getMaxInactiveInterval():
     
    int
     获取最大时间
     
    getAttribute(String):
     
    Object
     获取节点
     
    getAttributeNames():
     
    Enumeration<String>
     获取节点名称
     
    setAttribute(String, Object): 
     
     void
     设置节点
     
    removeAttribute(String):
     
    void
     移除节点
     
    invalidate(): 
     
    void
    注销
     
    isNew():
     
    boolean
     是否新

    常用的方法

    1、通过request请求获取Session对象

    HttpSession session = request.getSession();

    2、必须以键值对的形式往Session存储数据

    session.setAttribute("username", "广涛");

    3、获取、删除Session

    session.getAttribute("username")       // 获取数据
    session.removeAttribute("username");     // 删除数据

    4、获取Session的id

    String sessionId = session.getId();

    5、判断Session是否为新创建

    if (session.isNew()) {
        resp.getWriter().write("session创建成功,ID:" + sessionId);
    } else {
        resp.getWriter().write("session已经在服务器中存在了,ID:" + sessionId);
    }

    使用session:

    package com.guangtao;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.io.IOException;
    
    public class sessionDemo1 extends HttpServlet{
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            // 解决乱码问题
            req.setCharacterEncoding("GBK");
            resp.setCharacterEncoding("GBK");
    HttpSession session = req.getSession(); session.setAttribute("username", "广涛"); String sessionId = session.getId(); if (session.isNew()){ resp.getWriter().write("session创建成功,ID:" + sessionId); }else { resp.getWriter().write("session已经在服务器中存在了,ID:" + sessionId); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }

    设置Session的有效期: 使用HttpSession的invalidate方法来设置失效时间

    session.setMaxInactiveInterval(24*60*60);  // 单位为秒
  • 相关阅读:
    Hierarchy Query (Connect by) and ORA600 ([kkqcbydrv:1])
    The Steps to Create a New Oracle Database
    Change Schema Name (II)
    [转]The differences between V$UNDOSTAT and V$ROLLSTAT
    【Oracle Mgmt】Oracle Character Semantics (NLS_LENGTH_SEMANTICS) and etc...
    [Oracle Mgmt]About Oracle Password File
    Show parameter & Table Not exists
    RMAN Recovery Window and Redundancy Concept
    [PLSQL]Are you sure it will be definitely random? (DBMS_RANDOM.SEED)
    IOT, Secondary Index and Mapping Table
  • 原文地址:https://www.cnblogs.com/tisnk/p/14231817.html
Copyright © 2020-2023  润新知