《Java Servlet Programming》读书笔记_2010.06.15

2010-06-15

Reporting
l  To retrieve a stack trace as a String, we have to jump through a few hoops. We need to let the Exception print to a special PrintWriter built around a ByteArrayOutputStream. That ByteArrayOutputStream can catch the output and convert it to a String.

n  Like: public static String getStackTraceAsString(Throwable t)
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        PrintWriter writer = new PrintWriter(bytes, true);
        t.printStackTrace(writer);
        return bytes.toString();
     }
then: // Return the file
    try {
      ServletUtils.returnFile(file, out);
    } catch (FileNotFoundException e) {
      log(“Could not find file: ” + e.getMessage());
      res.sendError(res.SC_NOT_FOUND);
    } catch (IOException e) {
      log(“Problem sending file”, e);
      res.sendError(res.SC_INTERNAL_SERVER_ERROR,
                            ServletUtils.getStackTraceAsString(e));
    }

 

Exceptions

l  There are some types of exceptions a servlet has no choice but to catch itself.
l  A servlet can propagate to its server only those exceptions that subclass IOException, ServletException, or RuntimeException.
n  The reason has to do with method signatures.
n  The service() method of Servlet declares in its throws clause that it throws IOException an ServletException exceptions.

n  For it to throw and not catch anything else causes a compile-time error.

 

ServletException

l  ServletException is a subclass of java.lang.Exception that is specific to servlets— the class is defined in the javax.servlet package.
l  It has the same constructors as java.lang.Exception:
n  javax.servlet.ServletException()
n  javax.servlet.ServletException(String msg)
l  ServletException can also support a “root cause” Throwable object. This lets ServletException act as a wrapper around any type of exception or error, giving the server a way to know what “root” problem caused the ServletException to be thrown. ServletException’s two additional constructors:
n  javax.servlet.ServletException(Throwable rootCause)
n  javax.servlet.ServletException(String msg, Throwable rootCause)
n  Using the root cause capability you can pass on any underlying exception or error.
l  public Throwable ServletException.getRootCause()
n  The server can retrieve and examine the underlying exception, stack trace and all

n  The call returns null if there is no exception

 

UnavailableException

l  The javax.servlet package defines one subclass of ServletException, Unavailable Exception, although you can, of course, add your own. 
l  This exceptionindicates a servlet is unavailable, either temporarily or permanently.
n  Permanent unavailability means that the servlet instance throwing the UnavailableException cannot recover from the error. The servlet might be misconfigured, or the state of the servlet may be corrupted.
n  A servlet that throws a permanent UnavailableException during request handling will be removed from service, and a new instance will be create to handle requests.
n  Temporary unavailability means the servlet cannot handle requests for some duration due to a systemwide problem.
l  During the unavailability, the server handles requests for the servlet by returning an SC_SERVICE_UNAVAILABLE(503) status code with a Retry-After header telling the client the estimated end time of the unavailability.
l  If a servlet throws a temporary UnavailableException exception during its init() method, the servlet will never enter into service, and the server will attempt to initialize a new instance after the period of unavailability.
l  javax.servlet.UnavailableException(String msg)
n  creates a new exception that indicates the servlet is permanently unavailable, with an explanation given by msg.
l  javax.servlet.UnavailableException(String msg, int seconds)
n  creates a new exception that indicates the servlet is temporarily unavailable, with an explanation given by msg.
n  A well-written servlet should include in the explanation the reason for the problem and any corrective action the server administrator should perform to let the servlet become available. The duration of its unavailability is given by seconds. This time is only an estimate. If no estimate can be made, a nonpositive value may be used.

l  UnavailableException provides the isPermanent() and getUnavailableSeconds() methods to retrieve information about an exception.

 

Configuring Exception Pages

l  The full behavior when a server catches a servlet exception varies based on the server, however a web application can indicate, via its deployment descriptor, a set of error pages for handling particular kinds of exceptions. Like: 
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
    "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">

<web-app>
    <!-- ..... -->
    <error-page>
        <exception-type>javax.servlet.ServletException</exception-type>
        <location>/servlet/ErrorDisplay</location>
    </error-page>
</web-app>
n  This entry indicates that any ServletException thrown to the server should be displayed using the ErrorDisplay servlet.
n  The <exception-type> must be fully qualified with the package name.
n  For dynamic<location> targets, the server makes available two request attributes to describe the thrown exception:
n  javax.servlet.error.exception_type

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值