Java Tutorial/JSP/Error Page

Материал из Java эксперт
Перейти к: навигация, поиск

Display Error Message In Error Handling Page

index.jsp



<%@ page errorPage="errorPage.jsp" %>
<HTML>
    <HEAD>
        <TITLE>Using the exception Object</TITLE>
    </HEAD>
    <BODY>
        <H1>Using the exception Object</H1>
        <%
            int value = 1;
            value = value / 0;
        %>
    </BODY>
</HTML>





Error page

<%--
  Copyright (c) 2002 by Phil Hanna
  All rights reserved.
  
  You may study, use, modify, and distribute this
  software for any purpose provided that this
  copyright notice appears in all copies.
  
  This software is provided without warranty
  either expressed or implied.
--%>
<%@ page
      errorPage="ErrorPage.jsp"
      import="java.io.*"
      import="java.util.*"
%>
<%
   Enumeration enames;
   Map map;
   String title;
   // Print the request headers
   map = new TreeMap();
   enames = request.getHeaderNames();
   while (enames.hasMoreElements()) {
      String name = (String) enames.nextElement();
      String value = request.getHeader(name);
      map.put(name, value);
   }
   out.println(createTable(map, "Request Headers"));
   // Print the session attributes
   map = new TreeMap();
   enames = session.getAttributeNames();
   while (enames.hasMoreElements()) {
      String name = (String) enames.nextElement();
      String value = "" + session.getAttribute(name);
      map.put(name, value);
   }
   out.println(createTable(map, "Session Attributes"));
%>
<%-- Define a method to create an HTML table --%>
<%!
   private static String createTable(Map map, String title)
   {
      StringBuffer sb = new StringBuffer();
      // Generate the header lines
      sb.append("<table border="1" cellpadding="3">");
      sb.append("<tr>");
      sb.append("<th colspan="2">");
      sb.append(title);
      sb.append("</th>");
      sb.append("</tr>");
      // Generate the table rows
      Iterator imap = map.entrySet().iterator();
      while (imap.hasNext()) {
         Map.Entry entry = (Map.Entry) imap.next();
         String key = (String) entry.getKey();
         String value = (String) entry.getValue();
         sb.append("<tr>");
         sb.append("<td>");
         sb.append(key);
         sb.append("</td>");
         sb.append("<td>");
         sb.append(value);
         sb.append("</td>");
         sb.append("</tr>");
      }
      // Generate the footer lines
      sb.append("</table>");
      // Return the generated HTML
      return sb.toString();
   }
%>





Pass Message to Error Handling Page

index.jsp



<%@ page errorPage="errorPage.jsp" %>
<HTML>
    <HEAD>
        <TITLE>Using request Object Attributes</TITLE>
    </HEAD>
    <BODY>
        <H1>Using request Object Attributes</H1>
        <%
            request.setAttribute("message", "Divide by zero.");
            int value = 1;
            value = value / 0;
        %>
    </BODY>
</HTML>





Provide Error Handling Page

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>