xml - About servlet Exception -
web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>servletexceptionhandling</display-name> <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> </error-page> </web-app>
myexceptionservlet.java
package com.journaldev.servlet.exception; import java.io.ioexception; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; @webservlet("/myexceptionservlet") public class myexceptionservlet extends httpservlet { private static final long serialversionuid = 1l; protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { throw new servletexception("get method not supported."); } }
appexceptionhandler.java
package com.journaldev.servlet.exception; import java.io.ioexception; import java.io.printwriter; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; @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>"); } }
1) problem facing not getting processerror method in servlet appexceptionhandler due not able see exception in front end.
so please tell have done mistake.
Comments
Post a Comment