一,HTTP 是一种"无状态"协议,这意味着每次客户端检索网页时,客户端打开一个单独的连接到 Web 服务器,服务器会自动不保留之前客户端请求的任何记录。所以需要跟踪用户的sessionID来判断是否是同一个用户访问。

1,使用cookie保存sessionID,cookie是将服务器需要保存的信息写入到本地的文件夹里保存。可以将session保存到cookie中,对于客户端的后续请求可以使用接收到的 cookie 来识别。

浏览器不支持 cookie,所以建议不要使用这种方式来维持 session 会话。

例:

package com.test;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;public class SessionCookies extends HttpServlet {/*** Constructor of the object.*/public SessionCookies() {super();}/*** Destruction of the servlet. <br>*/public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here
    }/*** The doGet method of the servlet. <br>** This method is called when a form has its tag value method equals to get.* * @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//获得sessionHttpSession ss=request.getSession();String aa=ss.getId();PrintWriter out=response.getWriter();/*out.print(aa);*///将session储存到cookie中Cookie session=new Cookie("session",aa);Cookie cookie=null;Cookie[] cookies=null;//获得cookie中的sessionIDcookies=request.getCookies();for(int i=0;i<cookies.length;i++){cookie=cookies[i];out.print(cookie.getName());}}/*** The doPost method of the servlet. <br>** This method is called when a form has its tag value method equals to post.* * @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println("  <BODY>");out.print("    This is ");out.print(this.getClass());out.println(", using the POST method");out.println("  </BODY>");out.println("</HTML>");out.flush();out.close();}/*** Initialization of the servlet. <br>** @throws ServletException if an error occurs*/public void init() throws ServletException {// Put your code here
    }}

2,使用HttpSession跟踪

Servlet 还提供了 HttpSession 接口,该接口提供了一种跨多个页面请求或访问网站时识别用户以及存储有关用户信息的方式。

例:

package com.test;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;public class SessionUrl extends HttpServlet {/*** Constructor of the object.*/public SessionUrl() {super();}/*** Destruction of the servlet. <br>*/public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here
    }/*** The doGet method of the servlet. <br>** This method is called when a form has its tag value method equals to get.* * @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/Integer typecount=0;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//获取session。无则创建新的sessionHttpSession session=request.getSession(true);PrintWriter out=response.getWriter();//判断是否是新创建的sessionif(session.isNew()){out.print("新的session创建成功");}else{//如果不是新的session则记录这个session访问页面的次数typecount++;out.print(typecount);}}/*** The doPost method of the servlet. <br>** This method is called when a form has its tag value method equals to post.* * @param request the request send by the client to the server* @param response the response send by the server to the client* @throws ServletException if an error occurred* @throws IOException if an error occurred*/public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println("  <BODY>");out.print("    This is ");out.print(this.getClass());out.println(", using the POST method");out.println("  </BODY>");out.println("</HTML>");out.flush();out.close();}/*** Initialization of the servlet. <br>** @throws ServletException if an error occurs*/public void init() throws ServletException {// Put your code here
    }}

转载于:https://www.cnblogs.com/dybe/p/8098285.html

jsp:session的跟踪方式相关推荐

  1. 【JSP HTTP 状态码】【JSP 表单处理】【JSP 过滤器】【JSP Cookie 处理】【JSP Session】【JSP 文件上传】

    JSP HTTP 状态码 HTTP请求与HTTP响应的格式相近,都有着如下结构: 以状态行+CRLF(回车换行)开始 零行或多行头模块+CRLF 一个空行,比如CRLF 可选的消息体比如文件,查询数据 ...

  2. spring-security-学习笔记-02-基于Session的认证方式

    spring-security-学习笔记-02-基于Session的认证方式 文章目录 spring-security-学习笔记-02-基于Session的认证方式 2 基于Session的认证方式 ...

  3. (一)基于Session的认证方式

    创建工程 本案例工程使用maven进行构建,使用SpringMVC.Servlet3.0实现. 创建maven工程 security-springmvc,工程结构如下: 引入如下依赖如下,注意: 1. ...

  4. 获取http请求中的参数控制器给jsp传递数据的方式

    这里写自定义目录标题 获取http请求中的参数 直接参数名获取 通过对象的方式获取 通过Servlet API方式获取 当请求中的参数和方法中参数名不一致 直接在url中获取参数的方式 控制器给jsp ...

  5. php memcache 封装类,PHP 自定义session储存 MEMCACHE 方式类

    自定义session储存 MEMCACHE 方式类 在php.ini配置文件中更改设置 (Registered_save_handlers 有三种方式 files user memcache) ses ...

  6. PHP 自定义session储存 数据库 方式类   高洛峰 细说PHP

    自定义session储存 数据库 方式类 在php.ini配置文件中更改设置 (Registered_save_handlers 有三种方式 files user memcache) session. ...

  7. MyEclipse设置JSP页面默认编码方式

    MyEclipse设置JSP页面默认编码方式以及设置在Java文件中作者.日期等说明 MyEclipse设置JSP页面默认编码方式: windows(窗口)-Preferences(首选项)-MyEc ...

  8. session的存储方式和配置

    Session又称为会话状态,是Web系统中最常用的状态,用于维护和当前浏览器实例相关的一些信息.我们控制用户去权限中经常用到Session来存储用户状态,这篇文章会讲下Session的存储方式.在w ...

  9. tensorflow tf.ConfigProto() (配置tf.Session的运算方式)(allow_soft_placement、inter_op_parallelism_threads等)

    tf.ConfigProto()主要的作用是配置tf.Session的运算方式,比如gpu运算或者cpu运算 log_device_placement=True 设置tf.ConfigProto()中 ...

最新文章

  1. 【数据结构】拓扑排序
  2. MySql数据库查询结果用表格输出PHP代码示例
  3. NLP:词向量与ELMo模型笔记
  4. 算法设计棋盘覆盖问题c语言,棋盘覆盖问题(用分治法求解)
  5. MS CRM 2011——让活动实体在活动菜单中显示
  6. kubernetes1.8.4安装指南 -- 3. 安装docker ce
  7. Redis的3个高级数据结构
  8. Kong-dashboard 安装 启动运行
  9. OSPF的虚链路配置
  10. 【elasticsearch】xpack exporter failed to flush export bulks
  11. 【前沿方案】华为自动驾驶网络解决方案.pdf(附80页pdf下载链接)
  12. mysql集群和memcached_Memcached 高可用集群架构
  13. react根据中文获取拼音_react下将输入的汉字转化为拼音
  14. 使用Setup Factory把java应用打包成安装软件并更改默认安装路径
  15. JS模拟百度文库评分
  16. 计算机科学丛书 图灵,图灵计算机科学丛书
  17. 黄健翔昨天在意大利对澳大利亚的比赛上的解说激情四射
  18. HPUX下虚拟技术IVM的使用整理
  19. 图解:手机控制电脑的软件的使用教程
  20. apk包反编译,签名

热门文章

  1. 面对新的挑战,成为更好的自己--进击的技术er
  2. python如何把矩阵转换为图片_如何将numpy数组转换为(并显示)图片
  3. 9宫格 java_java实现9宫格及九宫格求解的程序.docx
  4. LAZARUS APT利用恶意word文档攻击MAC用户
  5. mysql的联接算法_【MySQL—SQL编程】联接
  6. c语言win32api勾取,第一次用C语言+win32api写窗体应用程序,晕死了
  7. 共享屏幕,录屏的方法
  8. 表格进阶03——简历制作(用表格布局)
  9. Pomodoro Technique
  10. Hazelcast 介绍与使用(整理)