zoukankan      html  css  js  c++  java
  • 【jsp】配置错误页面

    1,使用JSP方式

     如果配置是Jsp时,需要把isErrorPage设置为true,

    以及设置 <%@ page language="Java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8" isErrorPage="true"%>

    错误页面:

    <%@page import="java.io.PrintStream"%>  
    <%@page import="java.io.ByteArrayOutputStream"%>  
    <%@ include file="WEB-INF/views/includes/tags.jsp"%>  
    <%@ page language="java" contentType="text/html; charset=UTF-8"  
        pageEncoding="UTF-8" isErrorPage="true"%>  
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    <html>  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    <title>500 服务器内部错误</title>  
    </head>  
    <body>  
     <div class="ui-alert-panel">  
            <h1>服务器内部错误</h1>  
            <p>处理您的请求时发生错误!请确认您通过正确途径操作。</p>  
        </div>  
      <div style="display:none;">  
      <%  //此处输出异常信息  
          exception.printStackTrace();  
      
          ByteArrayOutputStream ostr = new ByteArrayOutputStream();  
          exception.printStackTrace(new PrintStream(ostr));  
          out.print(ostr);  
      %>  
      </div>  
    </body>  
    </html>
    errorPage.jsp

    可以使用这个页面来触发上面的那个错误:

    <!-- 
        errorPage: 填入一个jsp文件, 当出现错误时, 会将错误对象传递至错误页面
     -->
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8" errorPage="errorPage.jsp"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <%
            String a = null;
            a.toString();//制造一个错误
        %>
    </body>
    </html>
    home.jsp

    2,也可以使用配置xml的方式:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
      <display-name>day06_JSPDemo1</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
      </welcome-file-list>
      <!--
          对于项目的异常状态处理
      -->    
    <!-- 默认的错误处理页面 -->  
            <error-page>  
                <error-code>403</error-code>  
                <location>/403.html</location>  
            </error-page>  
            <error-page>  
                <error-code>404</error-code>  
                <location>/404.html</location>  
            </error-page>  
            <!-- 仅仅在调试的时候注视掉,在正式部署的时候不能注释 -->  
            <!-- 这样配置也是可以的,表示发生500错误的时候,转到500.jsp页面处理。 -->  
            <error-page>   
                <error-code>500</error-code>   
                <location>/500.html</location>   
            </error-page>   
              
            <!-- 这样的配置表示如果jsp页面或者servlet发生java.lang.Exception类型(当然包含子类)的异常就会转到500.jsp页面处理。 -->  
            <error-page>   
                <exception-type>java.lang.Exception</exception-type>   
                <location>/500.jsp</location>   
            </error-page>   
              
            <error-page>   
                <exception-type>java.lang.Throwable</exception-type>   
                <location>/500.jsp</location>   
            </error-page>  
            <!--   
            当error-code和exception-type都配置时,exception-type配置的页面优先级高  
            及出现500错误,发生异常Exception时会跳转到500.jsp  
             -->   
    </web-app>
    web.xml

    错误页面也可以自己写,我们也可以引入公益404页面,比如:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>网页不见了~</title>
    <script type="text/javascript" src="//qzonestyle.gtimg.cn/qzone/hybrid/app/404/search_children.js" charset="utf-8" homePageUrl="http://localhost/demo1.jsp" homePageName="回到主页"></script>
    </head>
    <body>
        <%
            //这个设置可以欺骗IE浏览器,让IE浏览器成功显示公益404页面
            response.setStatus(200);
        %>
    </body>
    </html>
    500.jsp

    原文链接:web.xml配置错误页面

  • 相关阅读:
    kingso_sort
    kingso_module
    KINGSO介绍
    kingso
    铁饭碗的含义不是在一个地方永远有饭吃,而是在任何地方都有饭
    立威廉_百度百科
    甜蜜间谍_百度百科
    贝克曼
    报喜鸟集团有限公司_百度百科
    浙江乔治白服饰股份有限公司
  • 原文地址:https://www.cnblogs.com/HDK2016/p/7071940.html
Copyright © 2011-2022 走看看