• 12.exception对象


    excepton对象是一个异常对象,当一个页面在运行过程中发生了异常,就产生了这个对象,如果一个jsp页面要应用此对象,就必须把isErrorPage设置为true,否则无法编译。它实际上是java.lang.Throwable的对象,常用方法:

    String getMessage()返回描述异常的消息

    String toString()反水关于异常的简短描述信息

    void printStackTrace()显示异常及其栈轨迹

    Throwable FillInStackTrace()重写异常的执行栈轨迹

    使用:在实际中有时候会出现异常,可以专门写一个jsp来处理

    抛出异常的jsp

    <%@ page language="java" import="java.util.*"
        contentType="text/html; charset=utf-8" errorPage="exception.jsp"%>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://"
                + request.getServerName() + ":" + request.getServerPort()
                + path + "/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
    </head>
    
    <body>
        <h1>抛出异常页面</h1>
        <%
        System.out.println(100/0);
        %>
    </body>
    </html>

    需要在page头那里添加处理异常的jsp,即errorPage="exception.jsp"

    exception.jsp

    <%@ page language="java" import="java.util.*"
        contentType="text/html; charset=utf-8" isErrorPage="true"%>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://"
                + request.getServerName() + ":" + request.getServerPort()
                + path + "/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
    </head>
    
    <body>
        <h1>exception内置对象</h1>
        异常消息为:<%=exception.getMessage() %><br>
        异常的字符串描述:<%=exception.toString() %>
    </body>
    </html>

    需要在头部添加

    isErrorPage="true"

    运行的结果

    
    



  • 相关阅读:
    leetcode 字符串的全排列 All In One
    数据结构栈 All In One
    vscode code snippets not working All In One
    js 使用 canvas 实现一个一笔一画写汉字的效果 All In One
    社保 & 养老金 & 养老保险 All In One
    python 中 lambda 函数
    linux 中vim 中的回退、前进
    python 中统计fasta文件scaffolds平均长度、最长scaffolds、最短scaffolds
    ubuntu 中设置 root登录filezilla
    python中统计基因组所含N碱基总个数
  • 原文地址:https://www.cnblogs.com/caimuqing/p/5776599.html
Copyright © 2020-2023  润新知