Search code examples
javajspjakarta-eeservlets

RequestDispatcher.forward loop


I am using

<url-pattern>/*</url-pattern>

to map all the requests to one sevlet,where i do all the authentication work. but I want to skip some static content (like css files).so I tried fowrding them from that sevlet to where the resource file is

if(isResourceFile){
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("static/" + fileName);
    dispatcher.forward(request, response);
}

but this will start a infinite loop because this will again call the same sevlet

is there any way to work around this without mapping all the resource(css) files in web.xml?


Solution

  • Assuming that you're looking to authenticate just JSP files, you could change the URL:

    /*.jsp
    

    I think it's a better idea to handle your authentication using a filter rather than a servlet. And in a production environment, you should use a front-end webserver for static content.