Presentation is loading. Please wait.

Presentation is loading. Please wait.

Handling Errors in Web Applications

Similar presentations


Presentation on theme: "Handling Errors in Web Applications"— Presentation transcript:

1 Handling Errors in Web Applications

2 HTTP Error Codes An HTTP response sent from the Web server to the client includes a status code, which tells the Web browser if the request was successful or unsuccessful. 400 Bad Request 401 Unauthorized 404 Not Found 405 Method Not Allowed 415 Unsupported Media Type 500 Internal Server Error 501 Not Implemented 503 Service Unavailable

3 Servlet Exceptions In addition to HTTP errors, a Java technology Web application can generate exceptions to indicate a problem with processing the HTTP request. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException { int x = 0, y = 0; try int z = x / y; } catch (ArithmeticException ae) { throw new ServletException(ae);}

4 Using Custom Error Pages
The generic error pages provided by the Web browser (for HTTP error codes) and the Web container (for servlet exceptions) are often ugly and not very informative to the end user. There are two ways to activate an error page within a Web application: Declarative – declare error pages in the deployment descriptor Programmatic – handle the Java technology exceptions directly in the servlet code.

5 Declaring HTTP Error Pages
The error-page element in the deployment descriptor to declare to the Web container that if an HTTP response is being sent back with a particular status code (for example, 404 “File Not Found”), then the HTML of the response is specified by the error page of your choice. It contains two subelements: error-code and location.

6 Example <error-page> <error-code>404 </error-code>
<location>/error/404.html </location> </error-page>

7 Declaring Servlet Exception Error Pages
Using the error-page element, the web container can forward specific exception types to the error page of your choice. The exception-type subelement is used to identify the fully qualified exception class.

8 Example <error-page>
<exception-type> java.lang.NumberFormatException </exception-type> <location>/error/bad_number </location> </error-page>

9 Programmatic Exception Handling
The servlet contains codes to catch all exceptions and handle them directly. To handle exceptions programmatically, all error-prone business logic is wrapped in a try-catch block.


Download ppt "Handling Errors in Web Applications"

Similar presentations


Ads by Google