web session 原理1
原理
我们都知道,浏览器无状态的。浏览器是操作不了session的,浏览器能够做的只是传递cookie,每次都传递。 把当前主机下的,和当前请求相同域下的cookie 传递到服务器去,只要cookie没过时。
当我们第一次访问某个web 应用的时候,比如 http://localhost:8080/, 且假设之前没有访问过,那么 当前浏览器是不会存在 http://localhost:8080/ 网站的 session cookie 的,那么, 我们的请求头就不会包含 类似于: Cookie:JSESSIONID=C0EC2D80C7DE37C26013EF3FB4ED8B2E 这样的请求头cookie。 然后, 必然的,web 服务器会返回来一个类似这样的请求头:
Set-Cookie:JSESSIONID=9BAE417668B911F21FBBC2BD909B36A8; Path=/; HttpOnly
浏览器会识别Set-Cookie, 然后读取其内容,然后添加到浏览器 对应域名下 cookie池中去。 之后,每次都会携带这个JSESSIONID的cookie。 直到服务器端对当前session进行了invalidate 操作,或者 当前session 超时,那么, 它就失效了(注意session 失效了,但是 当前域下面的其他 cookie 却不会失效的,可以说失效的cookie 仅仅是JSESSIONID )。 然后,我们再次发送http请求, web 服务器又会给我们分配新的 session, 也就是再次响应Set-Cookie:xxx 。
做个测试。
提供两个servlet, 第一个给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; /**
* Created by luokai on 2017/10/15.
*/
public class SessionOperationServlet extends HttpServlet{ @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
} @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.setAttribute("user", "lk"); System.out.println("new session isNew = " + session.isNew());
System.out.println("new session getMaxInactiveInterval = " + session.getMaxInactiveInterval()); // session 是不需要login 的,但是可以被 设置为失效。
}
}
第二个让session失效:
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration; /**
* Created by luokai on 2017/10/15.
*
* 这里的Logout ,其实就是 invalidate 操作
*
*/
public class SessionLogoutServlet extends HttpServlet{ @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
} @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
// request.getSession(true); 这样会新建 session
System.out.println("session has attr = " + session.getAttributeNames().hasMoreElements());
if (session.getAttributeNames().hasMoreElements()) {
dumpSession(session);
}
System.out.println("session id = " + session.getId());
session.invalidate();
session = request.getSession();
System.out.println("new session isNew = " + session.isNew());// invalidate后的第一次request.getSession(), 那么session.isNew() 为true
System.out.println("new session getMaxInactiveInterval = " + session.getMaxInactiveInterval());
System.out.println("new session has attr = " + session.getAttributeNames().hasMoreElements());
System.out.println("new session id = " + session.getId());
} private static void dumpSession(HttpSession session) {
Enumeration<String> attributeNames = session.getAttributeNames();
while (attributeNames.hasMoreElements()) {
String s = attributeNames.nextElement();
System.out.println(s + " = " + session.getAttribute(s));
}
}
}
web.xml配置:
<servlet>
<servlet-name>sessOut</servlet-name>
<servlet-class>SessionLogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sessOut</servlet-name>
<url-pattern>/sessOut</url-pattern>
</servlet-mapping> <servlet>
<servlet-name>sessOper</servlet-name>
<servlet-class>SessionOperationServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sessOper</servlet-name>
<url-pattern>/sessOper</url-pattern>
</servlet-mapping>
先访问 http://localhost:8080/sessOper
然后访问 http://localhost:8080/sessOut
, 我们就可以看到 session是如何起作用了的!
web session 原理1的更多相关文章
- java web Session会话技术(原理图解+功能+与Cookie的区别+基本使用)
java web Session会话技术(原理图解+功能+与Cookie的区别+基本使用) 这是我关于会话技术的第二篇文章,对 Cookie有不了解的兄弟可以点击下方的Cookie跳转 Cookie链 ...
- 聊聊从web session的共享到可扩展缓存设计
先从web session的共享说起 许多系统需要提供7*24小时服务,这类系统肯定需要考虑灾备问题,单台服务器如果宕机可能无法立马恢复使用,这必定影响到服务.这个问题对于系统规模来说,从小到大可 ...
- [转]PHP Session原理分析及使用
之前在一个叫魔法实验室的博客中看过一篇<php session原理彻底分析>的文章,作者从session的使用角度很好阐述了在代码运行过程中,每个环节的变化以及相关参数的设置及作用.本来想 ...
- web基础-web工作原理,http协议,浏览器缓存
1,web工作原理 2,http协议 3,浏览器缓存 4,cookie和session -------------------------------------------------------- ...
- python---cookie模拟登陆和模拟session原理
cookie模拟登陆: import tornado.web class IndexHandler(tornado.web.RequestHandler): def get(self): #self. ...
- web安全原理-文件包含漏洞
前言 起来吃完早饭就开始刷攻防世界的题,一个简单的文件包含题我竟然都做不出来我服了 拿出买的书开始从头学习总结文件包含漏洞! 一.文件包含漏洞 文件包含漏洞 文件包含函数的参数没有经过过滤或者严格的 ...
- atitit. access token是什么??微信平台公众号开发access_token and Web session保持状态机制
atitit. access token是什么??微信平台公众号开发access_token and Web session保持状态机制 1. token机制and session保持状态机制 1 ...
- C/S B/S 及WEB工作原理
一.C/S B/S区别 (http://wenku.baidu.com/link?url=e8bxaqz_lYCXws6TlDRJEq1qsLumNTBhr3Es6eA1ZuhHhq9FZGbVgo ...
- session原理总结
session原理总结 session多服务器共享的方案梳理 session原理 session的工作原理 客户端禁用cookie时session解决方案[转]
随机推荐
- oracle 存储过程、游标参考实例
create or replace procedure INIT_DICT_QUEUECODE(p_queueId int,p_paramType in varchar2,p_queenName in ...
- Java 中统计文件中出现单词的次数练习
统计英文article.txt文件中出现hello这个单词的次数 这个是article.txt文件内容 { hello The Royal Navy is trying hello to play h ...
- 【VSCode】Windows下VSCode编译调试c/c++【更新】
便携版已更新,点此获取便携版 用于cpptools插件的配置文件更新 更新的launch.json // Available variables which can be used inside of ...
- SpringBoot集成RabbitMQ
官方说明:http://www.rabbitmq.com/getstarted.html 什么是MQ? MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.MQ ...
- linux 系统下有sda和hda的硬件设备分别代表什么意思
linux 系统下有sda和hda的硬件设备分别代表什么意思/dev/sda1 # SCSI设备,sda,sdb,sdc,三块盘,1,2,3代表分区(PV)/dev/sda2/dev/sdb1/dev ...
- QTreeWidget的Item点击事件
转载:cw123458945 #!/usr/bin/env python import sys from PyQt4.QtCore import SIGNAL from PyQt4.QtGui imp ...
- [C#][Report]Cry
本文来自:https://wiki.scn.sap.com/wiki/display/BOBJ/Crystal+Reports%2C+Developer+for+Visual+Studio+Downl ...
- 让SH/BAT脚本定位到运行目录的相对位置,实现其脚本可在任意运行目录下被正确执行
让SH/BAT脚本定位到运行目录的相对位置 实现其脚本可在任意运行目录下被正确执行 在Linux下的bash脚本 #!/bin/bash cd `` 在Windows下的BAT脚本 echo off ...
- Zabbix agentd 命令
#zabbix_agentd -p 查看zabbix所有的内置监控项 [root@nod01 zabbix_agentd.d]# zabbix_agentd -pagent.hostname [s|Z ...
- setjmp的跳转
** 问 :goto语句只能在函数内使用,那如果想要在函数内部直接跳到函数外怎么办呢?** ** 答:setjmp跳转 介绍: 举例: #include<stdio.h> #include ...