Discussions
Web tier: servlets, JSP, Web frameworks: Best way to handle 404 - page not found error in a jsp.
-
Best way to handle 404 - page not found error in a jsp. (15 messages)
- Posted by: Amit Sharma
- Posted on: October 07 2004 08:13 EDT
hi
The problem that i am facing is I have a simple test application if a page is not found than it show 404 error but instead of this page i want to show my own custom error page.Threaded Messages (15)
- Use a <error-page> element in your web.xml file. by Joe Attardi on October 07 2004 16:08 EDT
- Use a <error-page> element in your web.xml file. by Amit Sharma on October 08 2004 00:17 EDT
-
HTTPErrorTrapping by BALAKRISHNAN SUNDARAMOORTHY on October 11 2004 11:01 EDT
-
404 Exception handling by BALAKRISHNAN SUNDARAMOORTHY on October 13 2004 01:12 EDT
-
404 Exception handling by Amit Sharma on October 13 2004 02:15 EDT
- 404 Exception handling by Amit Sharma on October 13 2004 02:27 EDT
- Thanks Sunbalan..... by Bhuvana Muruganandam on January 27 2005 12:00 EST
- re: page not found 404 internet explorer by jo docx on February 09 2006 09:38 EST
- 404 Exception handling by rohit kumar on June 20 2012 01:35 EDT
-
404 Exception handling by Amit Sharma on October 13 2004 02:15 EDT
-
404 Exception handling by BALAKRISHNAN SUNDARAMOORTHY on October 13 2004 01:12 EDT
-
HTTPErrorTrapping by BALAKRISHNAN SUNDARAMOORTHY on October 11 2004 11:01 EDT
- Use a <error-page> element in your web.xml file. by Amit Sharma on October 08 2004 00:17 EDT
- Try a filter by Morten Frederiksen on April 09 2005 12:02 EDT
- Forgot something by Morten Frederiksen on April 09 2005 12:14 EDT
-
Question about Wrapper by Kurt Boets on May 27 2005 05:11 EDT
- RE: Question about Wrapper by Morten Frederiksen on June 10 2005 06:26 EDT
-
Question about Wrapper by Kurt Boets on May 27 2005 05:11 EDT
- Re: Try a filter by TH LIN on June 04 2009 06:03 EDT
- Try a filter by johnny english on October 25 2011 14:30 EDT
- Forgot something by Morten Frederiksen on April 09 2005 12:14 EDT
-
Use a <error-page> element in your web.xml file.[ Go to top ]
- Posted by: Joe Attardi
- Posted on: October 07 2004 16:08 EDT
- in response to Amit Sharma
Hey Amit,
In your deployment descriptor (web.xml) file, use an <error-page> element to define the 404 error, as follows:
<error-page>
<error-code>404</error-code>
<location>/ErrorPage.jsp</location>
</error-page>
-- Joe -
Use a <error-page> element in your web.xml file.[ Go to top ]
- Posted by: Amit Sharma
- Posted on: October 08 2004 00:17 EDT
- in response to Joe Attardi
Hey joe,
I have tried that but it is still not working.
Suppose that a Application has a form in a form.jsp with an action attribute set another action.jsp page and in errorpage attribute set to "Error.jsp".
And then what happens is that the application does not find the action.jsp as it does not exits.
solve this problem pls -
HTTPErrorTrapping[ Go to top ]
- Posted by: BALAKRISHNAN SUNDARAMOORTHY
- Posted on: October 11 2004 23:01 EDT
- in response to Amit Sharma
Hi Amit,
Alternatively, you can specify an HTTP error code in the <exception-type> element
Handling HTTP error codes in web.xml
<error-page>
<exception-type>404</exception-type>
<location>/404error.html</location>
</error-page>
-sunbalan
BALAKRISHNAN SUNDARAMOORTHY
S'PORE -
404 Exception handling[ Go to top ]
- Posted by: BALAKRISHNAN SUNDARAMOORTHY
- Posted on: October 13 2004 01:12 EDT
- in response to BALAKRISHNAN SUNDARAMOORTHY
Its possible that from the Microsoft Internet Explorer, you may not be able to see some of the customized error pages that are configured in web.xml. Instead you will see 'Page Not Found' - 404 error. To rectify this problem, from the browser's 'Tools' main menu , choose the option 'Internet Options'. Click on the 'Advanced' tab. Uncheck the option 'Show friendly HTTP error messages' and try invoking the servlets again.
Thanks
sunbalan -
404 Exception handling[ Go to top ]
- Posted by: Amit Sharma
- Posted on: October 13 2004 02:15 EDT
- in response to BALAKRISHNAN SUNDARAMOORTHY
Hi Subalan,
The thing that you suggest to show the custom 404 error page are not give desired result for my application. Is there any way that I can configure this custom error page on my server(Tomcat) so that it gives the desired result.
thanks for all the above tips.
Amit Sharma. -
404 Exception handling[ Go to top ]
- Posted by: Amit Sharma
- Posted on: October 13 2004 02:27 EDT
- in response to Amit Sharma
hi subalan
thanks yaar your tip is working it was due to the Explorer .
thanks for the help.
Amit Sharma -
Thanks Sunbalan.....[ Go to top ]
- Posted by: Bhuvana Muruganandam
- Posted on: January 27 2005 12:00 EST
- in response to Amit Sharma
It really worked for me....
-Bhuvana Sundaramoorthy -
re: page not found 404 internet explorer[ Go to top ]
- Posted by: jo docx
- Posted on: February 09 2006 09:38 EST
- in response to BALAKRISHNAN SUNDARAMOORTHY
You need a 5KB+ error page
from
http://forum.java.sun.com/thread.jspa?threadID=642939&messageID=3793598 :
IE overrides several HTTP error status pages but it has a size threshold. Basically if the error page send by the server has a large enough body then IE decides it's meaningful and displays it.
Usually to be safe you should make error pages that are larger then 512 bytes. The threshold varies per HTTP status code. You can look at what your thresholds are currently set to. In IE 5 and greater the settings are stored in the registry under[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\ErrorThresholds]
Err Size(bytes)
400 512
403 256
404 512
405 256
406 512
408 512
409 512
410 256
500 512
501 512
505 512 -
404 Exception handling[ Go to top ]
- Posted by: rohit kumar
- Posted on: June 20 2012 01:35 EDT
- in response to BALAKRISHNAN SUNDARAMOORTHY
hi ,
but it wont be possible to say the client to adjust with internet tools option. Is there any other way apart from .htaccess
-
Try a filter[ Go to top ]
- Posted by: Morten Frederiksen
- Posted on: April 09 2005 12:02 EDT
- in response to Amit Sharma
You cannot get around the fact that ie default shows its own error page, when the response contains a error code.
But as a workarround you can create a filter like this:
public class ErrorHandlerFilter implements Filter {
public void init(FilterConfig arg0) throws ServletException {
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) servletResponse;
if (!(response instanceof ErrorAwareRequestWrapper)) {
response = new ErrorAwareRequestWrapper(response);
}
filterChain.doFilter(servletRequest, response);
}
public void destroy() {
}
}
And a wrapper for the response like this:
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
public class ErrorAwareRequestWrapper extends HttpServletResponseWrapper{
public ErrorAwareRequestWrapper(HttpServletResponse httpServletResponse) {
super(httpServletResponse);
}
public void sendError(int errorCode) throws IOException {
if (errorCode == HttpServletResponse.SC_NOT_FOUND){
throw new PageNotFoundException();
}
super.sendError(errorCode);
}
}
The exception:
public class PageNotFoundException extends RuntimeException{
}
In your web.xml, define the filer (should be the first filter):
<filter>
<filter-name>errorHandlerFilter</filter-name>
<filter-class>ErrorHandlerFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>errorHandlerFilter</filter-name>
<url-pattern>/*</url-pattern>
<!-- These are needed by Tomcat 5 for forwards -->
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
…
…
…
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
<error-page>
<exception-type>PageNotFoundException</exception-type>
<location>/404.jsp</location>
</error-page>
--Morten -
Forgot something[ Go to top ]
- Posted by: Morten Frederiksen
- Posted on: April 09 2005 12:14 EDT
- in response to Morten Frederiksen
Your errorpage (in my example: 404.jsp) must have this:
<%@ page language="java" isErrorPage="true" %>
--Morten -
Question about Wrapper[ Go to top ]
- Posted by: Kurt Boets
- Posted on: May 27 2005 05:11 EDT
- in response to Morten Frederiksen
Hi,
When is your sendError(int sc) called ?
And how/where can you determine the status code ? -
RE: Question about Wrapper[ Go to top ]
- Posted by: Morten Frederiksen
- Posted on: June 10 2005 18:26 EDT
- in response to Kurt Boets
Hi
The sendError(int sc) in ErrorAwareRequestWrapper is callede (or should be) by the webcontainer, when it "discovers" that there are no mappings (or static content) to a given web-resource.
The filter is mappede to recive all request, also those who result in a "page not found", otherwise it would be impossible to trace these incidents.
The status code can be determined by the int parameter (sc), see http://www.w3.org/Protocols/HTTP/HTRESP.html
/Morten -
Re: Try a filter[ Go to top ]
- Posted by: TH LIN
- Posted on: June 04 2009 06:03 EDT
- in response to Morten Frederiksen
thanks for the solution!!!! -
Try a filter[ Go to top ]
- Posted by: johnny english
- Posted on: October 25 2011 14:30 EDT
- in response to Morten Frederiksen
Hi Morten,
I did all the steps you mentioned in the article. it compiles and and in run time it's not calling sendError method in ErrorAwareRequestWrapper.java, due to which it's not displaying the error page. what am I missing?. I kept some Sysytem.out lines in all the methods. From the stdout I see that it's not printing what's there in sendError method. what am I missing?