消除jsessionid

/*
 * JBoss, Home of Professional Open Source
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jboss.seam.wiki.core.ui;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Startup;
import org.jboss.seam.annotations.intercept.BypassInterceptors;
import org.jboss.seam.annotations.web.Filter;
import org.jboss.seam.web.AbstractFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Removes jsessionid from encoded URLs, see http://jira.jboss.com/jira/browse/JBSEAM-3018
 *
 * More details: http://foreverprecio.us/groovy_tech
 *
 * @author Christian Bauer
 */
@Startup
@Scope(ScopeType.APPLICATION)
@Name("wikiUrlSessionIdFilter")
@BypassInterceptors
@Filter
public class WikiUrlSessionIdFilter extends AbstractFilter {

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {

        if (!(req instanceof HttpServletRequest)) {
            chain.doFilter(req, res);
            return;
        }

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        // Redirect requests with JSESSIONID in URL to clean version (old links bookmarked/stored by bots)
        // This is ONLY triggered if the request did not also contain a JSESSIONID cookie! Which should be fine for bots...
        if (request.isRequestedSessionIdFromURL()) {
            String url = request.getRequestURL()
                         .append(request.getQueryString() != null ? "?"+request.getQueryString() : "")
                         .toString();
            // TODO: The url is clean, at least in Tomcat, which strips out the JSESSIONID path parameter automatically (Jetty does not?!)
            response.setHeader("Location", url);
            response.sendError(HttpServletResponse.SC_MOVED_PERMANENTLY);//发送301错误.新的URL由Location指出,浏览器应自动重定向到新的url. --google更多错误代码.
            return;
        }

        // Prevent rendering of JSESSIONID in URLs for all outgoing links
        HttpServletResponseWrapper wrappedResponse =
            new HttpServletResponseWrapper(response) {
                @Override
                public String encodeRedirectUrl(String url) {
                    return url;
                }

                @Override
                public String encodeRedirectURL(String url) {
                    return url;
                }

                @Override
                public String encodeUrl(String url) {
                    return url;
                }

                @Override
                public String encodeURL(String url) {
                    return url;
                }
            };
        chain.doFilter(req, wrappedResponse);

    }
}




http://www.99inf.net/SoftwareDev/Java/34659.htm更多重定向资料.
附:  tomcat jsessionid 干扰 SEO(搜索引擎最佳化)
  Tomcat jsessionid's interfering with bots and SEO?
Administrator says :-

The Servlet spec provides a mechanism for Servlet containers to manage sessions by rewriting the outgoing url's on a page to include a session id. This is implemented as a path parameter, as opposed to a query parameter (denoted by ;jsessiond rather than ?jsessionid). According some interpretations of the http spec 1.0, this should be ok. Different jsessionids should not make otherwise identical URIs different. But tell that to the web agents out there processing our URIs! Feedburner and bloglines both treat the jsessionid as a core part of the url, the pages on our website looked different (in terms of identity) to them every time they arrived. Google's cache reveals plenty of our URIs with sessionids. Realising this we hurriedly removed jsessionid generation from our foreverprecio.us wedding websites

你可能感兴趣的:(tomcat,jboss,servlet,groovy,seam)