Servlet在web.xml中的配置
昨天写一个下载文件的demo,就是用最简单的方式实现文档的下载,不使用框架什么的,当然就是使用servlet的方式进行交互了,但是昨天在配置servlet的时候遇到了一个小问题,就是在Tomcat7.0.23、JDK1.6,下面与Tomcat7.0.81,JDK1.7下面有一点区别,没有搞清楚为什么,只在这里记录下。
1、Tomcat7.0.23 、JDK1.6
<servlet> <servlet-name>DownloadServlet</servlet-name> <servlet-class>cn.com.fasoft.demo.DownloadServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DownloadServlet</servlet-name> <url-pattern>/fileDownload</url-pattern> </servlet-mapping>
@WebServlet("DownloadServlet")
这种配置是可以的,Tomcat会正常启动。地址也是可以访问的。
2、Tomcat7.0.81 、JDK1.7
<servlet>
<servlet-name>DownloadServlet</servlet-name>
<servlet-class>cn.com.fasoft.demo.DownloadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DownloadServlet</servlet-name>
<url-pattern>/fileDownload</url-pattern>
</servlet-mapping>
@WebServlet(name="DownloadServlet",urlPatterns="/fileDownload")
这种配置是可行的,但是采用上面的@WebServlet的配置方式,Tomcat会报错的,就是下面这个最明显的错误,他会认为创建两个servlet同时指向url-pattern,这是不允许的
Caused by: java.lang.IllegalArgumentException: The servlets named [DownloadServlet] and [cn.com.fasoft.demo.DownloadServlet] are both mapped to the url-pattern [/fileDownload] which is not permitted
at org.apache.catalina.deploy.WebXml.addServletMapping(WebXml.java:293)
at org.apache.catalina.startup.ContextConfig.processAnnotationWebServlet(ContextConfig.java:2458)
at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2133)
at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2094)
at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2086)
at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2086)
at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2086)
at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2086)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1303)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:888)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:388)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5519)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
... 6 more
还是应该按照规范的方式进行配置,才能保证代码的正确性。

浙公网安备 33010602011771号