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. vue+element级联选择器对接后台数据

    1.后台接口返回的数据肯定要和级联选择器的数据一致,所以我专门弄个model存放返回的值,如下:/** * @Auther: GGDong * @Date: 2019/4/3 10:30 */@Get ...

  2. 如何在VMware workstation上创建Linux虚拟机

    由于需要使用Linux虚拟机部署Hadoop集群,故在win10系统上安装了VMware workstation 14,现将介绍如何在VMware workstation上创建Linux虚拟机.下面以 ...

  3. Spring+SpringMVC+JDBC实现登录

    Spring+SpringMVC+JDBC实现登录 有一位程序员去相亲的时候,非常礼貌得说自己是一名程序员,并解释自己是做底层架构的,于是女方听到"底层"两个字,就一脸嫌弃:什么时 ...

  4. Prefix.pch文件的用法

    我们知道,每新建立一个工程,比如说HelloWord,在分类SupportingFiles里都会有一个以工程名开头-Prefix.pch结尾的文件,如HelloWord-Prefix.pch.对于这个 ...

  5. [Swift]LeetCode1079. 活字印刷 | Letter Tile Possibilities

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  6. 笔记-迎难而上之Java基础进阶2

    Set集合 import java.util.*; public class HashSetDemo{ public static void main(String[] args){ //Set接口的 ...

  7. 根据经纬度反向地理编译出地址信息(如果报错:Error Domain=kCLErrorDomain Code=8 "(null)")

    注意:Error Domain=kCLErrorDomain Code=8 "(null)" 如果出现这个错误  一定是 经纬度有问题   一定是 经纬度有问题 一定是 经纬度有问 ...

  8. postgresql修改数据库名

    alter database abc rename to cba;

  9. [干货分享] AXURE-整套高保真UI框架和元件组(暗黑风格)

      写在前面 在我们的开发团队里,一般在产品通过策划和需求评审后,在还没开始设计之前,产品经理和美工会一起定一套UI规范. 一方面用于规范整体界面,防止界面开发过程中出现UI不一致性的情况(有时候标准 ...

  10. spring cloud 测试的时候报 BeanCreationNotAllowedException: Error creating bean with name 'eurekaAutoServiceRegistration' 但能正确跑完测试方法

    因为都能正确的跑测试方法,所以我也不太注意它,但是有时候闲得蛋疼就会找一下原因. 具体原因我也说不清,直接丢个连接 https://github.com/spring-cloud/spring-clo ...