簡體   English   中英

如何為 JakartaEE/Glassfish 應用程序啟用堆棧跟蹤 output?

[英]How to enable stack trace output for JakartaEE/Glassfish application?

精簡版

使 Glassfish 在其通用500 服務器錯誤頁面中包含堆棧跟蹤的選項是什么:

在此處輸入圖像描述

長版

在 ASP.net 中,您可以通過在web.config xml 文件中設置值來使通用錯誤頁面包含堆棧跟蹤:

web.config

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

這會導致頁面顯示錯誤的堆棧跟蹤:

在此處輸入圖像描述

什么是等效的 JakartaEE/Glassfish 選項?

現在灰屏死機不顯示堆棧跟蹤:

在此處輸入圖像描述

或者

Jakarta-EE 中注冊全局未捕獲異常處理程序的機制是什么,所以我可以自己顯示堆棧跟蹤?

在 ASP.net 中,將 go 放入global.asax文件中,然后輸入Application_Error回調的代碼:

全球.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 等價物是什么?

研究工作

首先,您通過在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>

然后創建一個新的黃屏死機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> &nbsp;&nbsp; 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>&nbsp;<%=htmlEncode(serverInfo)%>; Java Version: <%=htmlEncode(javaVersion)%>

        </font>

    </body>
</html>

Bob 是你的叔叔 - 一個有用的錯誤頁面:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM