views:

278

answers:

3

Hi there, I want a Servlet to handle requests to files depending on prefix and extension, e.g.

prefix_*.xml

Since mapping on beginning AND end of request path is not possible, I have mapped all .xml requests to my Servlet. The question now is: how can I drop out of my servlet for XML files not starting with "prefix", so that the request is handled like a "normal" request to an xml file?

This is probably quite simple but I do not seem to be able to find this out... :-/

Thanks a lot in advance

A: 

Not shure, but once you catch all *.xml requests you can inspect the request again in your code via HttpServletRequest.getRequestURI()

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri =req.getRequestURI();
        int i = uri.lastIndexOf('/');
        int j = uri.lastIndexOf('.', i);
        if (uri.substring(i+1, j).startsWith("prefix_")) {
            // your code
        }
    }

(code not tested, only an idea ...)

PeterMmm
This doesn't answer the question: how to fall back to default processing in this case.
Avi
Thank you... I've been that far. What is (or hopefully in a few minutes: was) missing for me is the "else" part of it, as Avi pointed out.
PeterP
A: 

I would strongly suggest using a proper MVC framework for this. As you've discovered, the flexibility of the standard servlet API is very limited when it comes to request dispatching.

Ideally, you would be able to use your existing servlet code in combination with an MVC framework, with the framework doing the diapcthing based on path pattern, and your servlets doing the business logic. Luckily, Spring MVC allows you to do just that, using the ServletForwardingController. It'd be a very lightweight spring config.

So you'd have something like this in your web.xml:

<servlet>
   <servlet-name>myServlet</servlet-name>
   <servlet-class>foo.MyServlet</servlet-class>
</servlet>

<servlet>
   <servlet-name>spring</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<url-mapping>
   <servlet-name>spring</servlet-name>
   <url-pattern>*</url-pattern>
</url-mapping>

You would then have a WEB-INF/spring-servlet.xml file like this:

<beans>
    <bean name="/prefix*.xml" class="org.springframework.web.servlet.mvc.ServletForwardingController">
       <property name="servletName" value="myServlet"/>
    </bean>
</beans>

And that would be pretty much it. All requests for /prefix*.xml would go to myServlet, and all others would fall through to the container.

skaffman
Hey, thanks... this looks pretty easy. Spring keeps surprising me :) Actually, we already use swing as MVC framework - I'll check this out.
PeterP
It's sometimes surprising how little config you can get away with in Spring, there's a lot of convention-over-configuration that you can take advantage of.
skaffman
+2  A: 

Hi, another solution (maybe fits for you) is if you are using/plan to use an Apache in front of that web container instance you could use the rewrite module of apache. Rewriting the url to something more easy to handle for the Webapp container.

Hope this helps. David.

David Santamaria
Thank you... Sounds helpful - However, I'd prefer not to be bound to Apache and (since we are already bound to Spring as MVC), I'd rather stick for skaffman's suggestion. Seems to be a lot easier under the given circumstances :) Thx
PeterP