I configured a custom error page in my web.xml file, but the image referenced in the page shows up as a broken link.
The custom error page is just a simple html page:
<!DOCTYPE html>
<html><head>
<meta charset="UTF-8"><title>401 Error</title></head>
<body>
<p style="font-size: 200%; text-align: center">HTTP Error 401: Not authorized to view sensitive data.<br/>
<img src="NoAccessImage.png" alt="401Error"><br/>
You must log in before viewing the requested page.</p>
</body></html>
This page is stored in an error folder, along with the image file it references.
An Authentication filter is used to throw the 401 error and the page does show up if I attempt to view protected content without first logging in. But the referenced image is missing. If I just drag the file into a web browser, it displays correctly, so I figured this is a question of context. I tried changing the img src tag to "/error/NoAccessImage.png" with no result.
The relevant AuthenticationFilter code is below:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
try {
boolean authorized = false;
HttpServletRequest r = (HttpServletRequest) request;
HttpSession session = ((HttpServletRequest)request).getSession(false);
String uri = r.getRequestURI();
if(uri.indexOf("/Login")>0) {
chain.doFilter(request, response);
return;
}
if (session != null) {
String school = (String) session.getAttribute("school");
if(school != null && school.length()>0 ) {
authorized = (school.equals(getURISchool(uri)));
}
}
if (authorized) {
chain.doFilter(request, response);
return;
} else {
((HttpServletResponse) response).sendError(401, "You must log in to view the schedule.");
}
} catch (IOException io) {
System.out.println("IOException raised in AuthenticationFilter");
}
}
The server doesn’t redirect to your error page. It fowards to it. So, if the initial invoked URL is
http://localhost/myApp/foo/bar/baz/SomeAction, since you used a relative path for your image, the browser will look for the image inhttp://localhost/myApp/foo/bar/baz/NoAccessImage.png. You should thus use an absolute path to reference the image (and for all your images, resources and links, in general).And since each application has a context path, you must prepend the context path to the URL. That’s what the JSTL
<c:url>tag does:which will be translated to
(
myAppbeing the contect path of your application)