Tomcat 6.x web.xml default and custom servlet routing
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.catali开发者_C百科na.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.
Tomcat's default servlet before 6.0.30 actually serves a static resource identified by HttpServletRequest.getPathInfo()
, so that /style.css
will be returned when /resources/style.css
is requested.
Tomcat's behavior has changed from version 6.0.30 onwards. So the original configuration from the question works in newer versions of Tomcat. See "50026: Add support for mapping the default servlet to URLs other than /. (timw)" in the changelog.
Jetty's default servlet uses a full path.
It should work fine. Are those files in real also located in the /resources
folder?
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.
Try changing your url-pattern
for myservlet
to /
, and optionally adding <mvc:default-servlet-handler />
(see here) to your Spring configuration.
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
精彩评论