Check Page Exception : PageContext « JSP « Java Tutorial






index.xml

<%@ page errorPage="processError.jsp" %>    
<%-- Declare the page to send errors to --%>


<%-- This scriptlet checks a hidden field to see whether or not to throw an exception --%>
<%
  String hiddenField = request.getParameter("hiddenValue");
  if ( hiddenField !=null && !hiddenField.equals(""))
    throw new java.lang.NullPointerException();
%>

<HTML>
  <HEAD><TITLE>Generate Error</TITLE></HEAD>
  <BODY>
    This page generates an error when you click the button.<P>
    <FORM METHOD="POST" ACTION="index.jsp">
      <INPUT TYPE="HIDDEN" NAME="hiddenValue" VALUE="Bang!">
      <INPUT TYPE="SUBMIT" VALUE="Generate exception!">
    </FORM>
  </BODY>
</HTML>

processError.jsp

<%@ page isErrorPage="true" %>

<HTML>
  <HEAD><TITLE>Process Error</TITLE></HEAD>
  <BODY>
    <% if ( exception != null ) {
         out.write("\nAn error occurred. This page is to tell you what you did wrong.\n");
       }
       else {
         out.write("\nYou have reached this page, but no error information is available.\n");
       }
    %>
  </BODY>
</HTML>
  Download:  JSPCheckPageException.zip( 939 k)








23.33.PageContext
23.33.1.Save Value to PageContext
23.33.2.Set PageContext Attribute in JSP Page
23.33.3.Do Calculation on Value Saved in PageContext
23.33.4.Dispatch Page Based on Form Input
23.33.5.Compare Attribute in PageContext
23.33.6.Check Page Exception