Tomcat的400错误的处理  2008-10-04   Java  

Tomcat存在一个BUG,如果URL为:http://localhost:8080/a%,则返回空白页面。不论如何配置总是返回空白页面。那么如何实现当发生400错误时返回自己定制的错误页面呢?

重点是修改CoyoteAdapter.java,加粗两行代码修改之后就没问题了,

// %xx decoding of the URL
try {
	req.getURLDecoder().convert(decodedURI, false);
} catch (IOException ioe) {
	res.setStatus(400);
	res.setMessage("Invalid URI: " + ioe.getMessage());
	return true;
}
// Normalization
if (!normalize(req.decodedURI())) {
	res.setStatus(400);
	res.setMessage("Invalid URI");
    return true;
}

编译tomcat源码时,请引入lib\catalina.jar,lib\tomcat-coyote.jar和bin\tomcat-juli.jar三个包。

请将包含CoyoteAdapter类的jar文件置在tomcat\lib下,如果不好用,则将CoyoteAdapter类覆盖到catalina.jar中。

现在当方式400错误时,将改变为返回404错误。
在tomcat/conf/web.xml中配置

<error-page>
    <error-code>404</error-code>
    <location>/error.html</location>
</error-page> 

就可以显示error.html错误页面了,注意需要将error.html放置到ROOT应用下。
tomcat6.0.14,其它版本也存在这样的问题。


 发表评论

(必填)

(必填)

评论(必填,最多字数:100):