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

4

7

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?

Werbel answered 24/10, 2010 at 11:30 Comment(11)
this is one good reason to avoid Eclipse wizardsRecalcitrant
Can you use servlet 3.0?Keene
@Keene - I'm a novice when it comes to servlets. I probably could use servlet 3.0, but I don't even know which version I'm currently using (I think the latest) or how to tell it to use a different version.Werbel
@matt b - That would be the easiest solution, but rather than avoid it, I'd prefer to understand it. :-)Werbel
Grab Tomcat 7.0 and use servlet 3.0. WTP has support for it as well, so give it a try.Keene
@Keene - I am already using Tomcat 7.0 ... anything specific I should do to make it use servlet 3.0?Werbel
Check your project facets in Eclipse. The version of Dynamic Web Module should be 3.0Keene
@Keene - Cheers for the info. For better or worse, that's exactly the version I have, so this doesn't resolve this issue.Werbel
Your web.xml claims you are using 2.4 rather than 3.0. There is some mismatch ;) Can you start clean?Keene
@Keene - That may be from either the tutorial I'm currently working through or one before. I'll try changing it to 3.0 to see if it helps (or hurts).Werbel
@Keene - Doesn't seem to change anything. Eclipse behaves the same.Werbel
C
11

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">
Casper answered 24/10, 2010 at 19:30 Comment(4)
I think you've called it. My book on JSP is pretty old and some other tutorials I looked at were pretty old. I was creating the web.xml because they all instructed me to. I saw the annotations in the servlet Eclipse created and read somewhere that this meant the web.xml wasn't needed, but then the new servlets weren't working without being added after fixing the breaking web.xml for the old ones. I'll try this going forward. :-)Werbel
That works, except one thing: how should I include servlets in a <jsp:include> tag? For example, I now have a TableServlet, but if I put <jsp:include page="/TableServlet" flush="true"> in the JSP, I get this error: Multiple annotations found at this line: - Fragment "/TableServlet" was not found at expected path /SamsTeachYourselfJSP_2/ WebContent/TableServlet - Fragment "/TableServlet" was not found at expected path /SamsTeachYourselfJSP_2/ WebContent/TableServletWerbel
That's another problem. Eclipse's JSP/EL syntax validator is an epic fail. Just ignore and run it. You'll see that it just works. You can disable that terrible JSP validator in Eclipse prefs. Related questions: stackoverflow.com/questions/1790749, stackoverflow.com/questions/2268153, stackoverflow.com/questions/2975168, etc..etc..Casper
Truly it works :) By the way, including a servlet in a JSP is a bit a smell. Anyway, you're still learning, huh? May I be so kind to suggest you to read both the jsp and servlets tag info pages to properly dive into the basics? At the bottom of the tag info pages you can find very useful links pointing to a wealth of information.Casper
T
1

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.

Terena answered 17/6, 2014 at 21:3 Comment(1)
Here is the original stackoverflow fix for the <load-on-startup> problem. This is where I found the definition of jee:descriptionGroup. The top level dtd on <servlet> (aka servletType) web-common_3_1.xsdTerena
H
1

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

Haire answered 5/8, 2016 at 14:3 Comment(0)
S
0

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

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

In my case I had DOCTYPE tag and removing the DOCTYPE tag from the web.xml cleared me 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" >

Surd answered 5/8, 2015 at 10:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.