2013-04-13 55 views
10

伙计们我正在努力解决在Windows环境中在Tomcat中打开自定义错误页面的问题。我得到了一些解决方案,但没有运气。我尝试了几乎所有的链接,但它不适合我。他们中的一些其他的解决方案部署在Tomcat的,但不是我的Web服务工作错误代码500的Tomcat 7中的自定义错误页面

的web.xml

<welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
    <welcome-file>index.htm</welcome-file> 
    <welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 

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


我从我的Servlet发送错误3.0应用 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

我已将我的error.html放在我的WebService的根目录中。

一些我的JSP页面而不是HTML还我试图

在JSP中的情况下,我使用的链接:

<%@ page isErrorPage="true" %> 
<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>JSP Page</title> 
</head> 
<body> 
    <h1>Error</h1> 
</body> 
</html> 


FYI它是为其他的Tomcat工作正常内置的解决方案,它的工作完美。但在我的Webservice我得到的回应500,但页面打开空白。还有一件事我禁用了我的Internet Explorer 9.0显示错误友好页面仍然响应来自servlet的500。但错误页面将空白。

如果有任何想法或任何关于我的查询的疑问只是让我知道。我需要它尽快。

+0

为什么是Web服务试图在发生错误时处理HTML页面? –

+0

...你的servlet和JSP与Web服务有什么关系?你确定你知道什么是Web服务吗? – eis

+0

,因为我们在我们的项目中使用我们的自定义错误页面 –

回答

18

尝试把下面的代码片段在WEB-INF/web.xml

<error-page> 
    <error-code>500</error-code> 
    <location>/Error.jsp</location> 
</error-page> 

<error-page> 
    <exception-type>java.lang.Exception</exception-type> 
    <location>/Error.jsp</location> 
</error-page> 

在这种情况下Error.jsp处于同一水平WEB-INF目录(而不是里面)。

+0

Harshal非常感谢您的回复但我试过它也Unfortunetly没有运气.. –

+1

这似乎对我有帮助!注意,排序很重要,你需要在异常映射之前进行500映射(如图所示) –

3

同样可以实现编程如果使用嵌入式例如Tomcat这样的:

 Context appContext = ...    
     ErrorPage errorPage = new ErrorPage(); 
     errorPage.setErrorCode(HttpServletResponse.SC_NOT_FOUND); 
     errorPage.setLocation("/my_custom_error_page.html"); 
     appContext.addErrorPage(er);