Struts2 action 的 ;jsessionid=xxx 使找不到页面 Bug 的解决 [转帖]

一: jsessionid 简介: 
因为Session默认是需要Cookie支持的 有些客户浏览器是关闭Cookie的, 
这个时候就需要在URL中指定服务器上的jssessionid标识.如果你的web应用带有 jsessionid ,首先你的web应用不安全,而且也不利于 SEO (亦不美观). 

二: Java代码: 

 1 import javax.servlet.*;
 2 import javax.servlet.http.HttpServletRequest;
 3 import javax.servlet.http.HttpServletResponse;
 4 import javax.servlet.http.HttpServletResponseWrapper;
 5 import javax.servlet.http.HttpSession;
 6 import java.io.IOException;
 7 
 8 public class DisableUrlSessionFilter implements Filter {
 9 
10     public void doFilter(ServletRequest request, ServletResponse response,
11             FilterChain chain) throws IOException, ServletException {
12         if (!(request instanceof HttpServletRequest)) {
13             chain.doFilter(request, response);
14             return;
15         }
16 
17         HttpServletRequest httpRequest = (HttpServletRequest) request;
18         HttpServletResponse httpResponse = (HttpServletResponse) response;
19 
20         if (httpRequest.isRequestedSessionIdFromURL()) {
21             HttpSession session = httpRequest.getSession();
22             if (session != null)
23                 session.invalidate();
24         }
25         // wrap response to remove URL encoding
26         HttpServletResponseWrapper wrappedResponse = new HttpServletResponseWrapper(
27                 httpResponse) {
28             @Override
29             public String encodeRedirectUrl(String url) {
30                 return url;
31             }
32 
33             public String encodeRedirectURL(String url) {
34                 return url;
35             }
36 
37             public String encodeUrl(String url) {
38                 return url;
39             }
40 
41             public String encodeURL(String url) {
42                 return url;
43             }
44         };
45         chain.doFilter(request, wrappedResponse);
46     }
47 
48     public void init(FilterConfig config) throws ServletException {
49     }
50 
51     public void destroy() {
52     }
53 }

三: web.xml配置filter: 

 1     <filter>
 2            <filter-name>jsessionid</filter-name>
 3            <filter-class>
 4             xinyu.filter.DisableUrlSessionFilter
 5            </filter-class>
 6         </filter>
 7         <filter-mapping>
 8            <filter-name>jsessionid</filter-name>
 9            <url-pattern>/*</url-pattern>
10         </filter-mapping>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。