Reputation: 4236
I am creating a simple custom error page for my JSP application that is running on tomcat 7. I am having an issue printing the exception. The exception variable when printed:
<%= exception %>
is always null.
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>
<%@ page isErrorPage="true" import="java.io.*" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>Error Page</title>
</head>
<body>
<div>
<h3>Error</h3>
<h4><b>Exception:</b><br></h4>
<p><i><%= exception %></i><p>
</div>
<footer></footer>
</body>
I also added:
<%@ page errorPage="errorPage.jsp" %>
to all of my pages.
When this runs it just will print null for the exception variable. If I try and change it to
exception.printStackTrace(response.getWriter()
I will get an error that looks like this:
SEVERE: Exception Processing ErrorPage[errorCode=404, location=/error.jsp]
org.apache.jasper.JasperException: /error.jsp (line: 1, column: 32) equal symbol expected
Upvotes: 1
Views: 4372
Reputation: 85779
Use Expression Language instead:
<%@ page isErrorPage="true" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>Error Page</title>
</head>
<body>
<div>
<h3>Error</h3>
<h4><b>Exception:</b><br></h4>
<p><i>${pageContext.exception.message}</i><p>
</div>
<footer></footer>
</body>
</html>
Note that this will print the exception only if it comes from an Exception
when generating the JSP e.g. having a scriptlet code that throws an unhandled Exception
. If it comes from a 400 or 500 HTTP response code, it won't print any text at all.
Upvotes: 1