2
votes

I have an error.html page which I want to redirect to when a user comes accross Error404 for example. The below is working:

<error-page>
    <error-code>404</error-code>
    <location>/error404.jsp</location>
</error-page>

However, my error404.jsp file contains an image which doesn't get displayed when user is redirected. If I just type in the URL, the full page with image is displayed. But if a user tries to do something which reports error 404, the error404.jsp file gets displayed without the image.

I also tried with having an error404.html but I get the same problem...

Do you have any ideas why this is happening?

Many thanks Ena

1

1 Answers

3
votes

When the error page is rendered, its content is returned in the HTTP response without a redirect. This means that the browser still 'sees' it in context of the URL of the request that had an error. So any relative resource links (images, css, etc.) will be relative to the current page's URL.

So if you have the following setup:

  • image1.gif in images folder
  • error404.jsp that references the image as "../images/image1.gif"

You will find that typing http://yourserver.com/YourContextRoot/error404.jsp works fine, but when you use http://yourserver.com/YourContextRoot/path1/path2/missingPage it tries to look for the image in the wrong path (i.e. under /YourContextRoot/path1/images/image1.gif).

The solution is to use JSP EL or scriptlet to create a server-relative link (i.e. starts with a '/'), by including context-root:

<img src="${pageContext.request.contextPath}/images/image1.gif" />

I hope that works!