views:

122

answers:

5

I have two servlets defined in the web.xml file, namely the default2 and myservlet. The default2 servlet is used to map the static files like the javascript and css. The myservlet is used for getting dynamic content.

<servlet>
    <servlet-name>default2</servlet-name>
    <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet>
    <servlet-name>myservlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:my-servlet.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

The servlet mapping is defined as follows

<servlet-mapping>
    <servlet-name>myservlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>default2</servlet-name>
    <url-pattern>/resources/*</url-pattern>
</servlet-mapping>

When i try to access any files under /resources, i get a 404. Any ideas why this config is not working or change this config to make it work.

A: 

Removed wrong portion of the answer as per @BalusC comment.

Set a break point in your servlet and perform a debug session. Look for the path that your servlet is picking up these files at. Make sure they match the location

Sean
Wrong, the servletcontainer will match the *most specific* servlet url pattern. Probably you're confusing it with the behaviour of filters.
BalusC
Yeah you are write. I wasn't thinking. Still setting a breakpoint in the servlet will help determine where the application is looking for a file.
Sean
I suggest to edit your post as you're getting negative score.
The Elite Gentleman
A: 

It should work fine. Are those files in real also located in the /resources folder?

BalusC
yes, these file are in real location.
Spring Monkey
The exception tells it is not. Are they accessible by the same URL when you remove the both servlets? If still not, then URL used and/or actual location is plain wrong (or you have a `Filter` listening on `/*` which is badly written and is thus disturbing everything).
BalusC
A: 

Your web.xml looks correct (except I would change your <load-on-startup> constants). Make sure that your /resources exists and is a publicly visible folder in your project path and not under /WEB-INF folder.

The Elite Gentleman
+1  A: 

Tomcat's default servlet actually serves a static resource identified by HttpServletRequest.getPathInfo(), so that /style.css will be returned when /resources/style.css is requested.

(Jetty's default servlet uses a full path, so this behaviour is not portable across containers).

axtavt
A: 

Try changing your url-pattern for myservlet to /, and optionally adding <mvc:default-servlet-handler /> (see here) to your Spring configuration.

James Earl Douglas