实现javaWeb网页自定义出错界面

1.需要配置web.xml文件中的信息,注意一点需要把所有会出错的错误码都声明出来,如果不声明出来会没有效果,我这里只声明了网页找不到的错误码404和500错误,其他的错误码可以自己百度搜索

<error-page>
    <error-code>404</error-code>
    <location>/errorInfo.jsp</location>
</error-page>

<error-page>
    <error-code>500</error-code>
    <location>/errorInfo.jsp</location>
</error-page>

2.定义一自己的错误jsp界面或者html界面,我这里是javaWeb的jsp界面,可以直接使用。

<%@page import="java.io.PrintStream"%>
<%@page import="java.io.ByteArrayOutputStream"%>
<%@page import="java.io.IOException"%>
<%@page import="java.io.OutputStream"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<%@page isErrorPage="true"%>
//定义一个方法
<%!
    public static OutputStream getErrorInfo(HttpServletRequest request, Throwable exe) {  
        try{ 
            // 创建一个空的字节流,保存错误信息  
            OutputStream os = new ByteArrayOutputStream();
            PrintStream ps = new PrintStream(os);  
        
            // 收集错误信息  
            ps.println("错误码: " + request.getAttribute("javax.servlet.error.status_code"));   
            ps.println("异常类: " + request.getAttribute("javax.servlet.error.servlet_name"));  
            ps.println("出错页面地址: " + request.getAttribute("javax.servlet.error.request_uri"));  
            ps.println("访问的地址路径: " +   request.getAttribute("javax.servlet.forward.request_uri"));  
            ps.println(); 
             
            Map<String, String[]> map = request.getParameterMap(); 
            
            /* for (String key : map.keySet()) {  
                ps.println("请求中的参数包括:");  
                ps.println(key + "=" + request.getParameter(key));  
                ps.println();  
            }  */ 
              
            /* for (Cookie cookie : request.getCookies()) {  
                ps.println("请求中的 Cookie 包括:");  
                ps.println(cookie.getName() + "=" + cookie.getValue());  
                ps.println();  
            }   */
      
           
            if (exe != null) {   
                ps.println("堆栈信息");  
                exe.printStackTrace(ps);  
                ps.println();  
            }  
  
            return os;   
        } catch (Exception e) {  
            e.printStackTrace();  
            return null;  
        }  
    }%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>错误页面
    code:${requestScope['javax.servlet.error.status_code']}</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">

<style>
body {
    max-width: 600px;
    min-width: 320px;
    margin: 0 auto;
    padding-top: 2%;
}

textarea {
    width: 100%;
    min-height: 300px;
    outline: none;
    border: 1px solid gray;
    padding: 1%;
}

h1 {
    text-align: right;
    color: lightgray;
}

div {
    margin-top: 1%;
}
</style>
</head>
<body>
    <h1>抱 歉……</h1>
    <div style="padding:2% 0;text-indent:2em;">尊敬的用户:你访问的界面出现了一点小错误!如果问题重复出现,请向系统管理员反馈。</div>
    <textarea>
        <%
            out.print(getErrorInfo(request, exception));
        %>
    </textarea>
    <div align="center">
        <a href="index.jsp">回首页</a> | <a href="javascript:history.go(-1);">上一页</a>
    </div>
</body>
</html>

3.最后总结一点,实现此功能 1.需要配置web.xml信息,2.需要将此界面设置为出错界面<%@page isErrorPage="true"%>添加此项即可。

转载于原文页:https://blog.csdn.net/ma18845639852/article/details/69056816

转载于:https://www.cnblogs.com/Nobodys/p/10754920.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值