File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes HttpServletResponse.sendError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "HttpServletResponse.sendError" Watch "HttpServletResponse.sendError" New topic


Mike Landis

Joined: Jun 05, 2003
Posts: 21
Does HttpServletResponse.sendError allways return HTML to client?
I have a third part vendor's MVC framework that uses HttpServletResponse.sendError when an exception occurs. If I have browser client everytgunh is ok.
When I have a WAP client it seems to be so that HttpServletResponse.sendError
is not suitable because it does not return WML/UTF-8 and does not use
error-page tags defined in Web.xml file.
Are there any other ways to invoke errorpage defined to web.xml than HttpServletResponse.sendError when exception occurs? RequestDispatcher's forward is not good because it needs programming and I liked to use container's
automatic errorpage calling mechanism.
Best regards,
Bob Kerfoot
Ranch Hand

Joined: Oct 01, 2000
Posts: 47
You can use the <error-page> element under <web-app> to declaratively define error pages that respond to specific error codes or exception classes. I have included the following explanation from the Java� Servlet Specification Version 2.3:

SRV.9.9.2 Error Pages
To allow developers to customize the appearance of content returned to a wweb client when a servlet generates an error, the deployment descriptor defines a list of error page descriptions. The syntax allows the configuration of resources to be returned by the container either when a servlet sets a status code to indicate an error on the reponse, or if the servlet generates an exception or error that propogates to the container.
If a status code indicating an error is set on the response, the container
consults the list of error page declarations for the web application that use the status-code syntax and attempts a match. If there is a match, the container returns the resource as indicated by the location entry.
A servlet may throw the following exceptions during processing of a request:
Welcome Files
� runtime exceptions or errors
� ServletExceptions or subclasses thereof
� IOExceptions or subclasses thereof
The web application may have declared error pages using the exception-type element. In this case the container matches the exception type by comparing the exception thrown with the list of error-page definitions that use the exceptiontype element. A match results in the container returning the resource indicated in the location entry. The closest match in the class heirarchy wins.
If no error-page declaration containing an exception-type fits using the class-heirarchy match, and the exception thrown is a ServletException or subclass thereof, the container extracts the wrapped exception, as defined by the ServletException.getRootCause method. A second pass is made over the error page declarations, again attempting the match against the error page declarations, but using the wrapped exception instead.
Error-page declarations using the exception-type element in the deployment descriptor must be unique up to the class name of the exception-type. Similarly, error-page declarations using the status-code element must be unique in the deployment descriptor up to the status code.
The error page mechanism described does not intervene when errors occur in servlets invoked using the RequestDispatcher. In this way, a servlet using the RequestDispatcher to call another servlet has the opportunity to handle errors generated in the servlet it calls.
If a servlet generates an error that is not handled by the error page mechanism as described above, the container must ensure the status code of the response is set to status code 500.

The <error-page> element definition from the deployement despcritor's DTD follows:

An example usage of <error-page> from the Java� Servlet Specification
Version 2.3 follows:

Bob Kerfoot
Mike Landis

Joined: Jun 05, 2003
Posts: 21
The problem actually is that every thing works well if the user is
Web client and I'm using JSP pages generating HTML. But if I use the same technique with WAP client (pages that generate WML) it does not work anymore!!! I have checked that wml and utf-8 setting are ok, but it does not work,
I have struggle agains this problen 1,5 weeks. Could WAP Gateway need
something else, or can it fileter something away that errorpages
does not appear to WAP clients?
What a problem...
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
Originally posted by Mike Landis:

I have a third part vendor's MVC framework that uses HttpServletResponse.sendError when an exception occurs. If I have browser client everytgunh is ok.

Am I correct to read between the lines that the page returned to the browser is more friendly than "500: Internal error" -- i.e. that a view is rendered displaying a page with links to your own site, etc?
If so, then I would suspect that the model is already handling the error code. That would mean that you need to figure out how the internal framework is handling it (i.e. is it with an error page directive, as mentioned?) and override that behavior to check what type of client is using the system prior to sending the response.

"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The team, Newsletter 039.
Mike Landis

Joined: Jun 05, 2003
Posts: 21
Yes, when error occurs in Action class, page returned to Web client
is the one that is defined to web.xml file with error-page directive.
If it's WAP client just plain 'Error 500 occured in' comes to
WAP emulator..... not the one define to web.xml.
All contenttype and charsets are set properly in both cases.
I'm using Nokia Nobile Internet Toolkit 4.0 and it's Wap Gateway Simulator.
But ... maybe the most relevant place for the error is in thrid party vendor's MVC Framework. It does something that prevents my own errorpage appearing to WAP emulator when error occurs.
Mike Landis

Joined: Jun 05, 2003
Posts: 21
Yep, found the reason.
I tested same application witg Ericsson's Wap emulator and Nokia's WAP Gateway Simulator and then it started to work properly.
So code was ok but Nokia's Wap emulator contains a bug....
That must be the solution.
jQuery in Action, 3rd edition
subject: HttpServletResponse.sendError
It's not a secret anymore!