0

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

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219

1 Answers1

0

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> &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>

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

enter image description here

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219