Exceptions in Servlet : Exception « Servlets « Java






Exceptions in Servlet

/*
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
  <servlet>
    <servlet-name>DeliberateException</servlet-name>
    <servlet-class>DeliberateException</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>DeliberateException</servlet-name>
    <url-pattern>/deliberateException</url-pattern>
  </servlet-mapping>
</web-app>
*/

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DeliberateException extends HttpServlet {
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
    throw new NullPointerException();
  }

  public void doGet(HttpServletRequest req, HttpServletResponse res)
      throws IOException, ServletException {
    ServletOutputStream out = res.getOutputStream();
    res.setContentType("text/html");
    out.println("<html><head><title>Exception Thrower</title></head>");
    out.println("<body>You'll never see this!</body></html>");

  }
}
           
       








Related examples in the same category

1.javax.servlet.error.request_uri
2.ForwardException - show forwarding an exception to an error page