0

For a WebApp, I need to serve all .js, .css and all images from corresponding folders in my web app development tree ./js, ./css, ... through a default handling. So any URL like

http://www.example.com/js/x.js

should be served straight from the static files in the war.

The main servlet should deal with all requests that are not for the above. I need to be able to process requests like

http://www.example.com/PROJ/ELEM/WHATEVER

with the same unique main servlet.

So I thought I'd do this in the web.xml:

<servlet-mapping>
   <servlet-name>default</servlet-name>
   <url-pattern>/css/*,/js/*,/WEB-INF/*</url-pattern>
</servlet-mapping>

and map the main servlet like this to make some JSTL mods in a JSP file:

@WebServlet(urlPatterns="/*")
public class Main extends HttpServlet {
    protected void processRequest(HttpServletRequest request, 
                                  HttpServletResponse response)
            throws ServletException, IOException {
        request.setAttribute("test", "ok");
        request.getRequestDispatcher("/WEB-INF/index.jsp")
                            .forward(request, response);        
    }
}

When I do this I end up in a recursive loop.

Is there a way to achieve this?

BxlSofty
  • 501
  • 4
  • 16
  • Check this link :- http://stackoverflow.com/questions/870150/how-to-access-static-resources-when-using-default-servlet I think it will help you. – Rahul Jain Jul 28 '14 at 05:39
  • @RahulJain Thanks for the suggestion. The accepted answer to that question didn't work for me but the next one (with 30+ votes) did. Could you make it an answer, so I can vote on it and explain further? Thanks – BxlSofty Jul 28 '14 at 10:08

3 Answers3

1

Here is the explanation with same problem. http://www.kuligowski.pl/java/rest-style-urls-and-url-mapping-for-static-content-apache-tomcat,5

Rahul Jain
  • 658
  • 5
  • 20
  • 1
    This works for me. In summary: define a global filter where you detect the static paths and redirect it to the default servlet with a **chain.doFilter** then redirect all others to your smarter servlet(s) with a **forward** – BxlSofty Jul 28 '14 at 11:56
0

This is what already happens. There is a 'default servlet' that handles any request that isn't specifically mapped to an installed servlet.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

A simple variation on Rahul Jain's answer. You could do what spring MVC does for static resources : DispatcherServlet is a catch all, and it is configured to delegate to default servlet for a number or url. This may be interesting for a catch all servlet because it is often designed as a front controller that delegates actual serving to other controllers (be them servlets or not).

You can simply give the prefixes of urls that should serve static resources in a comma separated string in a context param of name static_path_prefixes and put that in your servlet :

String[] staticPathPrefixes;
RequestDispatcher defaultDispatcher;

@Override
protected void service(HttpServletRequest hsr, HttpServletResponse hsr1) throws ServletException, IOException {
    String path = hsr.getServletPath();
    for (String url: staticPathPrefixes) {
        if (path.startsWith(url)) {
            defaultDispatcher.forward(hsr, hsr1);
            return;
        }
    }
    super.service(hsr, hsr1);
}

@Override
public void init() throws ServletException {
    String urls = getServletConfig().getInitParameter("static_path_prefixes");
    staticPathPrefixes = urls.split(" *, *");
    defaultDispatcher = getServletConfig().getServletContext().
            getNamedDispatcher("default");
}
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252