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 1031301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T13:54:22+00:00 2026-05-16T13:54:22+00:00

I’m having a tough time consolidating errors in my application. Currently, my error.jsp looks

  • 0

I’m having a tough time consolidating errors in my application. Currently, my error.jsp looks like following (part of it):

 <%@ page isErrorPage="true" %>
 <%@page contentType="text/html"%>
 <%@page import="java.util.*"%>
 <%@page import="javax.servlet.*"%>
 <%@page import="javax.servlet.http.*"%>
 <%@page import="java.util.Calendar"%>
 <%@page import="java.text.SimpleDateFormat"%>

<html>
<%
String code = null, message = null, type = null, uri = null, time = null;
Object codeObj=null, messageObj=null, typeObj=null;
if (request.getAttribute("javax.servlet.error.status_code") != null)
    codeObj = request.getAttribute("javax.servlet.error.status_code");
if (request.getAttribute("javax.servlet.error.message") != null)
    messageObj = request.getAttribute("javax.servlet.error.message");
if (request.getAttribute("javax.servlet.error.exception_type")!=null)
    typeObj = request.getAttribute("javax.servlet.error.exception_type");

if (codeObj != null) code = codeObj.toString();
if (messageObj != null) message = messageObj.toString();
if (typeObj != null) type = typeObj.toString();
uri = (String) request.getAttribute("javax.servlet.error.request_uri");
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
time = sdf.format(cal.getTime());

String error = "Code:\t\t" + code + "\nType:\t\t" + type + "\nURL:\t\t" + uri + "\nTime:\t\t" + time +"\nMessage:\t" + message;
%>

This works fine in all scenarios except!:
Sometimes in my application I am catching built-in exceptions in MyException class with the following code:

catch(MyException ex){
    log.error(ex.getMessage(), uivex);
    String originalURL = "/errorpages/error.jsp?errorcode=" + (ex.getMajor() + ex.getMinor()) + "&errormessage=" + ex.getMessage();
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address);
    dispatcher.forward(request,response);   
}

Now the problem is that when I get forwarded to error.jsp page…Instead of seeing the actual error from MyException class..I’m seeing NullPointerException because nothing is present in javax.servlet.error.status_code and the page is declared as isErrorPage="true"

What should I do in this case? One solution is to make a completely different error.jsp (name it error1.jsp) page and forward the exceptions from MyException class to that page. Though, I would like to have everything in one place.

  • 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-05-16T13:54:23+00:00Added an answer on May 16, 2026 at 1:54 pm

    This code honestly hurts my eyes. Here is how a generic one should look like. You may find it useful.

    <%@ page pageEncoding="UTF-8" %>
    <%@ 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" />
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>Error</title>
            <link rel="stylesheet" href="style.css">
        </head>
        <body>
            <h1>Error</h1>
            <p>Unfortunately an unexpected error has occurred. Below you can find the error details.</p>
            <h2>Details</h2>
            <ul>
                <li>Timestamp: <fmt:formatDate value="${date}" type="both" dateStyle="long" timeStyle="long" />
                <li>Action: <c:out value="${requestScope['javax.servlet.forward.request_uri']}" />
                <li>Exception: <c:out value="${requestScope['javax.servlet.error.exception']}" />
                <li>Message: <c:out value="${requestScope['javax.servlet.error.message']}" />
                <li>Status code: <c:out value="${requestScope['javax.servlet.error.status_code']}" />
                <li>User agent: <c:out value="${header['user-agent']}" />
            </ul>
        </body>
    </html>
    

    The @page isErrorPage is by the way only useful if you want to have the ${exception} (i.e. request.getAttribute("exception") available in the JSP. In this particular case you don’t need it.

    And indeed, do not forward in the catch block at all. Just let it go. It will be dealt by the error page then.

    } catch (MyException ex) {
        log.error(ex.getMessage(), uivex);
        throw ex; // Or throw new ServletException(ex.getMessage(), ex);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.