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

前端 未结 1 1822
孤城傲影
孤城傲影 2021-01-13 16:10

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

In my web.xml:

&l         


        
相关标签:
1条回答
  • 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:

    • "JSP.1.4 Error Handling" section in the JavaServer Pages Specification (JSR 245).
    • Handling JSP Page Errors (from the official Java EE 5 Tutorial)

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

    0 讨论(0)
提交回复
热议问题