ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzatz_5.4.0.1/51/program/serverr.htm

88 lines
4.1 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<LINK rel="stylesheet" type="text/css" href="../../../rzahg/ic.css">
<title>Enhanced error reporting</title>
</head>
<BODY>
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h4><a name="serverr"></a>Enhanced error reporting</h4>
<p>A servlet can report errors by performing these actions:</p>
<ul>
<li>Calling the HttpServletResponse.sendError() method.</li>
<li>Throwing an uncaught exception within its service() method.</li>
</ul>
<p>The enhanced servlet error reporting function in WebSphere Application Server - Express provides a different way to implement error reporting. The error page (a JSP file or servlet) is configured for the application and used by all of the servlets in that application. The new mechanism handles caught and uncaught errors.</p>
<p>To return the error JSP file to the client, the Web container performs the following functions:</p>
<ul>
<li>Gets the ServletContext.RequestDispatcher for the URI configured for the Web module error path.</li>
<li>Creates an instance of the error bean (type ServletErrorReport). The bean scope is requested, so that the target servlet (the servlet that encountered the error) can access the detailed error information.</li>
</ul>
<p>For WebSphere Application Server - Express, the HttpServletResponse.sendError() method has been overridden to provide the following function:</p>
<pre> public void sendError(int statusCode, String message) {
ServletException e = new ServletErrorReport(statusCode, message);
request.setAttribute(ServletErrorReport.ATTRIBUTE_NAME, e);
servletContext.getRequestDispatcher(getErrorPath()).forward(request, response);
}</pre>
<p>To enable this function for your Web module, perform these steps:</p>
<ol>
<li>Create the error JSP file.</li>
<li>Place the file in the Web module document root.</li>
<li>Use the WebSphere Development Studio Client to configure an error path for the Web module. For more information, see the WebSphere Development Studio Client Help.</li>
</ol>
<p>To create an error JSP file, you need to know the public methods of the ServletErrorReport class (the error bean), which are:</p>
<pre> public class ServletErrorReport extends ServletException {
// Get the stack trace of the error as a string
public String getStackTrace()
// Get the message associated with the error.
// The same message is sent to the sendError() method.
public String getMessage()
// Get the error code associated with the error.
// The same error code is sent to the sendError() method.
// This will also be the same as the status code of the response.
public int getErrorCode()
// Get the name of the servlet that reported the error
public String getTargetServletName()
}</pre>
<p>The following is an example of an error JSP file:</p>
<pre> &lt;BEAN name=&quot;ErrorReport&quot; type=&quot;com.ibm.websphere.servlet.error.ServletErrorReport&quot;
scope=&quot;request&quot;&gt;&lt;/BEAN&gt;
&lt;html&gt;
&lt;head&gt;&lt;title&gt;ERROR: &lt;%= ErrorReport.getErrorCode() %&gt;&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;
&lt;H1&gt;An error has occurred while processing the servlet named:
&lt;%= ErrorReport.getTargetServletName() %&gt;&lt;/H1&gt;
&lt;B&gt;Message: &lt;/B&gt;&lt;%= ErrorReport.getMessage() %&gt;&lt;BR&gt;
&lt;B&gt;StackTrace: &lt;/B&gt;&lt;%= ErrorReport.getStackTrace() %&gt;&lt;BR&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>WebSphere Application Server - Express provides an optional internal servlet, com.ibm.ws.webcontainer.servlet.DefaultErrorReporter, that makes it easier to use the enhanced error reporting capability. You simply add the DefaultErrorReporter servlet and the error JSP (which you develop) to your Web module. Use the WebSphere Development Studio for iSeries tools to configure the default error page. For more information, see the WebSphere Development Studio Client Help.</p>
</body>
</html>