今天在尝试servlet中的session,在资料上看到,当浏览器关闭后,session会失效,但是当我使用IE和firefox出现了不同的情况。当我使用IE的使用,关闭IE后,session失效了,但是使用firefox,session依然是有用的,请问这个到底是怎么回事?
解决方案 »
- 求高手指点学习java servlet部分内容的方法
- 事物回滚和不提交有和区别?
- jsp如何实现无刷新实时显示已上传的文件数和未上传的文件数,显示已上传的图片缩略图
- hibernate延迟加载问题!!!
- javascript能够操作的客户端对象有什么?
- 数据库链接问题?急呀!!!
- <jsp:forword page="a.jsp"/>为什么不能转到a.jsp?
- 菜鸟求救!--关于Jboss配置
- 用java编写的简单浏览器网页不能正常显示
- 怎样把一个String类型变量里的所有单引号变成两个单引号?
- 如何根据数组生成textfield,并且实现数组数据的修改
- Tomcat正常启动,工程没加载怎么回事
* @author Zhou Hao, Fri, 20 Jul 2012 20:11:14 EST
*
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// This method is used to test the functionality through which session information
// can be retrieved even if a browser has been closed.
// This is achieved by setting related HTTP response header, that it, "Set-Cookie".
// The implementation of getSession() will not specify the age of the cookie (named as JSESSIONID),
// which means that a programmer has to reset the expiration information manually.
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession(false);
if(session !=null){
// if session has been set, just show it.
out.print(session.getId());
}else{
out.print("no session<br/>");
HttpSession session2 = request.getSession();
Collection<String> names = response.getHeaderNames();
for (String name : names) {
if("Set-Cookie".equals(name)){
// Get GMT timezone
TimeZone zone = TimeZone.getTimeZone("GMT");
// For testing purpose, I just set the expiration time as 2 minute after from now on.
Date d = new Date(System.currentTimeMillis()+60*1000);
// Set the date format in Set-Cookie header in accordance with HTTP specification.
// Example: "Set-Cookie ...;Expires=Wed, 09 Jun 2021 10:18:14 GMT".
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
// Set timezone as GMT.
//The SimpleDateFormat will convert the default timezone (in this machine is EST)
// to GMT timezone.
sdf.setTimeZone(zone);
// Format the date.
String date = sdf.format(d);
// Append the expiration information to response header Set-Cookie
response.setHeader(name, response.getHeader(name)+"; Expires="+date);
}
}
}
}