tomcat - Default servlet getRequestDispatcher of a static page -
@webservlet("/") public class rootservlet extends httpservlet { private static final long serialversionuid = 1l; protected void processrequest(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string pathinfo = request.getservletpath(); switch(pathinfo) { case "/": this.handlehomepage(request, response); break; default: request.getrequestdispatcher(pathinfo).forward(request, response); } }
i'm trying use default servlet catch context root url. when it's root url, handled handlehomepage method. if not, forwarded corresponding file. e.g. css, html, images file. cause never-ending exception occur. getrequestdispatcher allow forwarding static page?
you better create filter :
@webfilter(filtername = "rootfilter", urlpatterns = { "/*" }, dispatchertypes = { dispatchertype.request }) { @override public void dofilter(servletrequest p_orequest, servletresponse p_oresponse, filterchain p_ochain) throws ioexception, servletexception { // skip non-http requests if(!(p_orequest instanceof httpservletrequest)) { p_ochain.dofilter(p_orequest,p_oresponse); } else { string pathinfo = ((httpservletrequest)p_orequest).getservletpath(); switch(pathinfo) { case "/": // forward "root servlet" break; default: p_ochain.dofilter(p_orequest,p_oresponse); } } } }
note : code untested , not checked syntax errors.
Comments
Post a Comment