Servlet Exception Handling

Advertisements

Prev Tutorial Next Tutorial

Exception Handling in Servlet

The process of converting system error messages into user friendly error message is known as Exception handling. This is one of the powerful feature of Java to handle run time error and maintain normal flow of java application.

Exception

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's Instructions.

  • Programetically Exception Handling mechanism
  • Declarative Exception Handling mechanism

Programetically Exception Handling mechanism

The approach to use try, catch block in java code to handle exceptions is known as Programetically Exception Handling mechanism.

index.html

<form action="servlet1">
Name:<input type="text" name="userName"/> <br/>
<input type="submit" value="continue"/>
</form>

Exception Handling in Servlet

import java.io.*;  
import javax.servlet.*;  
import javax.servlet.http.*;  
  
public class FirstServlet extends HttpServlet {  
  
  public void doPost(HttpServletRequest request, HttpServletResponse response){  
    try{  
  
    response.setContentType("text/html");  
    PrintWriter out = response.getWriter();  
          
    String n=request.getParameter("userName");  
    out.print("Welcome "+n);  
  
    Cookie ck=new Cookie("uname",n);//creating cookie object  
    response.addCookie(ck);//adding cookie in the response  
  
    //creating submit button  
    out.print("<form action='servlet2'>");  
    out.print("<input type='submit' value='continue'>");  
    out.print("</form>");  
          
    out.close();  
  
        }catch(Exception e){System.out.println(e);}  
  }  
} 

Declarative Exception Handling mechanism

The approach to use xml tags in web.xml file to handle the exception is known as declarative exception handling mechanism.

This mechanism is usful if exception are common for mote than one servlet program. In real time application this mechanism is widely use.

error.html

<html>  
<body>  
<p> Oooops....... page not found</p>  
</body>  
</html>  

Myservlet.java

import java.io.*;
import javax.servlet.*;
  
public class FirstServlet extends HttpServlet {  
  
  public void service(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException
  {
   // get request parameter
   // business operation
   String resultvalue="<body bgcolor="cyan" text="red"> <h1> hello word</h1></body>";

   // prepare response
   resp.setContentType("text/html");
   printWriter out=resp.getWriter();

   // send response
   out.print(resultvalue);  
   out.close();  
   }  
} 

web.xml

<web-app>  
<servlet>  
<servlet-name>s1</servlet-name>  
<servlet-class>Myservlet</servlet-class>  
</servlet>  

<error-page>
<exception-type>java.lang.NumberFormateException</exception-type>  
<location>/error.html</location>  

</error-page>  

</web-app>  

Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements