如何启用Jakartaee/Glassfish应用程序的堆栈跟踪输出?

发布于 2025-02-12 05:10:26 字数 2916 浏览 1 评论 0原文

简短版

制作玻璃鱼的选项是什么选择在其通用 500服务器错误中包含堆栈跟踪 page:

”输入图像描述在这里“

asp.net中的长版本

您可以通过在web.config xml文件中设置一个值来包含堆栈跟踪:

web.config < /strong>:

<configuration>
   <system.web>
      <customErrors mode="Off" />
   </system.web>
</configuration>

这会导致页面显示错误的堆栈跟踪:

“在此处输入图像说明”

什么是等效的Jakartaee/Glassfish选项?

现在死亡的灰色屏幕没有显示堆栈跟踪:

”

情况

jakarta-ee中的机制是什么,以注册全球无人教的例外 处理程序,所以我可以自己显示堆栈跟踪?

在ASP.NET中,您进入您的 global.asax 文件,然后输入application_error回调的代码:

global.asax

    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs

        // Get the exception object.
        Exception exc = Server.GetLastError().GetBaseException();

        WriteCustomYellowScreenOfDeath(Response, exc);

       // Clear the error from the server
       Server.ClearError();
    }

什么是java/是什么Java-ee/jakarta-ee/Glassfish等效?

研究工作

Short Version

What's the option to make Glassfish include a stack trace in its generic 500 Server Error page:

enter image description here

Long Version

In ASP.net you can have the generic error page include a stack trace by setting a value in your web.config xml file:

web.config:

<configuration>
   <system.web>
      <customErrors mode="Off" />
   </system.web>
</configuration>

This causes the page to show the stack trace of the error:

enter image description here

What is the equivalent JakartaEE/Glassfish option?

Right now the gray screen of death doesn't show a stack trace:

enter image description here

Alternatively

What is the mechanism in Jakarta-EE to register a global uncaught exception handler, so i can display a stack trace myself?

In ASP.net you go into your global.asax file, and enter code for the Application_Error callback:

Global.asax:

    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs

        // Get the exception object.
        Exception exc = Server.GetLastError().GetBaseException();

        WriteCustomYellowScreenOfDeath(Response, exc);

       // Clear the error from the server
       Server.ClearError();
    }

What's the Java/Java-EE/Jakarta-EE/Glassfish equivalent?

Research Effort

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终止放荡 2025-02-19 05:10:26

首先,您通过在web.xml中指定它来告诉网络服务器,您想向我们提供一个自定义错误页面:

web.xml

