logo

How to generate web.xml in Eclipse

How to easily generate deployment descriptor (web.xml) in Eclipse for various Java EE versions? With examples for Java EE 7, Java EE 6 and Java EE 5

When you start a new web project, you often do not have a web.xml file. But usually after some time you need it, so how to effectively create it? You can either create it by hand, in this video tutorial description are examples of web.xml files for various Java EE versions. Or you can generate it straight from Eclipse.

 

For example when you create a new Maven project, web.xml was not generated. First make sure you are in a Java EE perspective. Next in webapp directory create WEB-INF directory, right click on Deployment Descriptor and select "Generate Deployment Descriptor Stub". 

 

This will generate Java EE 5 web.xml file (Servlet version is 2.5). If you want to change it, goto project Properties -> Project Facets, look for "Dynamic Web Module" and change version (btw. this is Servlet specification version):

 

- 2.5 is Java EE 5

- 3.0 is Java EE 6

- 3.1 is Java EE 7

 

You will also have to increase Java version, because each Servlet specification requires at least some Java version and for example Servlet specification 3.0 requires at least Java 6.

 

Now let's generate Java EE 6 web.xml file.

 

If you use Maven, your work is not yet done, because if you leave your project as it is now, you will later encounter an error that Java version in facet does not correspond to Java version which Maven uses for your project. That's why you need to add this plugin to pom.xml:

 

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.1</version>
      <configuration>
        <source>1.6</source>
        <target>1.6</target>
      </configuration>
    </plugin>
  </plugins>
</build>

 

And finally ALT + F5 to update Maven project and you're done.

 

Java EE 7 web.xml:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
            http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
</web-app>

 

Java EE 6 web.xml:

<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">
</web-app>

 

Java EE 5 web.xml:

<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_2_5.xsd"
      version="2.5">
</web-app>

comments powered by Disqus

rss

Free online Java web application examples

Want to develop whole web applications which use database, security and more? See my github account, where are lot's of example projects:
github

Search