Brian Kessler Brian Kessler - 1 year ago 115
Java Question

Why is creating Servlets in Eclipse breaking my web.xml?

Being somewhat lazy, I was rather happy to find that I could create a new servlet source code by going New -> Servlet, instead of going New -> Class and then editing the class into a servlet.

However, I have discovered that every time I create a new servlet in Eclipse, Eclipse modifies my web.xml.

Specifically, it modifies the top element to:

id="WebApp_ID" version="2.4">

(Linebreaks mine.)

This doesn't seem necessarily bad, but then it modifies various sub-elements by putting "javaee:" in front of their name, to indicate that these elements belong in that namespace.

For example, it changes




After which eclipse then complains about all the elements it modified, giving me notations like:

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

To make matters worse, when I use find and replace to delete all to "javaee:" which litters the file, Eclipse still complains about these even though they are no longer there. I must copy and paste the entire remaining file on top of itself to make these complaints go away.

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

  1. Make it stop doing this?

  2. Take advantage of whatever it is trying to do, and make it work for me instead of against me?

Answer Source

I have never seen this before, but this indicates that your Eclipse project is really messed up. At least the web.xml root declaration makes no utter sense. It look like a mix of Servlet 2.4 and 2.5 specifications. Maybe Eclipse is getting confused because the root namespace (xmlns) is pointing to the Servlet 2.4 one (with j2ee URI) while the web project itself is set as Servlet 2.5 or newer (which should be using the one with javaee URI).

Also, when your web project is set to Servlet 3.0 during creation, by default no web.xml will be generated by Eclipse because of the new Servlet 3.0 annotations like @WebServlet, @WebFilter, etc.. which makes the web.xml superfluous. When you create new servlets by New > Servlet, Eclipse will already autogenerate those annotations. Probably you've attempted to create the web.xml yourself based on misinformation.

I'd suggest to backup some code if necessary, throw the whole project away and create a new one with the proper settings and do not touch the web.xml root declaration.

Assuming that you're using the latest Eclipse version, Helios SR1 for Java EE developers, rightclick Eclipse's Project Explorer, choose New > Dynamic Web Project and just fill the project name and keep everything default. Click Next until the last step and then tick Generate web.xml deployment descriptor checkbox to let Eclipse generate one. The root declaration should then look like this:

    id="WebApp_ID" version="3.0">
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download