java - How can I use the same error page for multiple error codes in Tomcat? - Stack Overflow

I'm trying to send plain text error messages from a tomcat servlet, so that the responses may be p

I'm trying to send plain text error messages from a tomcat servlet, so that the responses may be presented to the user by the application.

I have the following in my web.xml:

<error-page>
    <error-code>409</error-code>
    <location>/string_error.jsp</location>
</error-page>

And string_error.jsp is the following:

${requestScope['javax.servlet.error.message']}

This successfully gives me plain-text error messages for a 409 response. However, I would like to use this same page for any error in the 400/500 range, without manually specifying a new <error-page> block for each one. I would have assumed that <error-code>*</error-code> would acplish this, but it does not. Does Tomcat provide a mechanism to do this?

I'm trying to send plain text error messages from a tomcat servlet, so that the responses may be presented to the user by the application.

I have the following in my web.xml:

<error-page>
    <error-code>409</error-code>
    <location>/string_error.jsp</location>
</error-page>

And string_error.jsp is the following:

${requestScope['javax.servlet.error.message']}

This successfully gives me plain-text error messages for a 409 response. However, I would like to use this same page for any error in the 400/500 range, without manually specifying a new <error-page> block for each one. I would have assumed that <error-code>*</error-code> would acplish this, but it does not. Does Tomcat provide a mechanism to do this?

Share Improve this question edited Jun 28, 2011 at 21:11 Mike Daniels 8,6422 gold badges34 silver badges44 bronze badges asked Jun 28, 2011 at 21:03 stefanstefan 1,57911 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

If you're using a Servlet 3.0 container, such as Tomcat 7.0, then you can just omit the <error-code> (or <exception-type>) element to make it a global default error page.

<error-page>
    <location>/string_error.jsp</location>
</error-page>

Since Servlet 3.0 those elements are namely optional.

However, if you're not on Servlet 3.0 yet, then you'd have to configure it at the container level. In for example Tomcat 6.0 (which is a Servlet 2.5 container), you'd need to create a custom error report valve class. You can then specify it as errorReportValveClass attribute of the <Host> element in /conf/server.xml file.

For other containers, consult their documentation.

You can add error-page elements by either the error-code or the (java) exception-type in Tomcat. I do not think it can be made general as you have suggested.

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

<error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/someJavaException.html</location>
</error-page>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744036763a4547566.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信