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 ...
随机推荐
- java笔记
ANT概述:http://www.blogjava.net/amigoxie/archive/2007/11/09/159413.html http://baitai.iteye.com/blog/7 ...
- 工欲善其事必先利其器——web调试工具firebug
一.Firebug工具简介 firebug是firefox下的一款开发类插件.firebug集html查看和编辑,JavaScript控制台,网络状况监视器于一体,是开发JavaScript,css, ...
- jdbc java数据库连接 4)PreParedStatement接口 之 区别和例子
Statement 和 PreparedStatement 的区别: 1)语句不同 PreparedStatement需要预编译以及需要参数 2)由于PreparedStatement有缓存区,所以效 ...
- Eclipse InstaSearch搜索词法 (很多并不支持)
1. 中文翻译 http://www.cnblogs.com/xing901022/p/4974977.html 2. 英文原文 http://lucene.apache.org/core/3_0_3 ...
- [bzoj1901][zoj2112][Dynamic Rankings] (整体二分+树状数组 or 动态开点线段树 or 主席树)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- Centos6安装Gitlab
安装参考 https://about.gitlab.com/downloads/ 可以从清华的镜像下载安装包, 注意区分自己用的是哪个发行版 https://mirror.tuna.tsinghua. ...
- 14-前端开发之HTML
1. 什么是HTML? HTML (Hyper Text Mark-up Language) 即超文本标记语言,是用来描述网页的一种语言. 它不是一种编程语言,而是一种标记语言 (markup lan ...
- 关于Advanced Installer 11.0打包软件过程一些记录
1.想要安装后在默认网站下为一个虚拟目录,IIs,Web应用程序,选中虚拟目录是一个Web应用程序.同时修改常规>文件夹,重新指向网站文件所在目录,不然会少一级目录 2.如果想单独的应用程序池, ...
- iOS下的按钮css去除原生样式
IOS环境下的按钮都是经过美化的,但通常我们在设计web app的时候不需要这些看上去老土的样式,所以,去除这些显得很有必要. 下面这句代码就是重置这些样式的: input[type=button]{ ...
- JavaScript 特殊对象 Array-Like Objects 详解
这篇文章拖了有两周,今天来跟大家聊聊 JavaScript 中一类特殊的对象 -> Array-Like Objects. (本文节选自 underscore 源码解读系列文章,完整版请关注 h ...