2 Replies Latest reply: Aug 21, 2013 7:29 AM by Prem RSS

    problem with jsp page

    Prem

      I am very much new to jsp and servlet. I want to set global error page for every exception occured at server side.

      I am trying with following code , but something is going wrong and I am not able to c error page. Please help me out.

       

      package com.web;
      
      
      import java.io.IOException;
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      
      
      /**
       * Servlet implementation class MainController
       */
      public class MainController extends HttpServlet {
        private static final long serialVersionUID = 1L;
             
          /**
           * @see HttpServlet#HttpServlet()
           */
          public MainController() {
              super();
              // TODO Auto-generated constructor stub
          }
      
      
        /**
        * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
        */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
      
      
        }
      
      
        /**
        * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
        */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String s1=null;
        s1.equals("abc");
        }
      
      
      }
      

       

       

       

      <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
          pageEncoding="ISO-8859-1"%>
      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>Test Error Page</title>
      </head>
      <body>
      <%@ page errorPage="ErrorPage.jsp"%>
      
      
      <form action="MainController" method="post">
      <input type="submit"/>
       </form>
      </body>
      </html>
      
      
      
      
      
      

       

       

       

       

       

      <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
          pageEncoding="ISO-8859-1"%>
      <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>Error Page</title>
      </head>
      <body>
      <%@page isErrorPage="true"  %>
      <%=exception%>
      </body>
      </html>
      

       

       

       

      <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
        <display-name>JSPTestWebProject</display-name>
        <welcome-file-list>
          <welcome-file>index.html</welcome-file>
          <welcome-file>index.htm</welcome-file>
          <welcome-file>index.jsp</welcome-file>
          <welcome-file>default.html</welcome-file>
          <welcome-file>default.htm</welcome-file>
          <welcome-file>default.jsp</welcome-file>
        </welcome-file-list>
        <servlet>
          <description></description>
          <display-name>MainController</display-name>
          <servlet-name>MainController</servlet-name>
          <servlet-class>com.web.MainController</servlet-class>
        </servlet>
        <servlet-mapping>
          <servlet-name>MainController</servlet-name>
          <url-pattern>/MainController</url-pattern>
        </servlet-mapping>
        
        <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/Errorpage.jsp</location>  
        </error-page>
      </web-app>
      
        • 1. Re: problem with jsp page
          925716
          1. Raise the exception from the doGet or doPost
          2. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
          3.   String s1=null
          4.   s1.equals("abc"); 
          5. throw new IOException("");
          6.   } 
          7. Keep the directive tags at the start of the jsp page
          8. <%@page isErrorPage="true"  %>  ,
          9. <%@ page errorPage="ErrorPage.jsp"%> 
          10. Try with these
          • 2. Re: problem with jsp page
            Prem

            But why we need to throw it explicitly ?