Why does creating servlets in Eclipse violate my web.xml?

Being somewhat lazy, I was very happy to find that I could create new servlet source code by going to New → Servlet, instead of going New → Class, and then editing the class in the servlet.

However, I found that every time I create a new servlet in Eclipse, Eclipse changes my web.xml.

In particular, it changes the top element to:

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4"> 

(Linebreaks mine.)

This does not seem bad, but then it modifies the various subelements by putting "javaee:" in front of its name to indicate that these elements belong to this namespace.

For example, it changes

 <display-name>ShowLifecycles</display-name> 

to

 <javaee:display-name>ShowLifecycles</javaee:display-name> 

After which eclipse then complains about all the elements that it changed, and gave me such notation as:

 cvc-complex-type.2.4.a: Invalid content was found starting with element 'javaee:display-name'. One of '{"http:// java.sun.com/xml/ns/j2ee":description, "http://java.sun.com/xml/ns/j2ee":display-name, "http://java.sun.com/xml/ns/ j2ee":icon, "http://java.sun.com/xml/ns/j2ee":distributable, "http://java.sun.com/xml/ns/j2ee":context-param, "http:// java.sun.com/xml/ns/j2ee":filter, "http://java.sun.com/xml/ns/j2ee":filter-mapping, "http://java.sun.com/xml/ns/ j2ee":listener, "http://java.sun.com/xml/ns/j2ee":servlet, "http://java.sun.com/xml/ns/j2ee":servlet-mapping, "http:// java.sun.com/xml/ns/j2ee":session-config, "http://java.sun.com/xml/ns/j2ee":mime-mapping, "http://java.sun.com/xml/ns/ j2ee":welcome-file-list, "http://java.sun.com/xml/ns/j2ee":error-page, "http://java.sun.com/xml/ns/j2ee":jsp-config, "http:// java.sun.com/xml/ns/j2ee":security-constraint, "http://java.sun.com/xml/ns/j2ee":login-config, "http://java.sun.com/xml/ns/ j2ee":security-role, "http://java.sun.com/xml/ns/j2ee":env-entry, "http://java.sun.com/xml/ns/j2ee":ejb-ref, "http:// java.sun.com/xml/ns/j2ee":ejb-local-ref, "http://java.sun.com/xml/ns/j2ee":service-ref, "http://java.sun.com/xml/ns/ j2ee":resource-ref, "http://java.sun.com/xml/ns/j2ee":resource-env-ref, "http://java.sun.com/xml/ns/j2ee":message- destination-ref, "http://java.sun.com/xml/ns/j2ee":message-destination, "http://java.sun.com/xml/ns/j2ee":locale- encoding-mapping-list}' is expected. 

To make matters worse, when I use find and replace to remove everything so that the "javaee:" that places the file, Eclipse still complains about them, even if they are no more. I have to copy and paste the entire remaining file on top of myself so that these complaints disappear.

I am sure that Eclipse is trying to be useful, expecting some desire or need for this namespace. How can I do one of two things:

  • Do not stop doing this?

  • Take advantage of what he is trying to do and make him work instead of me instead of me?

+7
java eclipse servlets xml-namespaces
source share
4 answers

I have never seen this before, but this indicates that your Eclipse project is really messed up. At the very least, the web.xml root declaration does not make full sense. It looks like a combination of Servlet 2.4 and 2.5 specifications. Perhaps Eclipse gets confused because the root namespace ( xmlns ) points to Servlet 2.4 (with the j2ee URI), and the web project itself is set to Servlet 2.5 or new (which should use one that has a javaee URI).

In addition, when your web project is configured to Servlet 3.0 at creation time, by default no web.xml will be generated by Eclipse due to new annotations of servlet 3.0 such as @WebServlet , @WebFilter , etc., which makes the web.xml redundant. When you create new servlets using New> Servlet, Eclipse already auto-generates these annotations. Perhaps you tried to create web.xml yourself based on misinformation.

I would suggest, if necessary, back up the code, discard the entire project and create a new one with the correct settings and not touch the web.xml root declaration.

Assuming you are using the latest version of Eclipse, Helios SR1 for Java EE Developers, right-click Eclipse Project Explorer, choose New> Dynamic Web Project and just fill in the project name and keep all the defaults. Click “Next” to the last step, and then check “Create web.xml deployment descriptor” so that Eclipse generates it. The root declaration should look like this:

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

I had a similar problem. I brought the web application from old Tomcat 6 / Java 6 to Tomcat 7 / Java 7. I copied and pasted the existing web.xml object into the new 3.0 web.xml 3.0. Everything was fine until I added a servlet with parameters init-param, display-name, description and load-on-startup. Eclipse marked these parameters as broken by "invalid content found in ...". Thanks to stackoverflow, I discovered that load-on-startup should have appeared after init-params, but only RTFM for how they knew. My other options were still marked as broken. This is what the web.xml header looked like when it was broken:

 <?xml version="1.0" encoding="UTF-8"?> <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"> 

I changed dtds to ns / j2ee and web-app_2_5.xsd and Ecliupse stopped complaining. But its 2014, and we will soon move to Tomcat 8. So I dug hard and found the documents. In web application 3.0, the parameters should be in the following order:

  • jee: descriptionGroup = any combination of three parameters: description, display-name, icon
  • servlet name
  • chose 1: servlet class or jsp file
  • n number of init-params
  • load at start
  • parameters: enabled, asynch, run-as, security-role-ref, multipart-config supported

As soon as I put my options in order, Eclipse was pleased with ns / javaee and web-app_3_0.xsd.

+1
source share

Move "display-name" as the first element in the "servlet" tag, the validation error should disappear.

+1
source share

Make sure the web.xml starts with a tag followed by a tag

 <?xml version="1.0" encoding="UTF-8"?> 

In my case, I had a DOCTYPE tag and removing the DOCTYPE tag from web.xml cleared me of this error,

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >

0
source share

All Articles