javaweb的会话跟踪技术

javaweb的会话跟踪技术

Cookie

在这里插入图片描述

图中有三个会话

1、cookie的基本使用

在这里插入图片描述

package com.web.web2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/aServlet")
public class AServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //发送cookie

        //1.创建cookie对象
        Cookie cookie=new Cookie("username","zs");

        //2.发送cookie,response
        resp.addCookie(cookie);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

打开的页面是空白页面,要想看到数据,则在浏览器中找到设置->安全与隐私–>cookie

package com.web.web2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/bServlet")
public class BServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取cookie

        //1.获取cookie数组
        Cookie[] cookies=req.getCookies();

        //2.遍历数组
        //这个增强循环可以靠iter打出来
        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            if ("username".equals(name)){
                //3.获取数据
                String value = cookie.getValue();
                System.out.println(name+":"+value);
                break;
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

打开的页面网址是http://localhost:8080/javaweb/aServlet
后再打开新的页面,网址是http://localhost:8080/javaweb/bServlet
返回控制台看结果

2、Cookie原理

在这里插入图片描述

3、Cookie使用细节

在这里插入图片描述
PS:Tomcat8版本以上可以支持存储中文

package com.web.web2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/aServlet")
public class AServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //发送cookie

        //1.创建cookie对象
        Cookie cookie=new Cookie("username","zs");

        //设置存活时间  1周7天
        cookie.setMaxAge(60*60*24*7);

        //2.发送cookie,response
        resp.addCookie(cookie);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

打开页面后,再关闭,再打开页面http://localhost:8080/javaweb/bServlet
控制台却能显示结果,说明结果已经被保存在浏览器中(可以联想到浏览器中的记住密码)
在这里插入图片描述

Seesion

1、基本使用

在这里插入图片描述

package com.web.web2.session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/ses1")
public class SessionDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //存储到Session中
        //1.获取Session对象
        HttpSession session = req.getSession();
        //2.存储数据
        session.setAttribute("username","zs");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

package com.web.web2.session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/ses2")
public class SessionDemo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //获取数据,从session中
        //1.获取session对象
        HttpSession session = req.getSession();
        //2.获取数据
        Object username = session.getAttribute("username");
        System.out.println(username);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

ses1和ses2各执行一遍,看控制台

2、Session原理

在这里插入图片描述
如何验证使用的同一个session,很简单,在两个文件中都加入System.out.println(session);

3、Session使用细节

在这里插入图片描述
这个销毁配置`webapp.xml文件里配置,单位为分钟,默认是30分钟

package com.web.web2.session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/ses2")
public class SessionDemo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //获取数据,从session中
        //1.获取session对象
        HttpSession session = req.getSession();
        //销毁
        session.invalidate();
        //2.获取数据
        Object username = session.getAttribute("username");
        System.out.println(username);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

小结

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值