JavaWeb15(Session)

Session详解

Seesion(会话):代表打开浏览器到关闭浏览器这一过程

什么是Session:

  • 服务器会给每个用户(浏览器)创建一个Seesion对象,只要浏览器没关闭,这个Session就有效
  • 用户登录后,网站的所有页面都可以访问(不用再次登录)说明Session保存了用户的一些信息

Session和Cookie的区别:

  • Cookie是把用户的数据写到浏览器中,由浏览器保存(可以保存多个)
  • Session把用户的数据写到Session中,由服务器保存(一般保存重要信息,减少服务器资源的浪费,如保存登录信息,在网站中经常会使用的数据)

Session取值测试:

SessionDemo01

package com.hao.session;

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 SeesionDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        //得到Session
        HttpSession session = req.getSession();
        //获取SessoinID
        String Sessionid = session.getId();
        //判断Session是否是新创建的
        if (session.isNew()){
            resp.getWriter().write("Session是新创建的,ID为:"+Sessionid);
        }else {
            resp.getWriter().write("Session不是新建的,ID为:"+Sessionid);
        }

    }
}

编写映射

<servlet>
        <servlet-name>SessionDemo01</servlet-name>
        <servlet-class>com.hao.session.SeesionDemo01</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>SessionDemo01</servlet-name>
        <url-pattern>/session1</url-pattern>
    </servlet-mapping>

测试访问

在这里插入图片描述

说明在打开浏览器的时候 Session就自动生成了,并且将Session的值 以JSESSION这个标识符赋值给Cookie。

Session也可以保存对象 代码测试

person类

package com.hao.pojo;

public class Person {
    private String name ;
    private int age;
    private String ID;

    public Person() {
    }

    public Person(String name, int age, String ID) {
        this.name = name;
        this.age = age;
        this.ID = ID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    @Override
    public String toString() {
        return super.toString();
    }
}

修改后的SessionDemo01(往Session中存放对象信息)

package com.hao.session;

import com.hao.pojo.Person;

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 SeesionDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        //得到Session
        HttpSession session = req.getSession();
        //给Session中存东西
        session.setAttribute("name",new Person("源浩",20,"4051"));
        //获取SessoinID
        String Sessionid = session.getId();
        //判断Session是否是新创建的
        if (session.isNew()){
            resp.getWriter().write("Session是新创建的,ID为:"+Sessionid);
        }else {
            resp.getWriter().write("Session不是新建的,ID为:"+Sessionid);
        }

    }
}

SessionDemo02

package com.hao.session;

import com.hao.pojo.Person;

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 SessionDemo02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        HttpSession session = req.getSession();
        Person person = (Person) session.getAttribute("name");
        resp.getWriter().write(person.toString());

    }
}

测试访问(先访问session1,存入信息后,再访问session2)

在这里插入图片描述

如何删除Session中的信息与注销Session

        //删除session中的信息
        session.removeAttribute("name");
        //注销session(但是注销后马上会生成一个新的session)
        session.invalidate();

如何设置Session自动注销

在web.xml中设置


<!--    设置session默认失效时间-->
    <session-config>
<!--    单位为分钟-->
        <session-timeout>1</session-timeout>
    </session-config>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值