views:

71

answers:

1

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?

A: 

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">
BalusC
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. :-)
Brian Kessler
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/TableServlet
Brian Kessler
That's another problem. Eclipse's JSP/EL syntax validator is an [epic fail](https://bugs.eclipse.org/bugs/buglist.cgi?quicksearch=jsp+el). Just ignore and run it. You'll see that it just works. You can disable that terrible JSP validator in Eclipse prefs. Related questions: http://stackoverflow.com/questions/1790749, http://stackoverflow.com/questions/2268153, http://stackoverflow.com/questions/2975168, etc..etc..
BalusC
You're right! It works.
Brian Kessler
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](http://stackoverflow.com/tags/jsp/info) and [servlets](http://stackoverflow.com/tags/servlets/info) 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.
BalusC
Cheers for the leads. :-)
Brian Kessler