Error Controller with java servlets

2019-09-16 10:01发布

How to make a ErrorController, like the ErrorController in the Zend Framework for PHP, with servlets in java?

Now I have this

<servlet>
        <display-name>ErrorController</display-name>
        <servlet-name>ErrorController</servlet-name>
        <servlet-class>project.controller.ErrorController</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>ErrorController</servlet-name>
        <url-pattern>/error</url-pattern>
    </servlet-mapping>

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

Is it possible to have something similar with servlets?

1条回答
放荡不羁爱自由
2楼-- · 2019-09-16 10:15

I am not sure about Zend, but in Java/Servlet you can define error pages for specific error codes (definition goes into WEB-INF/web.xml):

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

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

The location is not necessarily required to be jsp and can then be chewed by a filter which would take user to a relevant controller.

查看更多
登录 后发表回答