Servlet Exception Handling.ppt

21
Servlet Exception Handling

description

Servlet Exception Handling

Transcript of Servlet Exception Handling.ppt

Page 1: Servlet Exception Handling.ppt

Servlet Exception Handling

Page 2: Servlet Exception Handling.ppt

Servlet - Exception Handling

• When a servlet throws an exception, the web container searches the configurations in web.xml that use the exception-type element for a match with the thrown exception type.

Page 3: Servlet Exception Handling.ppt

• use the error-page element in web.xml to specify the invocation of servlets in response to certain exceptions or HTTP status codes.

Page 4: Servlet Exception Handling.ppt

• web.xml Configuration:• Consider, you have an ErrorHandler servelt

which would be called whenever there is any defined exception or error.

Page 5: Servlet Exception Handling.ppt

• <error-page> • <error-code>404</error-code>• <location>/ErrorHandler</location> • </error-page> • --------------------• <error-page>

Page 6: Servlet Exception Handling.ppt

• <exception-type> javax.servlet.ServletException </exception-type >

• <location>/ErrorHandler2</location> • </error-page>

Page 7: Servlet Exception Handling.ppt

• If you want to have a generic Error Handler for all the exceptions then you should define following error-page instead of defining separate error-page elements for every exception:

Page 8: Servlet Exception Handling.ppt

• <error-page>• <exception-type>java.lang.Throwable

</exception-type > <location>/ErrorHandler</location>

• </error-page>

Page 9: Servlet Exception Handling.ppt

• <error-page>• <error-code>404</error-code>•

<location>exceptions/Page404.jsp</location>• </error-page>

Page 10: Servlet Exception Handling.ppt

• Following are the points to be noted about above web.xml for Exception Handling:

• The servelt ErrorHandler is defined in usual way as any other servlet and configured in web.xml.

Page 11: Servlet Exception Handling.ppt

• If there is any error with status code either 404 ( Not Found) or 403 ( Forbidden ), then ErrorHandler servlet would be called.

• 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. Above example is very much generic and hope it serve the purpose to explain you the basic concept.

Page 12: Servlet Exception Handling.ppt

list of request attributes that an error-handling servlet can access

to analyse the nature of error/exception

• .

Page 13: Servlet Exception Handling.ppt

S.N. Attribute & Description

1javax.servlet.error.status_codeThis attribute give status code which can be stored and analysed after storing in a java.lang.Integer data type.

2

javax.servlet.error.exception_typeThis attribute gives information about exception type which can be stored and analysed after storing in a java.lang.Class data type.

Page 14: Servlet Exception Handling.ppt

3

javax.servlet.error.messageThis attribute gives information exact error message which can be stored and analysed after storing in a java.lang.String data type.

4

javax.servlet.error.request_uriThis attribute gives information about URL calling the servlet and it can be stored and analysed after storing in a java.lang.String data type.

5

javax.servlet.error.exceptionThis attribute gives information the exception raised which can be stored and analysed after storing in a java.lang.Throwable data type.

6javax.servlet.error.servlet_nameThis attribute gives servlet name which can be stored and analysed after storing in a java.lang.String data type.

Page 15: Servlet Exception Handling.ppt

list of HTTP status codes and associated messages that might be returned from the Web Server:

Code: Message: Description:

100

Continue

Only a part of the request has been received by the server, but as long as it has not been rejected, the client should continue with the request

101 Switching Protocols The server switches protocol.

200 OK The request is OK

300

Multiple Choices

A link list. The user can select a link and go to that location. Maximum five addresses

Page 16: Servlet Exception Handling.ppt

400 Bad Request The server did not understand the request

401 Unauthorized The requested page needs a username and a password

402 Payment Required You can not use this code yet

403 Forbidden Access is forbidden to the requested page

404 Not Found The server can not find the requested page.

405 Method Not Allowed The method specified in the request is not allowed.

Page 17: Servlet Exception Handling.ppt

• response.sendError(404, "Need authentication!!!" );

• response.sendError(status “string!!!" );

Page 18: Servlet Exception Handling.ppt

Logging Error Data (2)

• • Use HttpServletResponse object• • Two methods can be used :• 1. sendError (int statusCode)• 2. sendError (int statusCode, String message)• The sendError() methods do two things:• 1. Sets the response status code• 2. “Commits” the response:

Page 19: Servlet Exception Handling.ppt

Example

• servlet1

• try • {• response.sendError(1,"ssss");• }

Page 20: Servlet Exception Handling.ppt

Errorhandler• public void doGet(HttpServletRequest request,HttpServletResponse response)• throws ServletException, IOException• {

• PrintWriter out= response.getWriter();

• Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");

• out.println("<br> EORRO CODE"+(Integer) request.getAttribute("javax.servlet.error.status_code"));• out.println("<br> servlet name"+(String)request.getAttribute("javax.servlet.error.servlet_name"));•

• out.println("<br>request uri"+ (String)request.getAttribute("javax.servlet.error.request_uri"));

• out.println("<br>Exception Type : " +throwable.getClass( ).getName()+"</br></br>");

Page 21: Servlet Exception Handling.ppt

Inside web.xml

• <error-page><error-code>1</error-code>• <location>/ErrorHandler</location>• </error-page>