Java EE应用程序的理想错误页面 [英] Ideal error page for Java EE App

查看:191
本文介绍了Java EE应用程序的理想错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中整理错误时遇到困难。目前,我的 error.jsp 看起来像以下(其中一部分):

I'm having a tough time consolidating errors in my application. Currently, my error.jsp looks like following (part of it):

 <%@ page isErrorPage="true" %>
 <%@page contentType="text/html"%>
 <%@page import="java.util.*"%>
 <%@page import="javax.servlet.*"%>
 <%@page import="javax.servlet.http.*"%>
 <%@page import="java.util.Calendar"%>
 <%@page import="java.text.SimpleDateFormat"%>

<html>
<%
String code = null, message = null, type = null, uri = null, time = null;
Object codeObj=null, messageObj=null, typeObj=null;
if (request.getAttribute("javax.servlet.error.status_code") != null)
    codeObj = request.getAttribute("javax.servlet.error.status_code");
if (request.getAttribute("javax.servlet.error.message") != null)
    messageObj = request.getAttribute("javax.servlet.error.message");
if (request.getAttribute("javax.servlet.error.exception_type")!=null)
    typeObj = request.getAttribute("javax.servlet.error.exception_type");

if (codeObj != null) code = codeObj.toString();
if (messageObj != null) message = messageObj.toString();
if (typeObj != null) type = typeObj.toString();
uri = (String) request.getAttribute("javax.servlet.error.request_uri");
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
time = sdf.format(cal.getTime());

String error = "Code:\t\t" + code + "\nType:\t\t" + type + "\nURL:\t\t" + uri + "\nTime:\t\t" + time +"\nMessage:\t" + message;
%>

在所有场景中都可以正常工作,但!:
有时候在我的应用程序中我正在捕捉 - 在 MyException 类中的例外,包含以下代码:

This works fine in all scenarios except!: Sometimes in my application I am catching built-in exceptions in MyException class with the following code:

catch(MyException ex){
    log.error(ex.getMessage(), uivex);
    String originalURL = "/errorpages/error.jsp?errorcode=" + (ex.getMajor() + ex.getMinor()) + "&errormessage=" + ex.getMessage();
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address);
    dispatcher.forward(request,response);   
}

现在的问题是,当我转发到 error.jsp page ...而不是从 MyException class看到实际的错误。我看到 NullPointerException ,因为 javax.servlet.error.status_code 中没有任何内容,该页面被声明为 isErrorPage =true

Now the problem is that when I get forwarded to error.jsp page...Instead of seeing the actual error from MyException class..I'm seeing NullPointerException because nothing is present in javax.servlet.error.status_code and the page is declared as isErrorPage="true"

在这种情况下该怎么办?一个解决方案是使一个完全不同的error.jsp(名称为error1.jsp)页面,并将异常从 MyException 类转发到该页面。虽然,我想把一切都放在一个地方。

What should I do in this case? One solution is to make a completely different error.jsp (name it error1.jsp) page and forward the exceptions from MyException class to that page. Though, I would like to have everything in one place.

推荐答案

这段代码诚实地伤害我的眼睛。这是一个通用的应该是如何。您可能会发现它很有用。

This code honestly hurts my eyes. Here is how a generic one should look like. You may find it useful.

<%@ page pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<jsp:useBean id="date" class="java.util.Date" />
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Error</title>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <h1>Error</h1>
        <p>Unfortunately an unexpected error has occurred. Below you can find the error details.</p>
        <h2>Details</h2>
        <ul>
            <li>Timestamp: <fmt:formatDate value="${date}" type="both" dateStyle="long" timeStyle="long" />
            <li>Action: <c:out value="${requestScope['javax.servlet.forward.request_uri']}" />
            <li>Exception: <c:out value="${requestScope['javax.servlet.error.exception']}" />
            <li>Message: <c:out value="${requestScope['javax.servlet.error.message']}" />
            <li>Status code: <c:out value="${requestScope['javax.servlet.error.status_code']}" />
            <li>User agent: <c:out value="${header['user-agent']}" />
        </ul>
    </body>
</html>

@page isErrorPage 是顺便一提只有在您想要使用 $ {exception} (即 request.getAttribute(exception)在这种特殊情况下,您不需要它。

The @page isErrorPage is by the way only useful if you want to have the ${exception} (i.e. request.getAttribute("exception") available in the JSP. In this particular case you don't need it.

确实,不要转发 catch 阻止,只是让它走,它将被错误页面处理。

And indeed, do not forward in the catch block at all. Just let it go. It will be dealt by the error page then.

} catch (MyException ex) {
    log.error(ex.getMessage(), uivex);
    throw ex; // Or throw new ServletException(ex.getMessage(), ex);
}

这篇关于Java EE应用程序的理想错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