10

Is it OK to have multiple elements in the element in a J2EE web app version 2.4 compliant web.xml like this:

<filter-mapping>
    <filter-name>SomeFilter</filter-name>
    <url-pattern>*.htm</url-pattern>
    <url-pattern>*.do</url-pattern>
</filter-mapping>

I looked up the XSD "web-app_2_4.xsd" file from here : http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd and the definition looks like this:

  <xsd:complexType name="filter-mappingType">
    <xsd:annotation>
      <xsd:documentation>
            some documentation here
      </xsd:documentation>
    </xsd:annotation>

    <xsd:sequence>
      <xsd:element name="filter-name"
           type="j2ee:filter-nameType"/>
      <xsd:choice>
    <xsd:element name="url-pattern"
             type="j2ee:url-patternType"/>
    <xsd:element name="servlet-name"
             type="j2ee:servlet-nameType"/>
      </xsd:choice>
      <xsd:element name="dispatcher"
           type="j2ee:dispatcherType"
           minOccurs="0" maxOccurs="4"/>
    </xsd:sequence>
    <xsd:attribute name="id" type="xsd:ID"/>
  </xsd:complexType>

The URL pattern definition looks like this:

So I think, we can have multiple elements in the element. My Eclipse IDE however does not seem to agree with me, and expects a 'dispatcher' tag.

See image: Eclipse error

2
  • I think the xsd:choice element there just means that you have to choose one of the elements within it, i.e. url-pattern or servlet-name. Commented Oct 13, 2012 at 11:30
  • 2
    multiple url-patterns are allowed from J2EE 2.5 specification check xsd of >= 2.5 Commented Nov 20, 2013 at 12:46

2 Answers 2

16

Clearly no, but you can have:

<filter-mapping>
 <filter-name>SomeFilter</filter-name>
 <url-pattern>*.htm</url-pattern>
</filter-mapping>    

<filter-mapping>
 <filter-name>SomeFilter</filter-name>
 <url-pattern>*.do</url-pattern>
</filter-mapping>
Sign up to request clarification or add additional context in comments.

2 Comments

Can you have two filters with same name?
This maps the same filter (SomeFilter) to two different url patterns. Official docs: docs.oracle.com/cd/E13222_01/wls/docs81/webapp/…
4

Default is 1 for maxOccurs and minOccurs in sequence element:
https://msdn.microsoft.com/en-us/library/ms256089(v=vs.110).aspx.

And choice allows only one of the elements of it:
https://msdn.microsoft.com/en-us/library/ms256109(v=vs.110).aspx

1 Comment

@Darshana Thanks. Updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.