How to show the requested URL in a JSP error page?

14,178

Here is a simple example of JSP error page that shows the error code and the URL of the requested page:

404.jsp:

<%@ page language="java" isErrorPage="true" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <title>Error page</title>
    <meta charset="utf-8">
</head>
<body>
    <button onclick="history.back()">Back to Previous Page</button>
    <h1>404 Page Not Found.</h1>
    <br />
    <p><b>Error code:</b> ${pageContext.errorData.statusCode}</p>
    <p><b>Request URI:</b> ${pageContext.request.scheme}://${header.host}${pageContext.errorData.requestURI}</p>
    <br />
</body>
</html>

Useful reading:

P.S.
The use of scriptlets inside JSP is highly discouraged. Read this post.

Share:
14,178

Related videos on Youtube

informatik01
Author by

informatik01

I work as a senior software developer at French-Estonian IT and Telecommunications company Onoff Telecom. My main interests are Java, the related tools and frameworks (especially Spring Framework), and server side development in general. I love Computer Science. I am interested in learning and gaining more knowledge about programming languages, frameworks, platforms, techniques, algorithms and other Computer Science related topics. I find Stack Overflow a fantastic place to learn and find solutions to programming problems.

Updated on September 15, 2022

Comments

  • informatik01
    informatik01 over 1 year

    In the error page I would like to display the URL what the user requested.

    In my web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns="http://java.sun.com/xml/ns/javaee"
            xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
            xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                                http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
            id="WebApp_ID"
            version="3.0">
    
        <display-name>MyStuff</display-name>
    
        <error-page>
            <error-code>404</error-code>
            <location>/WEB-INF/error-404.jsp</location>
        </error-page>
    
    </web-app>
    

    This will forward to error-404.jsp, and here is the content of that file:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Page Not found</title>
    </head>
    <body>
        <p align="center">
        <%
            out.println("Requested resource: " + request.getRequestURL()+ " not found");
        %>
    </body>
    </html>
    


    The problem it is the request.getRequestURL() need to be changed, but don't know the keyword for what to search.

    When I start the browser for http://localhost:8080/MyStuff then I get the following error:

    Requested resource: http://localhost:8080/MyStuff/WEB-INF/error-404.jsp not found

    How to solve this?