Strip images, CSS and JS from the mapping servlet

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

:

<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> 

      

Do some kind of search. ex:

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

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

Are there any templates out there that highlight * .css, * .js, * .gif, etc. so requests shouldn't go through my controller?

Thank you so much, goodbye!

+1


source to share


3 answers


Two options come to mind:

1) Typically, in a web application like this, the "action" URLs that are processed by the servlet are either provided by a subdirectory like " /actions/*

" or get an extension like " *.action

" or " *.do

" (which is what Struts does). This way it is figured out which urls are for the servlet. This is more of an end-to-end solution rather than the exclusive one you are asking for, but I don't think you want.



2) A slightly more adventurous option is to set up a web application server behind an apache installation that serves images, css, etc. as flat files, sending just everything else to the application server. Typically, this is done to take the load off your application server. This will require you to copy all these files into the separete directory for apache to handle.

+2


source


Instead of blacklisting specific extensions, you can whitelist URL patterns that end up in your PostController servlet. For example:

 <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 plain JSPs. Now HTTP GET requests for files with the extension * .css, * .gif, etc. Will not be redirected through this servlet.

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

+1


source


Jetty interprets web.xml as you expect. I recently uncovered this issue when I moved my app from jetty to tomcat and suddenly I couldn't see my static resources. Very frustrating.

0


source







All Articles