博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
01_8_session
阅读量:4520 次
发布时间:2019-06-08

本文共 3381 字,大约阅读时间需要 11 分钟。

01_8_session

1. session总结

1.1服务器的一块内存(key-value)

1.2和客户端窗口对应(子窗口)(独一无二)

1.3客户端和服务器有对应的SessionID

1.4客户端服务器端发送SessionID的时候两种方式

  1. cookie(内存cookie)
  2. rewrite URL

1.5浏览器禁掉cookie,就不能使用session(使用cookie实现session

1.6如果想安全的使用session(不论客户端是否禁止cookie),只能使用URL重写(大大增加编程负担),很多网站要求客户端打开cookie

2.例子

2.1ShowSession.java

public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { response.setContentType("text/html; charset=utf-8");PrintWriter out = response.getWriter();String title = "Session Tracking Example";HttpSession session = request.getSession(true);String heading;Integer accessCount = (Integer) session.getAttribute("accessCount");System.out.println(accessCount);if (accessCount == null) {accessCount = new Integer(0);heading = "Welcom, Newcomer";System.out.println(accessCount);} else {heading = "Welcome Back";accessCount = new Integer(accessCount.intValue() + 1);}session.setAttribute("accessCount", accessCount);/*Integer access = (Integer) session.getAttribute("accessCount");System.out.println(access);*/out.println("");out.println("");out.println("  Session追踪");out.println("  ");out.print("

" + heading + "

");out.print("

Information on Your Session:

");out.println("
Info Type Value
Creation Time " + new Date(session.getCreationTime())+ "
Time of Last Access " + new Date(session.getLastAccessedTime()) + "
Number of Previous Accesses " + accessCount+ "
");out.println(" ");out.println("");out.flush();out.close();}

 

 

2.2SessionInfoServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { response.setContentType("text/html; charset=utf-8");PrintWriter out = response.getWriter();HttpSession session = request.getSession(true); out.println("");out.println("");out.println("  Session Info Servlet");out.println("  ");out.print("

Session Information

");out.print("New Session:" + session.isNew());out.println("
Session ID:" + session.getId());out.println("
Session Creation Time:" + new Date(session.getCreationTime()));out.println("
Session Last Accessed Time:" + new Date(session.getLastAccessedTime()));out.println("

Request Information

");out.println("Session ID from Request:" + request.getRequestedSessionId());out.println("
Session ID Via Cookie:" + request.isRequestedSessionIdFromCookie());out.println("
Session ID Via rewritten URL:" + request.isRequestedSessionIdFromURL());out.println("
Valid Sesion ID:" + request.isRequestedSessionIdValid());out.println(" ");out.println("");out.flush();out.close();}

 

 

2.3URLSession.java

public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { response.setContentType("text/html; charset=utf-8");PrintWriter out = response.getWriter();HttpSession session = request.getSession(true);out.println("");out.println("");out.println("  Session 追踪");out.println("  ");out.print("session id:" + session.getId() + "
");out.print("from url:" + request.isRequestedSessionIdFromUrl() + "
");out.print("from cookie:" + request.isRequestedSessionIdFromCookie() + "
");out.println(" test
");out.println(" test
");out.println(" ");out.println("");out.flush();out.close();}

 

转载于:https://www.cnblogs.com/flyback/p/8721669.html

你可能感兴趣的文章
qt 读取xml文件
查看>>
python3之正则表达式
查看>>
Visual Studio提示“无法启动IIS Express Web服务器”的解决方法
查看>>
Java 时间总结
查看>>
JavaScript 累加求和练习
查看>>
jQuery EasyUI 拖放 – 基本的拖动和放置
查看>>
计算累进税类问题
查看>>
ThinkInJava之内部类
查看>>
licode学习之erizo篇--WebrtcConnection
查看>>
动态规划——背包问题汇总
查看>>
iOS 日历提醒 (类似天猫淘宝的 利用代码添加事件到系统日历中)
查看>>
福大软工1816 · 第一次作业 - 准备
查看>>
[原创]浅谈移动互联网创业公司工具类产品
查看>>
composer查看安装情况
查看>>
操作系统概述
查看>>
前端组件,框架,以及模板
查看>>
实现带有getMin的栈
查看>>
这些年正Android - 母亲
查看>>
iOS 10中如何搭建一个语音转文字框架
查看>>
springmvc配置接口返回的数据是json
查看>>