Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
626 views
in Technique[技术] by (71.8m points)

tomcat - many url-pattern for the same servlet

I need to map the same servlet on two different url. I used netbeans 7.0.1 for managing my whole project, so I used its friendly interface to modify the web.xml file. What netbeans created is this:

<servlet-mapping>
    <servlet-name>fred</servlet-name>
    <url-pattern>*.jsp</url-pattern>
    <url-pattern>/url</url-pattern>
</servlet-mapping>

This is read by tomcat 5.5 without emitting any error, but only the second pattern works, while the first one is ignored.

Googling I found that the right way for tomcat is this one:

<servlet-mapping>
    <servlet-name>fred</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>fred</servlet-name>
    <url-pattern>/url</url-pattern>
</servlet-mapping>

So, my questions: is this a bug in tomcat? What syntax do other containers accept?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I guess it has more to do with the servlet spec the container/netbeans is using rather than being an issue with the container. Your net beans seems to be using the spec 2.5 to construct the servlet mapping and hence you get

<servlet-mapping>
   <servlet-name>fred</servlet-name>
   <url-pattern>*.jsp</url-pattern>
   <url-pattern>/url</url-pattern>
</servlet-mapping>

Read more about this here. It says

Previous versions of the servlet schema allows only a single url-pattern in a filter mapping.For filters mapped to multiple URLs this results in needless repetition of whole mapping clauses.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...