Pr.Pg Next Pg

Request Attributes Errors or Exceptions tutorials

  • Following is the list of request attributes that an error-handling servlet can access to analyse the nature of error/exception.

 

S.N.

Attribute

Description

Java type

1

javax.servlet.error.status_code

This attribute give status code which can be stored and analysed after storing in a java.lang.Integer data type.

java.lang.Integer

2

javax.servlet.error.exception_type

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

java.lang.Class

3

javax.servlet.error.message

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

java.lang.String

4

javax.servlet.error.request_uri

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

java.lang.String

5

javax.servlet.error.exception

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

java.lang.Throwable

6

javax.servlet.error.servlet_name

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

java.lang.String


 

 

Example: Servlet program to show exception handling using try..catch block

Sam Sir

//Program name calculate.java

// Program to show exception handling using try..catch block

import javax.servlet.*;

import javax.servlet.http.*;

import java.io. *;

 

public class calculate extends HttpServlet

{

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException

{

PrintWriter pw=res.getWriter();

try

{

int number1=Integer.parseInt(req.getParameter("num1"));

int number2=Integer.parseInt(req.getParameter("num2"));

int sum=number1+number2;

pw.println("<p>Sum of the numbers is&nbsp;&nbsp;"+sum+"<p>");

}

catch(NumberFormatException n)

{

pw.println(n);

}

}

}

 

 

 

 

HTML form for adding 2 number

Sam Sir

<!--add.html-->

<!--Addition UI-->

<html>

<title>exception demo</title>

<body>

<form action = "calculate" method = "get" align="center">

<table border="0">

<tr>

<td>enter first number:</td>

<td><input type = "text" name = "num1" align="center"></td>

</tr>

<tr>

<td>enter second number:</td>

<td><input type = "text" name = "num2" align="center"></td>

</tr>

<tr>

<td><input type = "submit" value = "add" align="center"></td>

</tr>

</table>

</form>

</body>

</html>

Output

Servlet exception demo

Servlet exception demo

Servlet exception demo

 

 

Servlet exception demo

 


 

Example: Servlet program to handle error in servlet

Sam Sir

//Program name ErrorHandle.java

// Program to handle error in servlet

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

// Extend HttpServlet class

public class ErrorHandle extends HttpServlet {

// Method to handle GET method request.

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException

{

// Analyze the servlet exception

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 response content type

response.setContentType("text/html");

 

PrintWriter out = response.getWriter();

String title = "Error/Exception Information";

out.println("<html>\n" +

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

"<body>");

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

out.println("<h2>Error information is missing to see error change the URL&tt;/h2>");

out.println("Please return 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 request URI: " + requestUri +

"<br><br>");

out.println("The exception message: " +

throwable.getMessage( ));

}

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

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

}

}

Output

Exception configure using web.xml

Note: Now try to use a servlet which raise any exception or type a wrong URL, this would trigger Web Container to call ErrorHandler servlet and display an appropriate message as programmed. For example, if you type a wrong URL then it would display the following result:

Exception configure using web.xml

 

Note: Insert this below code in web.xml file before running this program:

<error-page>

<error-code>404</error-code>

<location>/ErrorHandle</location>

</error-page>

<error-page>

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

<location>/ErrorHandle</location>

</error-page>

Pr.Pg border                                              Next Pg