Java/JSP/Errors

Материал из Java эксперт
Версия от 18:01, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Advanced Dynamic Web Content Generation: form error check

/*
Beginning JavaServer Pages
Vivek Chopra, Jon Eaves, Rupert Jones, Sing Li, John T. Bell
ISBN: 0-7645-7485-X
*/





Building a simple error handling page

<HTML>
    <HEAD>
        <TITLE>Building a simple error handling page.</TITLE>
    </HEAD>
    <BODY>
        <%
            try{
                int value = 1;
                value = value / 0;
            }
            catch (Exception e){
                System.out.println(e.getMessage());
            }
        %>
    </BODY>
</HTML>





Deal with the errors

// generateError.jsp
<%@ page errorPage="processError.jsp" %>    
<%-- Declare the page to send errors to --%>
<%-- After clicking the button, it will call itself again the generate error --%>

<%-- This scriptlet checks a hidden field to see whether or not to throw an exception --%>
<%
  String hiddenField = request.getParameter("hiddenValue");
  if ( hiddenField.equals("error"))
    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="generateError.jsp">
      <INPUT TYPE="HIDDEN" NAME="hiddenValue" VALUE="error">
      <INPUT TYPE="SUBMIT" VALUE="Generate exception!">
    </FORM>
  </BODY>
</HTML>
//Another JSP file: 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>





Error Handling: compile error in JSP page

/*
Beginning JavaServer Pages
Vivek Chopra, Jon Eaves, Rupert Jones, Sing Li, John T. Bell
ISBN: 0-7645-7485-X
*/

<html>
<head>
<title>Error in Scripting Element</title>
</head>
<body>
       <h1>Page with error in scripting element</h1>
<%
int sum= 0;
for { int i=0; i<50; i++ }
  sum = sum + i;
%>
</body>
</html>





Error without handler

//File Name: generateErrorNoHandler.jsp
<%-- This scriptlet checks a hidden field to see whether or not to throw an exception --%>
<%
  String hiddenField = request.getParameter("hiddenValue");
  if ( hiddenField.equals("error"))
    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="generateErrorNoHandler.jsp">
      <INPUT TYPE="HIDDEN" NAME="hiddenValue" VALUE="error">
      <INPUT TYPE="SUBMIT" VALUE="Generate exception!">
    </FORM>
  </BODY>
</HTML>





Jsp Error Bean

JSP error detected

/*
Beginning JavaServer Pages
Vivek Chopra, Jon Eaves, Rupert Jones, Sing Li, John T. Bell
ISBN: 0-7645-7485-X
*/





JSP Error handler

<%@page isErrorPage="true" %>
<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %>
<html>
<head><title>Sorry about the error</title></head>
<body>
<h2>Sorry, We Erred Handling Your Request</h2>
<strong>Here is information about the error:</strong> <br><br>
The servlet name associated with throwing the exception: 
<%-- JSP 2.0 usage only! 
<c:out value="${pageContext.errorData.servletName}" />  --%>
<br><br>
The type of exception: <c:out value=
  "${requestScope[
   \"javax.servlet.jsp.jspException\"].class.name}" />
<br><br>
The request URI:
<%-- JSP 2.0 usage only! 
<c:out value="${pageContext.errorData.requestURI}" />  --%>
<br><br>
The exception message: 
  <c:out value="${pageContext.exception.message}" />
 </body>
</html>





JSP error: no such page

/*
Beginning JavaServer Pages
Vivek Chopra, Jon Eaves, Rupert Jones, Sing Li, John T. Bell
ISBN: 0-7645-7485-X
*/





Page with error in JSP directive and actions

/*
Beginning JavaServer Pages
Vivek Chopra, Jon Eaves, Rupert Jones, Sing Li, John T. Bell
ISBN: 0-7645-7485-X
*/





Simple error testing

//File: index.jsp
<%@ page errorPage="errPage.jsp" %>
<HTML>
<HEAD><TITLE> Simple error testing </TITLE></HEAD>
<BODY>
<%! 
   String nullString = null;
%>
<%-- Intentionally invoking a NullPointerException --%>
The length of the nullString is <%= nullString.length() %>
</BODY>
</HTML>
/////////////////////////////////////////////////////
//File: errPage.jsp
<%@ page isErrorPage="true" %>
Uncaught exception <%= exception %> has been encountered!





Using an Error Page Jsp

//File: index.jsp
<%@ page errorPage="errorPage.jsp" %>
<HTML>
    <HEAD>
        <TITLE>Using an Error Page</TITLE>
    </HEAD>
    <BODY>
        <H1>Using an Error Page</H1>
        <%
            int value = 1;
            value = value / 0;
        %>
    </BODY>
</HTML>

//File: errorPage.jsp
<%@ page isErrorPage="true" %>
<HTML>
    <HEAD>
        <TITLE>An Error Page</TITLE>
    </HEAD>
    <BODY>
        <H1>An Error Page</H1>
        There was an error! Don"t Panic!!!
        <BR>
        Consult your physician immediately.
    </BODY>
</HTML>





Using the Exception Object in Jsp