What exactly is the web-app version? What does it affect?
Asked Answered
A

3

31

In a java web application, there is a file called web.xml and it has a versioning.

What exactly is this? What is it used for?

Here is the SO wiki for web.xml. But it does not really explain me much.

It allows you to define, declare and configure the Servlet API based implementations in your web application, such as servlets, filters and listeners.

Sample web.xml versioning:

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

Can someone explain this with simple examples perhaps?

Archean answered 13/4, 2013 at 7:25 Comment(0)
R
38

Web.xml is a central place where you define the configuration of your Web application. For instance you can specify there:

I would also suggest researching Servlet 3.0 specification, where many of these parameters can be set through annotations.

Versioning

Versioning refers to XML schema version that syntax of your web.xml file must obey. More important, it also indicates the version of Servlet specification that your application implements. An example how Servlet 3.0-compliant web.xml should begin:

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

Most IDEs will automatically generate that part of web.xml. If you are going to change it manually for some reason, be careful to match the versions of web-app and xsd.

For concrete examples of web.xml, see:

Rapallo answered 13/4, 2013 at 8:38 Comment(3)
How is the version of this file important?Archean
Is it like telling the container what implementation I am using? So I already have Java Servlet classes that implements the methods that come with version 3? So I can declare my webapp to be version 3 but use Tomcat 5 and just get along fine if I do not use any of the methods that were introduced with Servlet 3.0 API?Archean
@KorayTugay Yes, you'll be fine. However, if you mix versions in web.xml (i.e. put web-app version="2.5" and xsi:schemaLocation="..web-app_3_0.xsd") you won't be able to build the application.Rapallo
R
6

From Servlet 3.0 onwards, Web.xml is optional, You can use @WebServlet annotation instead.

If you are using Spring, org.springframework.web.WebApplicationInitializer can be used instead or along with web.xml.

WebApplicationInitializer :

Interface to be implemented in Servlet 3.0+ environments in order to configure the ServletContext programmatically -- as opposed to (or possibly in conjunction with) the traditional web.xml-based approach.

To answer your below question.

What does it affect? (In a Spring application)

web.xml versioning:

However, if WEB-INF/web.xml is present in the application, its version attribute must be set to "3.0" or greater, otherwise ServletContainerInitializer bootstrapping will be ignored by the servlet container.

You can read the full java doc of WebApplicationInitializer here.

Recency answered 8/7, 2016 at 16:0 Comment(1)
It seems so many hit-and-run downvoters around here, Why no one is ready to put comment while downvote?Recency
A
2

what i understand from the web.xml esp for the part of web-app version="3.0" is the version of your servlet. So, i restrict my answer to the servlet version and why it is so important. As you may know version of your servlet controls most of the other standards when you are programming with Java EE.

For example,

JSF 2.0 requires SERVLET 2.5 comes with JAVA EE5 and WEBLOGIC 10.3.X support all these technologies, for other containers you should check the release notes. Secondly, JSF 2.1 requires SERVLET 3.0 comes with JAVA EE6 and WEBLOGIC 12c supports all these technologies. Of course backward compatibility is ensured, however most of the cases it's possible to have some problems.

when we check the weblogic 12c release notes, i starred the most important technologies supported along with the servlet version. i hope this is also useful for you with the great answer of Miljen Mikic.

Standard    Version
Contexts and Dependency Injection for Java EE   1.0
Dependency Injection for Java EE    1.0
Expression Language (EL)    2.2, 2.1, 2.0
Only JSP 2.0 and greater supports Expression Language 2.x.
JAAS    1.0 Full
JASPIC  1.0
Java API for XML-Based Web Services (JAX-WS)    2.2, 2.1, 2.0
Java API for RESTful Web Services (JAX-RS)  1.1
Java Authorization Contract for Containers (JACC)   1.4
**Java EE   6.0**
Java EE Application Deployment  1.2
Java EE Bean Validation 1.1
Jave EE Common Annotations  1.0
Java EE Connector Architecture  1.6
Java EE EJB 3.1
Java EE Enterprise Web Services 1.3, 1.2, 1.1
Jave EE Interceptors    1.1
**Java EE JDBC  4.0, 3.0**
Java EE JMS 1.1, 1.0.2b
Java EE JNDI    1.2
**Java EE JSF   2.1, 2.0, 1.2, 1.1**
Java EE JSP 2.2, 2.1, 2.0, 1.2, and 1.1
JSP 1.2. and 1.1 include Expression Language (EL), but do not support EL 2.x or greater.
Java EE Managed Beans   1.0
**Java EE Servlet   3.0, 2.5, 2.4, 2.3, and 2.2**
Java RMI    1.0
JavaMail    1.4
JAX-B   2.2, 2.1, 2.0
JAX-P   1.3, 1.2, 1.1
JAX-R   1.0
JAX-RPC 1.1
JCE 1.4
**JDKs  6.0 (aka 1.6), 5.0 (aka 1.5, clients only)**
JMX 1.2, 1.0
JPA 2.0, 1.0
JSR 77: Java EE Management  1.1
JSTL    1.2
Managed Beans   1.0
OTS/JTA OTS 1.2 and JTA 1.1
RMI/IIOP    1.0
SOAP Attachments for Java (SAAJ)    1.3, 1.2
Streaming API for XML (StAX)    1.0
Web Services Metadata for the Java Platform 2.0, 1.1
Amaranth answered 13/4, 2013 at 11:17 Comment(2)
Isn't a servlet a Java class? How is it determined by the version we provide in web.xml? Doesn't it depend on the implementation we are using? You say: Secondly, JSF 2.1 requires SERVLET 3.0, however I have used jsf-impl-2.1.19 successfully with my version being 2.5 in my web-xml ?Archean
Yeah, you may be right but what i noticed is when web.xml is created depending on your servlet version. have u used any jsf 2.1 feature that isn't supported with older versions of jsf in your app?Amaranth

© 2022 - 2024 — McMap. All rights reserved.