views:

926

answers:

3

I am using the following servlet-mapping in my web.xml file:

<servlet>
    <servlet-name>PostController</servlet-name>
    <servlet-class>com.webcodei.controller.PostController</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>PostController</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

To do some kind of a search. ex:

 http://www.myweb.com/The search string here 

But the problem is that CSS, JS and Images are treated like a search request.

There are any patterns that strip out *.css, *.js, *.gif and etc, so the requests don't need to pass through my controller?

Thank you so much, bye bye!

+2  A: 

Two options come to mind:

1) Typically, in web app like this, the "action" URLs that are handled by a servlet, are given either a sub-directory like "/actions/*" or are given an extension like "*.action" or "*.do" (this is what Struts does). This way it's clear which URLs are intended for the servlet. This is more of an inclusive solution, rather than the exclusive one you're asking for, but I don't think what you want is possible.

2) The slightly more adventurous option is to set up your web app server behind an apache install that serves up the images, css, etc as flat files, sending only everything else onto the app server. Typically, this is done to take the load off your app server. It would require you to copy all these files to a separete directory for apache to handle.

sblundy
+1  A: 

Rather than blacklisting certain extensions, you might consider whitelisting the URL patterns that reach your PostController servlet instead. For instance:

 <servlet>
    <servlet-name>PostController</servlet-name>
    <servlet-class>com.webcodei.controller.PostController</servlet-class>
 </servlet>
 <servlet-mapping>
    <servlet-name>PostController</servlet-name>
    <url-pattern>/*.jsp</url-pattern>
 </servlet-mapping>

if you are using simple JSPs. Now, HTTP GET requests for files with extension *.css, *.gif, etc. will not be routed through this servlet.

As the questioner pointed out, there are many more URLs that should not be routed through this controller than otherwise.

jordan002
A: 

Jetty interprets web.xml as you're expecting it to. I exposed this problem recently when I moved an application from jetty to tomcat, and all of a sudden couldn't see my static resources anymore. Very frustrating.