Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it ok to throw a ServletException from a Servlet?

Tags:

java

servlets

I have been throwing ServletExceptions 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?

like image 711
Raymond Roestenburg Avatar asked May 08 '09 10:05

Raymond Roestenburg


People also ask

What throws a servlet exception?

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.

Which exception is thrown when a servlet is unable to handle?

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?

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.

How can a servlet call a JSP error page?

(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.

What happens when a servlet throws an exception?

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.

What is servletexception string message?

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)

What is the purpose of an exception in servlet constructor?

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.

How to invoke servlets in response to particular errors?

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.


2 Answers

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.

  1. Create a simple exception class RequestException that extends Exception.
  2. Check the validity of the request with method(s) that throw new RequestException(message).
  3. 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.

like image 56
pharsicle Avatar answered Sep 22 '22 19:09

pharsicle


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.

like image 33
alamar Avatar answered Sep 22 '22 19:09

alamar