<web-app>
    <!-- Any unhandled (i.e. Throwable) errors, display the page ysod.jsp (rather than the built-in default -->
    <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/ysod.jsp</location>
    </error-page>
</web-app>

然后您创建一个新的new < em>死亡的黄色屏幕 jsp文件:

ysod.jsp

<!DOCTYPE html>
<%@page import="java.io.PrintWriter"%>
<%@page import="java.io.StringWriter"%>
<%@page import="java.io.StringWriter"%>
<%@page isErrorPage="true" %>
<%!
    public String htmlEncode(String s)
    {
        //Java does not have any htmlEncode or escapeHtml (https://stackoverflow.com/a/1400705/12597)
        return s;
    }
%>

<%
    Throwable cause = exception;
    while (cause.getCause() != null)
    {
        cause = cause.getCause();
    }

    String path = "/";
    String message = cause.getMessage(); //e.g ."Division by zero error";
    String exceptionClassName = exception.getClass().getName(); // e.g. "EOverflow";
    String sourceFile = "$(sourceFile:C:\\website\\WebSite\\src\\FrobTheGrobber.java)";
    String sourceLine = "$(sourceLine:619)";
    String serverInfo = getServletContext().getServerInfo().trim(); //e.g. "Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34209"
    String javaVersion = System.getProperty("java.version");

    StringWriter sw = new StringWriter();
    exception.printStackTrace(new PrintWriter(sw));
    String stackTrace = sw.toString();

    StackTraceElement[] stes = exception.getStackTrace();
    if (stes.length > 0)
    {
        StackTraceElement st = stes[0];
        sourceFile = st.getFileName();
        sourceLine = Integer.valueOf(st.getLineNumber()).toString();
    }

%>
<html>
    <head>
        <title><%= htmlEncode(message)%></title>
        <meta name='viewport' content='width=device-width' />
        <style>
         body {
                font-family:'Verdana';
                font-weight:normal;
                font-size: .7em;
                color:black;
            }
         p {
                font-family:'Verdana';
                font-weight:normal;
                color:black;
                margin-top: -5px;
            }
         b {
                font-family:'Verdana';
                font-weight:bold;
                color:black;
                margin-top: -5px;
            }
         H1 {
                font-family:'Verdana';
                font-weight:normal;
                font-size:18pt;
                color:red;
            }
         H2 {
                font-family:'Verdana';
                font-weight:normal;
                font-size:14pt;
                color:maroon;
            }
         pre {
                font-family:'Consolas','Lucida Console',Monospace;
                font-size:11pt;
                margin:0;
                padding:0.5em;
                line-height:14pt;
            }
         .marker {
                font-weight: bold;
                color: black;
                text-decoration: none;
            }
         .version {
                color: gray;
            }
         .error {
                margin-bottom: 10px;
            }
         .expandable {
                text-decoration:underline;
                font-weight:bold;
                color:navy;
                cursor: grab;
            }
         @media screen and (max-width: 639px) {
                pre {
                    width: 440px;
                    overflow: auto;
                    white-space: pre-wrap;
                    word-wrap: break-word;
                }
         }
         @media screen and (max-width: 479px) {
                pre {
                    width: 280px;
                }
         }
        </style>
    </head>

    <body bgcolor='white'>
        <span><H1>Server Error in '<%=htmlEncode(path)%>' Application.<hr width=100% size=1 color=silver></H1>

            <h2> <i><%=htmlEncode(message)%></i> </h2></span>

      <font face='Arial, Helvetica, Geneva, SunSans-Regular, sans-serif'>

      <b> Description: </b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

      <br><br>

        <b> Exception Details: </b><%=htmlEncode(exceptionClassName)%>: <%=htmlEncode(message)%><br><br>
        <br>

<!--
        <b> Source Error: </b>
        <table width=100% bgcolor='#ffffcc'>
            <tr>
                <td>
                    <code><pre>
                    </pre></code>

                </td>
            </tr>
        </table>
-->


        <b> Source File: </b> <%=htmlEncode(sourceFile)%><b>    Line: </b> <%=htmlEncode(sourceLine)%>
        <br><br>

        <b>Stack Trace:</b> <br><br>

        <table width=100% bgcolor='#ffffcc'>
            <tr>
                <td>
                    <code><pre>
<%=htmlEncode(stackTrace)%>
                    </pre></code>

                </td>
            </tr>
        </table>

        <br>

        <hr width=100% size=1 color=silver>

        <b>Version Information:</b> <%=htmlEncode(serverInfo)%>; Java Version: <%=htmlEncode(javaVersion)%>

        </font>

    </body>
</html>

和鲍勃的叔叔 - a 有用错误页面:

First you tell the web-server that you want to us a custom error page by specifying it in your web.xml:

web.xml:

<web-app>
    <!-- Any unhandled (i.e. Throwable) errors, display the page ysod.jsp (rather than the built-in default -->
    <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/ysod.jsp</location>
    </error-page>
</web-app>

And then you create a new Yellow Screen of Death JSP file:

ysod.jsp

<!DOCTYPE html>
<%@page import="java.io.PrintWriter"%>
<%@page import="java.io.StringWriter"%>
<%@page import="java.io.StringWriter"%>
<%@page isErrorPage="true" %>
<%!
    public String htmlEncode(String s)
    {
        //Java does not have any htmlEncode or escapeHtml (https://stackoverflow.com/a/1400705/12597)
        return s;
    }
%>

<%
    Throwable cause = exception;
    while (cause.getCause() != null)
    {
        cause = cause.getCause();
    }

    String path = "/";
    String message = cause.getMessage(); //e.g ."Division by zero error";
    String exceptionClassName = exception.getClass().getName(); // e.g. "EOverflow";
    String sourceFile = "$(sourceFile:C:\\website\\WebSite\\src\\FrobTheGrobber.java)";
    String sourceLine = "$(sourceLine:619)";
    String serverInfo = getServletContext().getServerInfo().trim(); //e.g. "Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34209"
    String javaVersion = System.getProperty("java.version");

    StringWriter sw = new StringWriter();
    exception.printStackTrace(new PrintWriter(sw));
    String stackTrace = sw.toString();

    StackTraceElement[] stes = exception.getStackTrace();
    if (stes.length > 0)
    {
        StackTraceElement st = stes[0];
        sourceFile = st.getFileName();
        sourceLine = Integer.valueOf(st.getLineNumber()).toString();
    }

%>
<html>
    <head>
        <title><%= htmlEncode(message)%></title>
        <meta name='viewport' content='width=device-width' />
        <style>
         body {
                font-family:'Verdana';
                font-weight:normal;
                font-size: .7em;
                color:black;
            }
         p {
                font-family:'Verdana';
                font-weight:normal;
                color:black;
                margin-top: -5px;
            }
         b {
                font-family:'Verdana';
                font-weight:bold;
                color:black;
                margin-top: -5px;
            }
         H1 {
                font-family:'Verdana';
                font-weight:normal;
                font-size:18pt;
                color:red;
            }
         H2 {
                font-family:'Verdana';
                font-weight:normal;
                font-size:14pt;
                color:maroon;
            }
         pre {
                font-family:'Consolas','Lucida Console',Monospace;
                font-size:11pt;
                margin:0;
                padding:0.5em;
                line-height:14pt;
            }
         .marker {
                font-weight: bold;
                color: black;
                text-decoration: none;
            }
         .version {
                color: gray;
            }
         .error {
                margin-bottom: 10px;
            }
         .expandable {
                text-decoration:underline;
                font-weight:bold;
                color:navy;
                cursor: grab;
            }
         @media screen and (max-width: 639px) {
                pre {
                    width: 440px;
                    overflow: auto;
                    white-space: pre-wrap;
                    word-wrap: break-word;
                }
         }
         @media screen and (max-width: 479px) {
                pre {
                    width: 280px;
                }
         }
        </style>
    </head>

    <body bgcolor='white'>
        <span><H1>Server Error in '<%=htmlEncode(path)%>' Application.<hr width=100% size=1 color=silver></H1>

            <h2> <i><%=htmlEncode(message)%></i> </h2></span>

      <font face='Arial, Helvetica, Geneva, SunSans-Regular, sans-serif'>

      <b> Description: </b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

      <br><br>

        <b> Exception Details: </b><%=htmlEncode(exceptionClassName)%>: <%=htmlEncode(message)%><br><br>
        <br>

<!--
        <b> Source Error: </b>
        <table width=100% bgcolor='#ffffcc'>
            <tr>
                <td>
                    <code><pre>
                    </pre></code>

                </td>
            </tr>
        </table>
-->


        <b> Source File: </b> <%=htmlEncode(sourceFile)%><b>    Line: </b> <%=htmlEncode(sourceLine)%>
        <br><br>

        <b>Stack Trace:</b> <br><br>

        <table width=100% bgcolor='#ffffcc'>
            <tr>
                <td>
                    <code><pre>
<%=htmlEncode(stackTrace)%>
                    </pre></code>

                </td>
            </tr>
        </table>

        <br>

        <hr width=100% size=1 color=silver>

        <b>Version Information:</b> <%=htmlEncode(serverInfo)%>; Java Version: <%=htmlEncode(javaVersion)%>

        </font>

    </body>
</html>

And Bob's your uncle - a useful error page:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文