jsp - Java-EE redirect to a custom error page -
is there way include error page wrong path request? need wrong path requested user sent error.jsp
for example if "/example" part of web.xml send localhost:8080/example if user specifies localhost:8080/examp redirect error.jsp
i tried following code in web.xml , created error.jsp inside web pages directory in netbeans , still sent me yahoo error handle page:
<web-app> <error-page> <error-code>404</error-code> <location>/error.jsp</location> </error-page> <error-page> <exception-type>java.lang.throwable</exception-type> <location>/error.jsp</location> </error-page> </web-app>
error.jsp
<%@ page iserrorpage="true" %> <html> <head> <title>show error page</title> </head> <body> <h1>opps...</h1> <p>an error occurred.</p> </body> </html>
first create custom servlet , use both exceptions , errors. refer below code.
@webservlet("/appexceptionhandler") public class appexceptionhandler extends httpservlet { private static final long serialversionuid = 1l; protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processerror(request, response); } protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processerror(request, response); } private void processerror(httpservletrequest request, httpservletresponse response) throws ioexception { // analyze servlet exception throwable throwable = (throwable) request .getattribute("javax.servlet.error.exception"); integer statuscode = (integer) request .getattribute("javax.servlet.error.status_code"); string servletname = (string) request .getattribute("javax.servlet.error.servlet_name"); if (servletname == null) { servletname = "unknown"; } string requesturi = (string) request .getattribute("javax.servlet.error.request_uri"); if (requesturi == null) { requesturi = "unknown"; } // set response content type response.setcontenttype("text/html"); printwriter out = response.getwriter(); out.write("<html><head><title>exception/error details</title></head><body>"); if(statuscode != 500){ out.write("<h3>error details</h3>"); out.write("<strong>status code</strong>:"+statuscode+"<br>"); out.write("<strong>requested uri</strong>:"+requesturi); }else{ out.write("<h3>exception details</h3>"); out.write("<ul><li>servlet name:"+servletname+"</li>"); out.write("<li>exception name:"+throwable.getclass().getname()+"</li>"); out.write("<li>requested uri:"+requesturi+"</li>"); out.write("<li>exception message:"+throwable.getmessage()+"</li>"); out.write("</ul>"); } out.write("<br><br>"); out.write("<a href=\"index.html\">home page</a>"); out.write("</body></html>"); } }
then configure in deployment descriptor web.xml
<welcome-file-list> <welcome-file> index.html</welcome-file > </welcome-file-list> <error-page> <error-code> 404</error-code> <location> /appexceptionhandler</location > </error-page> <error-page> <exception-type> javax.servlet.servletexception</exception-type> <location>/appexceptionhandler</location > </error-page> <error-page> <exception-type >java.lang.throwable </exception-type> <location >/appexceptionhandler </location>
when exception occures call appexceptionhandler class , cause display custom error page.
Comments
Post a Comment