Servlet session
一、session介绍
Session用于保存服务端与客户端“会话”的信息。例如你逛淘宝时添加到购物车中的商品的信息就是保存到Session中。与Cookies不同的是,Session保存在服务端。每个客户端会有一个与之关联的Session,服务器会将Session的ID写到客户端的Cookies或者URL。如果客户端禁止Cookies了,服务器会将ID写到URL中。
二、Session实例
public class ShowSession extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Session Tracking Example";
//得到客户端对应的session对象,true表示如果在服务器端找不到对应的session对象则创建一个
//得到的是一个session引用,实现HttpSession的具体类
HttpSession session = request.getSession(true);
String heading;
// Use getAttribute instead of getValue in version 2.2.
//得到Session内,与accessCount名字所对应的值Integer accessCount = (Integer) session.getAttribute("accessCount");
if (accessCount == null) {
accessCount = new Integer(0);
heading = "Welcome, Newcomer";
} else {
heading = "Welcome Back";
accessCount = new Integer(accessCount.intValue() + 1);
}
// Use setAttribute instead of putValue in version 2.2.
//像Session内放入对象,第一个参数作为key或者称之为名字,与Map差不多
session.setAttribute("accessCount", accessCount);out.println("<html><head><title>Session追踪</title></head>"
+ "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1 ALIGN=\"CENTER\">"
+ heading + "</H1>\n"
+ "<H2>Information on Your Session:</H2>\n"
+ "<TABLE BORDER=1 ALIGN=\"CENTER\">\n"
+ "<TR BGCOLOR=\"#FFAD00\">\n" + " <TH>Info Type<TH>Value\n"
+ "<TR>\n" + " <TD>ID\n" + " <TD>" + session.getId() + "\n"
+ "<TR>\n" + " <TD>Creation Time\n" + " <TD>"
//表示Session对象的创建时间
+ new Date(session.getCreationTime()) + "\n" + "<TR>\n"
+ " <TD>Time of Last Access\n" + " <TD>"
//最后一次访问时间
+ new Date(session.getLastAccessedTime()) + "\n" + "<TR>\n"
+ " <TD>Number of Previous Accesses\n" + " <TD>"
+ accessCount + "\n" + "</TABLE>\n" + "</BODY></HTML>");
}
三、第二个Seesion实例
public class SessionInfoServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException{
HttpSession mySession = request.getSession(true);
response.setContentType("text/html");
//response表示服务端的回应
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Session Info Servlet</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("<H3>Session Information</H3>");
//判断Session是否创建
out.println("New Session: " + mySession.isNew());
//得到Session的ID
out.println("<BR>Session ID: " + mySession.getId());
out.println("<BR>Session Creation Time: " +
new java.util.Date(mySession.getCreationTime()));
out.println("<BR>Session Last Accessed Time: " +
new java.util.Date(mySession.getLastAccessedTime()));
out.println("<H3>Request Information</H3>");
out.println("Session ID from Request: " +
//request代表客户端的请求,获得从客户端带来的ID
request.getRequestedSessionId());
out.println("<BR>Session ID via Cookie: " +
//判断ID号是否从Cookie内取出来的
request.isRequestedSessionIdFromCookie());
out.println("<BR>Session ID via rewritten URL: " +
//判断ID号是否从URL内取出来的
request.isRequestedSessionIdFromURL());
out.println("<BR>Valid Session ID: " +
//判断从客户端取出的ID是否合法
request.isRequestedSessionIdValid());
out.println("</BODY></HTML>");
out.close(); //close output stream
}
四、Session URL重写
public class URLSession extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession(true);
out.println("<html><head><title>Session追踪</title></head>"
+ "<BODY>\n"
+ "session id:" + session.getId() + "</br>"
+ "from url:" + request.isRequestedSessionIdFromURL() + "</br>"
+ "from cookie:" + request.isRequestedSessionIdFromCookie() + "</br>"
//URL重写
+ "<a href=" + response.encodeURL(request.getRequestURL().toString())
+ ">test</a> </br>"
//得到当前访问的Session地址
+ "<a href=" + request.getRequestURI().toString() + ">test</a>"
+ "</BODY></HTML>"); }
五、Session总结
Session是服务器端的一块内存
Session与客户端窗口对应,只要客户端窗口不关就可以访问到,默认失效时间30m,在Tomacat的conf文件夹下的web.xml文件中配置。
<!-- ==================== Default Session Configuration ================= -->
<!-- You can set the default session timeout (in minutes) for all newly -->
<!-- created sessions by modifying the value below. -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
客户端与服务器有对应的SessionID
客户端向服务器端发送SessionID的时候有两种方式:
1、Cookie
2、重写URL
Servlet session的更多相关文章
- JavaWeb学习记录总结(二十九)--Servlet\Session\Cookie\Filter实现自动登录和记住密码
一.Servlet package autologin.servlet.login; import java.io.IOException;import java.security.MessageDi ...
- jsp&servlet——session监听
session监听,需要实现HttpSessionAttributeListener接口 attributeAdded:监听添加session attributeRemoved:监听删除session ...
- Servlet Session 跟踪
HTTP 是一种"无状态"协议,这意味着每次客户端检索网页时,客户端打开一个单独的连接到 Web 服务器,服务器会自动不保留之前客户端请求的任何记录. 但是仍然有以下三种方式来维持 ...
- Servlet session的理解
servlet参见http://blog.csdn.net/bryanliu1982/article/details/5214899 session参见http://lavasoft.blog.51c ...
- Servlet Session MVC模式
一 什么是Session 当首次使用session时,服务器端要创建session,session是保存在服务器端,而给客户端的session的id(一个cookie中保存了sessionId). ...
- Servlet Session的使用
Session 是服务器端会话技术.当浏览器访问 Web 服务器的资源时,服务器可以为每个用户浏览器创建一个 Session 对象,每个浏览器独占一个 Session 对象.由于每个浏览器独占一个 S ...
- servlet session 相关
1.session是server维护的一个变量,如果消除每个session?----这里只做指定key的session删除 1.1.显示调用 废除指定key的session session.remov ...
- 在报表中给session赋值实现报表间参数共享
1. 问题描述 在报表开发工具FineReport中,若有几张不同的报表,每张报表都有一个共同的项可以选择,比如日期时间.我们希望选择了第一张报表的时间之后,其他报表的默认时间都变为第一张报表选择的时 ...
- Using Sessions and Session Persistence---reference
Using Sessions and Session Persistence The following sections describe how to set up and use session ...
随机推荐
- ajax请求成功后新开窗口window.open()被拦截解决方法
ajax 异步请求成功后需要新开窗口打开 url,使用的是 window.open() 方法,但是很可惜被浏览器给拦截了,怎么解决这个问题呢 问题: 前面开发项目时碰到一个问题,ajax 异步请求 ...
- 报表软件JS开发引用HTML DOM的location和document对象
上一次提到,在报表软件FineReport的JavaScript开发中,可以访问并处理的HTML DOM对象有windows.location.document三种.这次就继续介绍后两种,locati ...
- [转]MVC过滤器
本文转自:http://www.cnblogs.com/HopeGi/p/3342083.html APS.NET MVC中(以下简称“MVC”)的每一个请求,都会分配给相应的控制器和对应的行为方法去 ...
- opencv_haar分类器的训练
本文为作者原创,未经允许不得转载:原文由作者发表在博客园: http://www.cnblogs.com/panxiaochun/p/5345412.html 因为工作的原因,本人需要用到分类器来检测 ...
- 1.认识EJB
什么是EJB?1. Enterprice JavaBeans(EJB)是一个用于分布式业务应用的标准服务端组件模型. . 采用EJB架构编写的应用是可伸的.事务性的.多用户安全的. . 采用EJB编写 ...
- return false
”return false“到底做了什么? 当你每次调用”return false“的时候,它实际上做了3件事情: •event.preventDefault();•event.stopPropaga ...
- ELK日志系统:Elasticsearch + Logstash + Kibana 搭建教程
环境:OS X 10.10.5 + JDK 1.8 步骤: 一.下载ELK的三大组件 Elasticsearch下载地址: https://www.elastic.co/downloads/elast ...
- silverlight 4.0 的oob模式下,调用com通过wmi重启自身进程 killself
silverlight目前开发的应用,想做到系统内注销后自动重新启动下 sllauncher.exe ,实现方式是通过WMI的COM接口,获取到当前应用的执行命令行(CommandLine):并通过s ...
- 关于c++类的内存分配
参考:这里 虽然有些地方错了,但是也可以一看,大概能加深对c++类相关的内存分配的了解 然后这还不算十分深入,更深入的可以看这里. 这本书是时候读一下了:<深度探索C++对象模型> (待续 ...
- 移动web开发—页面头部 META 总结
meta指元素可提供有关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词. 标签位于文档的头部,不包含任何内容. 标签的属性定义了与文档相关联的名称/值对. ...