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("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");

out.println("<HTML>");

out.println("  <HEAD><TITLE>Session追踪</TITLE></HEAD>");

out.println("  <BODY>");

out.print("<H1 ALIGN=\"CENTER\">" + heading + "</H1>");

out.print("<H2 ALIGN=\"CENTER\">Information on Your Session:</H2>");

out.println("<TABLE BORDER=\"1\" ALIGN=\"CENTER\"><TR><TH>Info Type</TH><TH>Value</TH></TR><TR><TD>Creation Time</TD><TD>" + new Date(session.getCreationTime())+ "</TD></TR><TR><TD>Time of Last Access</TD><TD>" + new Date(session.getLastAccessedTime()) + "</TD></TR><TR><TD>Number of Previous Accesses</TD><TD>" + accessCount+ "</TD></TR></TABLE>");

out.println("  </BODY>");

out.println("</HTML>");

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("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");

out.println("<HTML>");

out.println("  <HEAD><TITLE>Session Info Servlet</TITLE></HEAD>");

out.println("  <BODY>");

out.print("<H3>Session Information</H3>");

out.print("New Session:" + session.isNew());

out.println("<BR/>Session ID:" + session.getId());

out.println("<BR/>Session Creation Time:" + new Date(session.getCreationTime()));

out.println("<BR/>Session Last Accessed Time:" + new Date(session.getLastAccessedTime()));

out.println("<H3>Request Information</H3>");

out.println("Session ID from Request:" + request.getRequestedSessionId());

out.println("<BR/>Session ID Via Cookie:" + request.isRequestedSessionIdFromCookie());

out.println("<BR/>Session ID Via rewritten URL:" + request.isRequestedSessionIdFromURL());

out.println("<BR/>Valid Sesion ID:" + request.isRequestedSessionIdValid());

out.println("  </BODY>");

out.println("</HTML>");

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("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");

out.println("<HTML>");

out.println("  <HEAD><TITLE>Session 追踪</TITLE></HEAD>");

out.println("  <BODY>");

out.print("session id:" + session.getId() + "<br/>");

out.print("from url:" + request.isRequestedSessionIdFromUrl() + "<br/>");

out.print("from cookie:" + request.isRequestedSessionIdFromCookie() + "<br/>");

out.println("<a href=" + response.encodeURL(request.getRequestURL().toString()) + "> test </a><br/>");

out.println("<a href=" + request.getRequestURL().toString() + "> test </a><br/>");

out.println("  </BODY>");

out.println("</HTML>");

out.flush();

out.close();

}

01_8_session的更多相关文章

随机推荐

  1. GoWeb开发_Iris框架讲解(一)

    Golang介绍 Go语言是谷歌推出的一种全新的编程语言,可以在不损失应用程序性能的情况下降低代码的复杂性.谷歌首席软件工程师罗布派克(Rob Pike)说:我们之所以开发Go,是因为过去10多年间软 ...

  2. 51nod1205(johnson)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1205 题意:中文题诶- 思路:johnson模板题 流水作业调 ...

  3. solidity学习笔记

    一 pragam solidity ^0.4.23; contract helloword{ string public  name ="hello"; function getN ...

  4. [Xcode 实际操作]六、媒体与动画-(11)UIView视图卷曲动画的制作

    目录:[Swift]Xcode实际操作 本文将演示UIView视图卷曲动画的制作. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit cla ...

  5. Node.js 内置模块crypto加密模块(5) RSA

    RSA加密算法 写在前面: 了解RSA算法的原理请查看下面的文章 一文搞懂 RSA 算法 来源:简书  作者:somenzz 在使用 Node 进行 RSA 加密之前我们首先需要获取RSA公共和私有密 ...

  6. centos下svnadmin的部署过程

    1.    安装SVN #yum –y install subversion 2.    安装openjdk #yum –y list java* #yum –y install java-1.8.0 ...

  7. 055 Jump Game 跳跃游戏

    给定一个非负整数数组,您最初位于数组的第一个索引处.数组中的每个元素表示您在该位置的最大跳跃长度.确定是否能够到达最后一个索引.示例:A = [2,3,1,1,4],返回 true.A = [3,2, ...

  8. X Samara Regional Intercollegiate Programming Contest DIV2

    http://codeforces.com/gym/101341 其实我觉得这份题很不错的,虽然是div2,但是感觉对我挺有帮助(我比较垃圾0.0),还没补完(做的时候一直蒙逼,要补很多题)先写一点点 ...

  9. MYSQL查询~ 存在一个表而不在另一个表中的数据

    A.B两表,找出ID字段中,存在A表,但是不存在B表的数据.A表总共13w数据,去重后大约3W条数据,B表有2W条数据,且B表的ID字段有索引. 方法一 使用 not in ,容易理解,效率低  ~执 ...

  10. 关于下载文件封装的两个类(Mars)

    首先是文件FileUtils.java package mars.utils; import java.io.File; import java.io.FileOutputStream; import ...