Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have the <error-page> servlet ignore some requests?

I have an application that uses the feature to serve html pages with some error status codes, such as 404. However some of the requests are things like restful services or images which should return the JSON provided by the restful service, or nothing at all in the case of the image. I'm using Tomcat as the servlet container.

To clarify, there are several servlets which some serve images, and others html, plus a filter which serves html for various paths which can configured by the user. The 404 status generally comes from the lack of a filter or servlet mapped to the requested path. Hence the error-page feature is convenient to serve a response for paths that have no filter or servlet to respond to them.

So what I have in web.xml is (ignore spaces between / and *):

... several other mappings above

<servlet-mapping>
  <servlet-name>RestServlet</servlet-name>
  <url-pattern>/servlet/rest/ *</url-pattern>
</servlet-mapping>

<servlet-mapping>
  <servlet-name>ErrorPageServlet</servlet-name>
  <url-pattern>/servlet/errorPage/ *</url-pattern>
</servlet-mapping>

...

<filter-mapping>
  <!--This will serve content and not forward to the filter chain for certain paths-->
  <filter-name>MainFilter</filter-name>
  <url-pattern>/ *</url-pattern>
</filter-mapping>

...

<error-page>
  <!--Page not found-->
  <error-code>404</error-code>
  <location>/servlet/errorPage/404</location>
</error-page>

Is it possible to have the error page servlet ignore some requests but serve html in the response for others? Or should I use some different method for delivering error pages?

To clarify, what I want is that if someone GETs /foo (which doesn't exist) it goes to the ErrorPageServlet, but for GET /servlet/rest/foo/bar returns 404 response code it doesn't go to the ErrorPageServlet.

like image 978
Sindri Traustason Avatar asked Nov 07 '13 09:11

Sindri Traustason


People also ask

How would you handle exceptions thrown by application with another servlet?

Each error-page element should have either error-code or exception-type element. We define the exception handler servlet in location element. Based on above configuration, if the application throw 404 error or ServletException, it will be handled by AppExceptionHandler servlet.

How do I display error messages in the same JSP page?

To create a JSP error page, we need to set page directive attribute isErrorPage value to true, then we can access exception jsp implicit object in the JSP and use it to send customized error message to the client.

Which two exceptions are thrown by service method of HTTP servlet?

If the web application throws either ServletException or IOException, then the web container invokes the /ErrorHandler servlet. You can define different Error Handlers to handle different type of errors or exceptions.


1 Answers

I finally figured out what the issue is. The restful services were calling HttpServletResponse.sendError() instead of HttpServletResponse.setStatus(). It turns out there is a huge difference between the two, and only sendError() results in the servlet being used.

The solution is simple, use sendError() if you want the error page to handle the output, use setStatus() if you want to write the output in the current servlet.

like image 116
Sindri Traustason Avatar answered Nov 14 '22 22:11

Sindri Traustason