Servlets - Exception Handling


When a servlet throws an exemption, the web container looks through the setups in web.xml that utilization the exception type element for a match with the thrown exemption type.

You would need to utilize the error-page element in web.xml to indicate the invocation of servlets in light of certain exceptions or HTTP status codes.

web.xml Configuration

Consider, you have a ErrorHandler servlet which would be called at whatever point there is any characterized exemption or mistake. Following would be the section made in web.xml.

<!- - servlet definition - > 

<servlet> 

<servlet-name>ErrorHandler</servlet-name> 

<servlet-class>ErrorHandler</servlet-class> 

</servlet> 

<!- - servlet mappings - > 

<servlet-mapping> 

<servlet-name>ErrorHandler</servlet-name> 

<url-pattern>/ErrorHandler</url-pattern> 

</servlet-mapping> 

<!- - mistake code related blunder pages - > 

<error-page> 

<error-code>404</mistake code> 

<location>/ErrorHandler</location> 

</blunder page> 

<error-page> 

<error-code>403</blunder code> 

<location>/ErrorHandler</location> 

</blunder page> 

<!- - exception case type related blunder pages - > 

<error-page> 

<exception-type> 

javax.servlet.ServletException 

</exemption type > 

<location>/ErrorHandler</location> 

</mistake page> 

<error-page> 

<exception-type>java.io.IOException</exemption type > 

<location>/ErrorHandler</location> 

</mistake page>

If you need to have a nonexclusive Error Handler for every one of the exemptions then you ought to characterize following mistake page as opposed to characterizing separate blunder page components for each exception case −

<error-page> 

<exception-type>java.lang.Throwable</exception case type > 

<location>/ErrorHandler</location> 

</mistake page>

Following are the focuses to be noted status above web.xml for Exception Handling −

  • The servlet ErrorHandler is characterized in common path as some other servlet and arranged in web.xml.

  • If there is any blunder 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, at that point the web container conjures the/ErrorHandler servlet.

  • You can characterize diverse Error Handlers to deal with various sort of blunders or exemptions. Above precedent is particularly nonexclusive and trust it fill the need to clarify you the essential concept.

Request Attributes − Errors/Exceptions

Following is the rundown of demand characteristics that a blunder dealing with servlet can access to dissect the idea of mistake/exception.

Sr.No. Attribute & Description
1

javax.servlet.error.status_code

This attribute give status code which can be put away and dissected in the wake of putting away in a java.lang.Integer information type.

2

javax.servlet.error.exception_type

This attribute gives data status exemption type which can be put away and broke down in the wake of putting away in a java.lang.Class information type.

3

javax.servlet.error.message

This trait gives data precise mistake message which can be put away and investigated in the wake of putting away in a java.lang.String information type.

4

javax.servlet.error.request_uri

This trait gives data status URL calling the servlet and it tends to be put away and investigated subsequent to putting away in a java.lang.String information type.

5

javax.servlet.error.exception

This property gives data status the exception case raised, which can be put away and analysed.

6

javax.servlet.error.servlet_name

This quality gives servlet name which can be put away and investigated subsequent to putting away in a java.lang.String information type.

Error Handler Servlet Example

This model would give you basic comprehension of Exception Handling in Servlet, however you can compose progressively modern channel applications utilizing a similar idea −

This model would give you basic comprehension of Exception Handling in Servlet, however you can compose progressively modern channel applications utilizing the equivalent concept:

//Import required java libraries 

import java.io.*; 

import javax.servlet.*; 

import javax.servlet.http.*; 

import java.util.*; 

//Extend HttpServlet class 

public class ErrorHandler extends HttpServlet { 

//Method to deal with GET strategy request. 

public void doGet(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException { 

//Analyze the servlet exception case  

Throwable throwable = (Throwable) 

request.getAttribute("javax.servlet.error.exception"); 

Integer statusCode = (Integer) 

request.getAttribute("javax.servlet.error.status_code"); 

String servletName = (String) 

request.getAttribute("javax.servlet.error.servlet_name");
    
    
    if (servletName == null) { 

servletName = "Unknown"; 

} 

String requestUri = (String) 

request.getAttribute("javax.servlet.error.request_uri"); 

if (requestUri == null) { 

requestUri = "Unknown"; 

} 

//Set reaction content type 

response.setContentType("text/html"); 

PrintWriter out = response.getWriter(); 

String title = "Error/Exception Information"; 

String docType = 

"<!doctype html open \"-/w3c//dtd html 4.0 " + 

"transitional//en\">\n"; 

out.println(docType + 

"<html>\n" + 

"<head><title>" + title + "</title></head>\n" + 

"<body bgcolor = \"#f0f0f0\">\n"); 

if (throwable == null && statusCode == null) { 

out.println("<h2>Error data is missing</h2>"); 

out.println("Please come back to the <a href=\"" + 

response.encodeURL("http://localhost:8080/") + 

"\">Home Page</a>."); 

} else if (statusCode != null) { 

out.println("The status code : " + statusCode); 

} else { 

out.println("<h2>Error information</h2>"); 

out.println("Servlet Name : " + servletName + "</br></br>"); 

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

out.println("The ask for URI: " + requestUri + "<br><br>"); 

out.println("The exception case message: " + throwable.getMessage( )); 

} 

out.println("</body>"); 

out.println("</html>"); 

} 

//Method to deal with POST strategy request. 

public void doPost(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException { 

doGet(request, response); 

} 

}

Compile ErrorHandler.java in regular way and put your class document in /webapps/ROOT/WEB-INF/classes.

Let us include the accompanying design in web.xml to deal with exception cases −

<servlet> 

<servlet-name>ErrorHandler</servlet-name> 

<servlet-class>ErrorHandler</servlet-class> 

</servlet> 

<!- - servlet mappings - > 

<servlet-mapping> 

<servlet-name>ErrorHandler</servlet-name> 

<url-pattern>/ErrorHandler</url-pattern> 

</servlet-mapping> 

<error-page> 

<error-code>404</mistake code> 

<location>/ErrorHandler</location> 

</mistake page> 

<error-page> 

<exception-type>java.lang.Throwable</exception case type > 

<location>/ErrorHandler</location> 

</mistake page>

Now endeavor to utilize a servlet which raise any exception case or type a wrong URL, this would trigger Web Container to call ErrorHandler servlet and show a fitting message as modified. For instance, on the off chance that you type a wrong URL, at that point it would show the accompanying outcome −

The status code : 404 

The above code may not work with some internet browsers. So attempt with Mozilla and Safari and it ought to work.







© Javacodegeeks 2018 -
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3