user3663882
user3663882

Reputation: 7357

Why do we have to specify attributes of the <web-app> tag in the web.xml file?

Consider two web.xml files: 1.

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
 version="3.0">

  <display-name>app</display-name>
  <servlet>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <servlet-name>dispatcher</servlet-name>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

2.

<web-app>

  <display-name>app</display-name>
  <servlet>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <servlet-name>dispatcher</servlet-name>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

The difference between them is that the web-app tag contains the attributes in the first one and the second one doesn't. The issue is if we apply the second web.xml we won't be able to use expressions like

${attrName}

in our jsp-pages. They won't be replaced with the corresponding values, added to a model in a Controller in the case of Spring MVC.

I thought, the only purpose we specify the schemaLocation attribute is to validate our xml-file (to get XSD-schema). How do the attributes actually work?

Upvotes: 3

Views: 2325

Answers (1)

skaffman
skaffman

Reputation: 403471

The first web.xml specifies the version of the Servlet API that the webapp requires (3.0), whereas the second one doesn't.

In the second case, the servlet container will take that to mean it was written for an older version of the Servlet API, specifically from a version of the Servlet API that didn't use the version attribute or strictly defined namespaces. Those older versions were pre-JSP-2.0, and so don't support the JSP expression language. That means your expressions won't be substituted (you wouldn't want older app that used that expression syntax for some other reason to suddenly have them evaluated by the container in a way that the author didn't intend).

For a modern webapp-you should always specify the Servlet API version you need.

The schemaVersion version isn't relevant in itself, except to say that the modern versions of the Servlet API require them to be defined.

Upvotes: 2

Related Questions