Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8481373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:32:41+00:00 2026-06-10T19:32:41+00:00

I know I can put something in the web.xml like this <error-page> <exception-type>java.lang.Throwable</exception-type> <location>/error.jsp</location>

  • 0

I know I can put something in the web.xml like this

<error-page>  
   <exception-type>java.lang.Throwable</exception-type>  
   <location>/error.jsp</location>  
</error-page>

However the jsp page won’t show any contructive information since it won’t get what exactly the exception is. I know we can have different exceptions forwarded to different pages by various exception-type but that’s too much to write in web.xml. I hope one page is enough and another for handling errors like 404.

So how should I pass the exception information to the jsp page? Use session?

The ideal situation might be the page gets the exception info and show some relevant messages about it without revealing the exception to the users. Instead it could log it into a file for future reference. What is the best approach to achieve this? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:32:42+00:00Added an answer on June 10, 2026 at 7:32 pm

    The information about the exception is already available by several request attributes. You can find the names of all those attributes in the RequestDispatcher javadoc:

    • ERROR_EXCEPTION – javax.servlet.error.exeption
    • ERROR_EXCEPTION_TYPE – javax.servlet.error.exception_type
    • ERROR_MESSAGE – javax.servlet.error.message
    • ERROR_REQUEST_URI – javax.servlet.error.request_uri
    • ERROR_SERVLET_NAME – javax.servlet.error.servlet_name
    • ERROR_STATUS_CODE – javax.servlet.error.status_code

    So, in a nutshell, this JSP example should display all the possible exception detail:

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    ...
    <ul>
        <li>Exception: <c:out value="${requestScope['javax.servlet.error.exception']}" /></li>
        <li>Exception type: <c:out value="${requestScope['javax.servlet.error.exception_type']}" /></li>
        <li>Exception message: <c:out value="${requestScope['javax.servlet.error.message']}" /></li>
        <li>Request URI: <c:out value="${requestScope['javax.servlet.error.request_uri']}" /></li>
        <li>Servlet name: <c:out value="${requestScope['javax.servlet.error.servlet_name']}" /></li>
        <li>Status code: <c:out value="${requestScope['javax.servlet.error.status_code']}" /></li>
    </ul>
    

    Additionally, you could also show this useful information:

    <%@ 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" />
    ...
    <ul>
        <li>Timestamp: <fmt:formatDate value="${date}" type="both" dateStyle="long" timeStyle="long" /></li>
        <li>User agent: <c:out value="${header['user-agent']}" /></li>
    </ul>
    

    The concrete Exception instance itself is in the JSP only available as ${exception} when you mark the page as an error page:

    <%@ page isErrorPage="true" %>
    ...
    ${exception}
    

    Only if you’re using EL 2.2 or newer, then you can print its stacktrace as below:

    <%@ page isErrorPage="true" %>
    ...
    <pre>${pageContext.out.flush()}${exception.printStackTrace(pageContext.response.writer)}</pre>
    

    Or if you’re not on EL 2.2 yet, then create a custom EL function for that:

    public final class Functions {
    
        private Functions() {}
    
        public static String printStackTrace(Throwable exception) {
            StringWriter stringWriter = new StringWriter();
            exception.printStackTrace(new PrintWriter(stringWriter, true));
            return stringWriter.toString();
        }
    
    }
    

    Which is registered in /WEB-INF/functions.tld:

    <?xml version="1.0" encoding="UTF-8" ?>
    <taglib 
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
        version="2.1">
    
        <display-name>Custom Functions</display-name>    
        <tlib-version>1.0</tlib-version>
        <uri>http://example.com/functions</uri>
    
        <function>
            <name>printStackTrace</name>
            <function-class>com.example.Functions</function-class>
            <function-signature>java.lang.String printStackTrace(java.lang.Throwable)</function-signature>
        </function>
    </taglib>
    

    And can be used as

    <%@ taglib prefix="my" uri="http://example.com/functions" %>
    ...
    <pre>${my:printStackTrace(exception)}</pre>
    

    As to the logging of the exception, easiest place would be a filter which is mapped on an URL pattern of /* and does basically the following:

    try {
        chain.doFilter(request, response);
    } catch (ServletException e) {
        log(e.getRootCause());
        throw e;
    } catch (IOException e) { // If necessary? Usually not thrown by business code.
        log(e);
        throw e;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is something I know can be done somehow , because I've done it
I have a little web app that uses the Facebook like widget. This page
I know I can put escaped HTML tags in string resources. However, looking at
Does someone know of a Sqlite manager that I can put on my site,
Simple question, I know, but I can't seem to find a way to put
Sometimes, I'll end up having to catch an exception that I know can never
I have a problem. The structure for my website looks something like this. root
I want to know can we have a JPanel with a Layout other than
I need to know can we add image in TextArea through StyleableTextField htmlText because
I have a query that I know can be done using a subselect, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.