many url-pattern for the same servlet

59,881

Solution 1

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.

Solution 2

Tomcat container will support 3 url patterns:

  1. complete character sequence
  2. /*
  3. *. ext (star means anything)

/* is recommended for only one single framework

if you use multiple framework then recommended .*

Share:
59,881
eppesuig
Author by

eppesuig

SysAdmin on Unix (HP-UX, AIX, Linux, and Solaris), Windows (from Windows Server 2008 to 2019), VMware vSphere, and KVM virtualizazion on Linux. I mainly work on Infor OS and Infor ERP LN sizing, software installation, performance analysis, virtualization, automating jobs, and tuning for problems related to Infor ERP LN and databases (mainly Oracle and SQL Server). I also design applications and program them, currently in Java. I am an open source software developer addicted to Debian GNU/Linux on whitch I work on a daily bases. My main programming languages are java, bash scripting, C, and rarely python.

Updated on December 06, 2020

Comments

  • eppesuig
    eppesuig over 3 years

    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?