Java/JSP/Errors
Содержание
Advanced Dynamic Web Content Generation: form error check
<source lang="java">
/* Beginning JavaServer Pages Vivek Chopra, Jon Eaves, Rupert Jones, Sing Li, John T. Bell ISBN: 0-7645-7485-X
- /
</source>
Building a simple error handling page
<source lang="java">
<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>
</source>
Deal with the errors
<source lang="java">
// 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.
<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> </source>
Error Handling: compile error in JSP page
<source lang="java">
/* 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>
Page with error in scripting element
<% int sum= 0; for { int i=0; i<50; i++ }
sum = sum + i;
%> </body> </html>
</source>
Error without handler
<source lang="java">
//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>
</source>
Jsp Error Bean
JSP error detected
<source lang="java">
/* Beginning JavaServer Pages Vivek Chopra, Jon Eaves, Rupert Jones, Sing Li, John T. Bell ISBN: 0-7645-7485-X
- /
</source>
JSP Error handler
<source lang="java">
<%@page isErrorPage="true" %> <%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %> <html> <head><title>Sorry about the error</title></head> <body>
Sorry, We Erred Handling Your Request
Here is information about the error:
The servlet name associated with throwing the exception:
<%-- JSP 2.0 usage only!
<c:out value="${pageContext.errorData.servletName}" /> --%>
The type of exception: <c:out value=
"${requestScope[ \"javax.servlet.jsp.jspException\"].class.name}" />
The request URI:
<%-- JSP 2.0 usage only!
<c:out value="${pageContext.errorData.requestURI}" /> --%>
The exception message:
<c:out value="${pageContext.exception.message}" /> </body>
</html>
</source>
JSP error: no such page
<source lang="java">
/* Beginning JavaServer Pages Vivek Chopra, Jon Eaves, Rupert Jones, Sing Li, John T. Bell ISBN: 0-7645-7485-X
- /
</source>
Page with error in JSP directive and actions
<source lang="java">
/* Beginning JavaServer Pages Vivek Chopra, Jon Eaves, Rupert Jones, Sing Li, John T. Bell ISBN: 0-7645-7485-X
- /
</source>
Simple error testing
<source lang="java">
//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!
</source>
Using an Error Page Jsp
<source lang="java">
//File: index.jsp <%@ page errorPage="errorPage.jsp" %> <HTML>
<HEAD> <TITLE>Using an Error Page</TITLE> </HEAD> <BODY>
Using an Error Page
<% int value = 1; value = value / 0; %> </BODY>
</HTML>
//File: errorPage.jsp <%@ page isErrorPage="true" %> <HTML>
<HEAD> <TITLE>An Error Page</TITLE> </HEAD> <BODY>
An Error Page
There was an error! Don"t Panic!!!
Consult your physician immediately. </BODY>
</HTML>
</source>