JSP 页面中 jsessioinid 出现在css和js路径中的怪现象

摘要: 用 Spring mvc  开发一个小应用, 用传统的方法配置好了所有的东西, 但在运行时发现样式,js并没有找到,页面并不是真正想要的页面,打开生成的 HTML 页面查看代码居然发现在 js, css 的路径中出现了jsessionid. 真是怪事, 但最终还是能解决

用 Spring mvc 开发一个小应用, 用传统的方法配置好了所有的东西, 但在运行时发现样式,js并没有找到,页面并不是真正想要的页面,打开生成的 HTML 页面查看代码居然发现在 js, css 的路径中出现了jsessionid. 真是怪事, 但最终还是能解决.

1. 简单的 页面


  
  Welcome!
 
  
  
 
  
 
  
  ...



2. Spring mvc 对资源文件的请求, 在 spring-servlet.xml 中配置



3. 生成的 HTML 页面, 居然带了 jsessonid

  
  Welcome!
 
  
 
  
  
  ...



这显然不是我们想要的结果,但如何决绝呢,其实方法也很简单, 有两种方法:
1. 在jsp页面中关掉session : <%@page session="true"%>
2. 采用 ${pageContext.request.contextPath} ,代替 jstl c:url

  
  Welcome!
 
  
 
  
 
  
  ...



产生这种问题的原因: 来源于 stackoverflow:
SESSIONID cookie is created/sent when session is created. Session is created when your code calls request.getSession() or request.getSession(true) for the first time. If you just want get session, but not create it if it doesn't exists, use request.getSession(false) -- this will return you a session or null. In this case, new session is not created, and JSESSIONID cookie is not sent. (This also means that session isn't necessarily created on first request... you and your code is in control when the session is created)

Sessions are per-context:

SRV.7.3 Session Scope

HttpSession objects must be scoped at the application (or servlet context) level. The underlying mechanism, such as the cookie used to establish the session, can be the same for different contexts, but the object referenced, including the attributes in that object, must never be shared between contexts by the container.

(Servlet 2.4 specification)

Update: Every call to JSP page implicitly creates new session if there is no session yet. This can be turned off by session='false' page directive, in which case session variable is not available on JSP page at all.

上一篇: 验证django 用户认证auth模块常见方法使用.
下一篇: Apache 反向代理 IIS 那点折腾事

Avatar

轻舞肥羊 评论于: 2014-02-26

hai shi zheyang de ya .

Avatar

轻舞肥羊 评论于: 2014-02-26

ke bu keyi fa pinglun .

Avatar

轻舞肥羊 评论于: 2014-02-26

55645645
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号