You must have seen hundreds of time Dynamic Web Module option in Eclipse. Do you know purpose of this option and what exactly it is for?
Sometime back we have written an article on How to fix “Cannot change version of project facet Dynamic Web Module to 3.0 Error in Eclipse“.
While working on that tutorial we dig some more into difference between Dynamic Web Module 3.0 and 3.1 and would like to share the same with my readers.
Dynamic Web Module
version correlates with Servlet API version
. Ideally a Servlet is an object
that receives a request and generates a response based on that request. Take a look at below image from wikipedia, it’s Servlet API history.
The basic Servlet package defines Java objects to represent servlet requests and responses, as well as objects to reflect the servlet’s configuration parameters and execution environment.
If you use Dynamic Web Module 3.1 with Java 18 the it’s not going to work at all. You need at-least Java 7 with Servlet 3.1 and Dynamic Web Module 3.1.
And that’s the reason, if you want to use version 3.1 you need to use the following proper schema and changes.
Point-1
How to Create a Dynamic Web Project in Eclipse? We will try steps with currently highest available web-app version 3.1
.
First of all, check out the difference between 3.0 and 3.1 schema
. Make sure to put correct .xsd
into web.xml file.
- 3.1 Schema: http://xmlns.
jcp
.org/xml/ns/javaee/web-app_3_1.xsd. - 3.0 Schema: http://java.
sun
.com/xml/ns/javaee/web-app_3_0.xsd
Open web.xml
file (if you don’t see then create one). Put this code into web.xml file.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"> <display-name>CrunchifyTutorials</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
Point-2
Open pom.xml
file and make sure to have latest maven-war-plugin
and maven-compiler-plugin
.
<plugin> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin>
If you want to checkout my pom.xml
file which I’m using for all my Crunchify tutorial then please download it from here.
https://crunchify.com/wp-content/uploads/code/pom.xml
Point-3
Make sure to use latest javax.servlet-api
dependency.
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency>
Point-4
Once you move from Dynamic Web Project from 3.0 to 3.1 then you may see below errors.
Save pom.xml file. Eclipse will build your project automatically and you will see now 3rd Error saying Perform Maven -> Update Project
.
Point-5
- As mentioned about, Right Click on
Project
- Click
Maven
- Click
Update Project
or
- use
Quick Fix
from error console. - And you should be all green 🙂
These are the standard point you should remember to make sure your production style J2EE project is configured correctly in Eclipse.