Session相关知识

news/发布时间2024/5/19 23:53:30
  1. 什么是Session

    当用户请求来自应用程序的Web页时,服务器会给每一个用户(浏览器)创建一个Session对象;
    用户在应用程序的Web页之间跳转时,存储在Session对象中的变量将不会丢失,而是在整个用户会话中一直存在下去,默认情况下,只要浏览器没有关闭,这个Session就一直存在。

  2. Session的常用方法

    isNew()//判断是否是新创建的Session,一般在第一次访问的时候出现
    getid()//拿到session的ID
    getCreationTime()//当前session创建的时间
    getLastAccessedTime()//最近的一次访问这个session的时间。
    setAttribute()//设置Session的值
    getAttribute()//获取Session的值
    removeAttribute()//移除Session的值
    invalidate()//手动注销Session
    
  3. Session的应用

    //往session中添加数据
    package com.test.session;
    import javax.servlet.ServletException;
    import javax.servlet.http.*;
    import java.io.IOException;public class SessionDemon01 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//解决乱码问题req.setCharacterEncoding("utf-8");resp.setCharacterEncoding("utf-8");resp.setContentType("text/html;charset=utf-8");//从请求中得到sessionHttpSession session = req.getSession();//往session中添加数据session.setAttribute("name","张三");//得到session的IDString id = session.getId();//判断session是否为新创建的if (session.isNew()){resp.getWriter().write("session创建成功,sessionID为"+id);}else{resp.getWriter().write("session已经在服务器中存在,sessionID为:"+id);}}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
    }//跨Servlet获取Session的值
    public class SessionDemon02 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//解决乱码问题req.setCharacterEncoding("utf-8");resp.setCharacterEncoding("utf-8");resp.setContentType("text/html;charset=utf-8");HttpSession session = req.getSession();//通过键取出值String name = (String) session.getAttribute("name");resp.getWriter().write(name);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
    }
    //注销Session,手动注销
    public class SessionDemon03 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {HttpSession session = req.getSession();session.removeAttribute("name");//手动注销session.invalidate();}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
    }
    //自动注销,在web.xml里加入<session-config>
    <session-config><!--Session一分钟后失效--><session-timeout>1</session-timeout>
    </session-config>
  4. Session和Cookie的区别

    • Cookie是把用户的数据写给用户的浏览器,由浏览器保存,可以保存多个
    • Cookie不是很安全,别人可以分析存放在本地的cookie并进行cookie欺骗
    • Cookie的有效期在cookie生成的时候设置进去
    • Session是在服务端保存的一个数据结构,用来跟踪用户的状态,这个数据可以保存在集群、数据库、文件中
    • Session是把用户的数据写进用户独占的session中,服务器保存,登陆信息等重要信息存放入session
    • 如果主要考虑到安全应当使用session

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ulsteruni.cn/article/66045485.html

如若内容造成侵权/违法违规/事实不符,请联系编程大学网进行投诉反馈email:xxxxxxxx@qq.com,一经查实,立即删除!

相关文章

13.网络编程

1.IP 地址 IP地址:InetAddress import java.net.InetAddress; import java.net.UnknownHostException;//测试IP public class TestInetAddress {public static void main(String[] args) {try {//获取本机地址InetAddress inetAddress1 = InetAddress.getByName("127.0.0…

android开发板USB连接PC后adb口丢失 解决

android开发板USB连接PC后adb口丢失 解决刚开始启动,90DB端口都是有的,屏幕上亮一下就黑了,然后modem端口一闪就没了; 然后 adb shell显示如下: 经确认是硬件modem相关人员修改问题导致的,modem口的导致adb口掉线了~!解决办法是,禁用系统服务里的 WWAN AutoConfig 请…

为什么不建议使用Executors来创建线程池

不建议使用`Executors`类的静态方法(如`newFixedThreadPool`, `newSingleThreadExecutor`, `newCachedThreadPool`等)来创建线程池,主要基于以下几个原因: 1. 隐藏关键配置参数:`Executors`提供的便捷方法通常会隐藏线程池的重要配置参数,比如线程池的大小、工作队列类型…

一站式生活新体验:可视化技术让公寓商场综合楼焕新生

可视化技术将传统的居住与购物空间进行了完美融合。在这里,你不再需要为了购买生活用品而特地跑到远处的商场,也不再需要为了找一家心仪的餐厅而四处奔波。通过可视化技术,你可以轻松查看到楼内的各个商铺、餐厅、健身房等配套设施的分布情况,一键导航直达目的地,享受一站…

01选择排序

01选择排序 1.选择排序含义每次选择最小的,放到左侧。持续进行。2.示例代码: def selectionSort(arr):for i in range(len(arr) - 1):# 记录最小数的索引minIndex = ifor j in range(i + 1, len(arr)):if arr[j] < arr[minIndex]:minIndex = j# i 不是最小数时,将 i 和最…

Jmeter调用java代码

加密:MD5、Base64、SHA、RSA、签名 混合加密: jmeter的md5加密函数:BeanShell 调用java代码: 调用jar包: 1)在测试计划中引入jar包2)调用代码