Changing default error message

J

jonesy

I am making website using java http servlets, and I would like to
customise the error message that users see when they try to access a
file that no longer exists.

This is the error message that is currently displayed:

Error 404--Not Found
From RFC 2068 Hypertext Transfer Protocol -- HTTP/1.1:
10.4.5 404 Not Found
The server has not found anything matching the Request-URI. No
indication is given of whether the condition is temporary or permanent.

If the server does not wish to make this information available to the
client, the status code 403 (Forbidden) can be used instead. The 410
(Gone) status code SHOULD be used if the server knows, through some
internally configurable mechanism, that an old resource is permanently
unavailable and has no forwarding address.



Below is code from my servlet that I tried:


try
{
res.sendRedirect("/Leave.xls"); //this redirects the user to the
file that sometimes might not exist
}
catch (FileNotFoundException e) //this is my attempt at creating a
custom error message
{
out.println("<HTML>\n<HEAD>\n<TITLE>Access Denied</TITLE>\n");
out.println("<link rel='stylesheet' type='text/css'
href='/style.css' media='screen' />\n");
out.println("</HEAD>");
out.println("<br><p>The file you requested does not currently
exist. Please try again later.</p>");
out.println("<p>If this error occurs again, please <a
href='/servlets/Contact'>contact us</a>.</p>");
out.println("<p>Otherwise, Return to the <a
href='/servlets/Home'>home page</a>");
out.println("</font>\n</BODY>\n</HTML>");
}
 
H

hiwa

jonesy said:
I am making website using java http servlets, and I would like to
customise the error message that users see when they try to access a
file that no longer exists.

This is the error message that is currently displayed:

Error 404--Not Found
10.4.5 404 Not Found
The server has not found anything matching the Request-URI. No
indication is given of whether the condition is temporary or permanent.

If the server does not wish to make this information available to the
client, the status code 403 (Forbidden) can be used instead. The 410
(Gone) status code SHOULD be used if the server knows, through some
internally configurable mechanism, that an old resource is permanently
unavailable and has no forwarding address.



Below is code from my servlet that I tried:


try
{
res.sendRedirect("/Leave.xls"); //this redirects the user to the
file that sometimes might not exist
}
catch (FileNotFoundException e) //this is my attempt at creating a
custom error message
{
out.println("<HTML>\n<HEAD>\n<TITLE>Access Denied</TITLE>\n");
out.println("<link rel='stylesheet' type='text/css'
href='/style.css' media='screen' />\n");
out.println("</HEAD>");
out.println("<br><p>The file you requested does not currently
exist. Please try again later.</p>");
out.println("<p>If this error occurs again, please <a
href='/servlets/Contact'>contact us</a>.</p>");
out.println("<p>Otherwise, Return to the <a
href='/servlets/Home'>home page</a>");
out.println("</font>\n</BODY>\n</HTML>");
}
Use <error-page> element of your web.xml file.
 
S

Simon Brooke

jonesy said:
I am making website using java http servlets, and I would like to
customise the error message that users see when they try to access a
file that no longer exists.

This is the error message that is currently displayed:

Error 404--Not Found
10.4.5 404 Not Found
The server has not found anything matching the Request-URI. No
indication is given of whether the condition is temporary or permanent.

If the server does not wish to make this information available to the
client, the status code 403 (Forbidden) can be used instead. The 410
(Gone) status code SHOULD be used if the server knows, through some
internally configurable mechanism, that an old resource is permanently
unavailable and has no forwarding address.



Below is code from my servlet that I tried:


try
{
res.sendRedirect("/Leave.xls"); //this redirects the user to the
file that sometimes might not exist
}
catch (FileNotFoundException e) //this is my attempt at creating a

It redirects the user, but it doesn't go and find the file itself, so the
exception will never be thrown.

Include at the end of your web.xml file, the following:

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

jonesy

thanks!


Simon said:
It redirects the user, but it doesn't go and find the file itself, so the
exception will never be thrown.

Include at the end of your web.xml file, the following:

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

--
(e-mail address removed) (Simon Brooke) http://www.jasmine.org.uk/~simon/
Ring of great evil
Small one casts it into flame
Bringing rise of Men ;; gonzoron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,044
Members
48,769
Latest member
Clifft

Latest Threads

Top