I have been throwing ServletException
s in the past, when something/anything goes wrong in a Servlet, mostly just wrapping the exception in ServletException
.
Now I am thinking it is actually better to not throw a ServletException
but to respond with response.sendError(sc)
and use the correct HTTP status codes.
If I can't send an error using reponse.sendError
, (IOException
), I wrap the IOException
in a ServletException
.
Is the above a better way to respond?
When is it OK to just throw a ServletException
?
Constructor Summary Constructs a new servlet exception with the specified message. Constructs a new servlet exception when the servlet needs to throw an exception and include a message about the "root cause" exception that interfered with its normal operation, including a description message.
Class UnavailableException. Defines an exception that a servlet or filter throws to indicate that it is permanently or temporarily unavailable. When a servlet or filter is permanently unavailable, something is wrong with it, and it cannot handle requests until some action is taken.
How constructor can be used for a servlet? Explanation: We cannot declare constructors for interface in Java. This means we cannot enforce this requirement to any class which implements Servlet interface. Also, Servlet requires ServletConfig object for initialization which is created by container.
(b) When the servlet throws the exception, it will automatically be caught by the calling JSP page. (c) The servlet needs to forward the request to the specific error page URL. The exception is passed along as an attribute named "javax. servlet.
When a servlet throws an exception, the web container looks for a match with the thrown exception type in web.xml configurations that employ the exception-type element. To define the invocation of servlets in response to particular errors or HTTP status codes, you’d have to utilize the error-page element in web.xml.
ServletException (String message, Throwable rootCause) Constructs a new servlet exception when the servlet needs to throw an exception and include a message about the "root cause" exception that interfered with its normal operation, including a description message. ServletException (Throwable rootCause)
Constructs a new servlet exception when the servlet needs to throw an exception and include a message about the "root cause" exception that interfered with its normal operation, including a description message.
To define the invocation of servlets in response to particular errors or HTTP status codes, you’d have to utilize the error-page element in web.xml. Exception Handling is the process of transforming system error messages into user-friendly error messages.
I have just come to the opposite conclusion to @alamar. My situation is writing a REST-style servlet to be called by code in an Oracle database, not by humans.
I want to return the HTTP code 400 Bad Request to the caller when the request information is invalid or missing. Throwing a ServletException
causes the container to return 500 Internal Server Error, which states there is something wrong with the server, not with the request.
Here is the solution I adopted.
RequestException
that extends Exception
.new RequestException(message)
.Catch RequestException
in the servlet’s doPost
method and call HttpServletResponse.sendError()
like this:
try {
validateRequest(request);
// Do stuff with a valid request.
} catch (RequestException e) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
}
The message is returned to the caller as it would be with a ServletException
.
It is better to throw a ServletException
.
You can later actually catch this exception with error-page directive in your web.xml
, and make appropriate actions: display a fancy error page to user, send the exception along with the stack trace to developers, write it to logs and so on.
If you just use sendError()
, you can't do that, to various extents.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With