Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

<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 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

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

to

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

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://
 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 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?

like image 716
Brian Kessler Avatar asked Oct 24 '10 11:10

Brian Kessler


People also ask

Is Web xml a servlet?

web. xml is part of the servlet standard for web applications.

How do I create a Web xml file in eclipse?

xml in Eclipse, even though it's in the webapps\ch11\WEB-INF directory, you can make it a linked file . To do that, right-click the ServletInPlace project, select New→ File, click the Advanced button, check the “Link to file in the file system” checkbox, and click the Browse button.

What is servlet xml?

xml - It is standard spring context file which contains all beans and the configuration that are common among all the servlets. It is optional file in case of web app. Spring uses ContextLoaderListener to load this file in case of web application. Spring-servlet. xml- It is a single entry point for Spring.

Does spring boot use Web xml?

Application Configuration Spring MVC web applications use the web. xml file as a deployment descriptor file. Also, it defines mappings between URL paths and the servlets in the web. xml file.


3 Answers

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:

<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">
like image 58
BalusC Avatar answered Sep 25 '22 20:09

BalusC


I was having a similar issue. I brought a web app over from an old Tomcat 6/Java 6 to Tomcat 7/Java 7. I copy and pasted existing web.xml body into the new 3.0 web.xml. Everything was fine until I added a servlet that had init-param, display-name, description and load-on-startup paramters. Eclipse flagged these params as broken with an "invalid content found at...". Thanks to stackoverflow, I found that load-on-startup had to come after init-params but only a RTFM for how they knew. My other params were still flagged 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 the dtds to ns/j2ee and web-app_2_5.xsd, and Ecliupse stopped complaining. But its 2014 and we'll be moving to Tomcat 8 soon. So I dug in hard and found the docs for . In web-app 3.0 the parameters have to be in this order:

  1. jee:descriptionGroup = any combo of 3 params: description, display-name, icon
  2. servlet-name
  3. chose 1: servlet-class or jsp-file
  4. n number of init-params
  5. load-on-startup
  6. options: enabled, asynch-supported, run-as, security-role-ref, multipart-config

Once I put my params in order, Eclipse was happy with ns/javaee and web-app_3_0.xsd.

like image 43
Ed Pike Avatar answered Sep 27 '22 20:09

Ed Pike


Move "display-name" as the first element under "servlet" tag, the validation error should go away.

like image 33
Vayu Avatar answered Sep 28 '22 20:09

Vayu