消除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);

}
}




[url]http://www.99inf.net/SoftwareDev/Java/34659.htm[/url]更多重定向资料.
附: 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值