简体   繁体   中英

Multiple Servlets in WebApplicationInitializer

I was trying to set up multiple servlets in my WebApplicationInitializer , one for the default requests (that trigger the jsp and return the html, using a DispatcherServlet , and one for static resources, using a custom made StaticServlet . What confuses me is how to apply the routing to get the requests to the right servlet, and in fact one of the static servlet is never called to resolve the requests seems to confirm my suspicions. So far this is the code I have on the WebApplicationInitializer :

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    servletContext.setSessionTrackingModes(
        Collections.<SessionTrackingMode>emptySet());

    // Create the 'root' Spring application context
    // which will contain the application components
    AnnotationConfigWebApplicationContext rootContext
        = new AnnotationConfigWebApplicationContext();
    rootContext.register(RootConfig.class);

    // Manage the lifecycle of the root application context
    servletContext.addListener(new ContextLoaderListener(rootContext));

    // Create the dispatcher servlet's Spring application context
    // to contain dispatched beans such as controllers
    AnnotationConfigWebApplicationContext dispatcherContext
        = new AnnotationConfigWebApplicationContext();
    dispatcherContext.register(DispatcherConfig.class);

    // Register and map the dispatcher servlet under /
    ServletRegistration.Dynamic dispatcher = servletContext.addServlet(
        "dispatcher",
        new DispatcherServlet(dispatcherContext));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");

    // Register and map the static dispatcher servlet under /static/*
    ServletRegistration.Dynamic staticDispatcher = servletContext.addServlet(
        "staticDispatcher",
        new FileServlet());

    staticDispatcher.setLoadOnStartup(1);
    staticDispatcher.setInitParameter("basePath", "/static/fonts/");
    staticDispatcher.addMapping("/static/*");

The static Servlet do not need a Configuration (Is a base HttpServlet ), but what bothers me is the fact that I am using two ServletRegistration in order to define the two different mappings. Is there a way to use the same one and define the mapping to a specific servlet? Or should the mapping be done at another level (maybe the Listener of the rootContext )? I have tried to look around but it seems no one has solved or have any problem (probably) to set up multiple servlets programmatically.

Any idea of why I'm not getting any hit on the static Servlet ?

Edit:

This is a static file request in one of my .jsp files, that would be supposed to go through the FileServlet :

<style type="text/css">
     @font-face {
        font-family: 'DinWeb';
        src: url(/static/fonts/DINWeb.eot?) format('eot'), url(/static/fonts/DINWeb.woff)  format('woff'), url(/static/fonts/DINComp.ttf) format('truetype');
        font-weight: normal;
    }
</style>

What I would expect is the request to be redirected to the FileServlet (since the url starts with /static/), and from there be manipulated/managed so that it would return the font (or a file or another media)

Founded an alternative Solution: Instead of making two DispactherServlet (or one and another), just implement a servlet and a Filter that will listen to all requests, and take care of those URLs that starts with static.

In my code I added this two lines:

FilterRegistration filterReg = servletContext.addFilter("staticFilter",     StaticFilesFilter.class);
filterReg.addMappingForUrlPatterns(null, false, "/*");

and removed completely the second custom made servlet. As per the filter, this link is pretty useful (Just click the download to see the java file of the filter implementation itself).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM