No web.xml in Eclipse + Glassfish v3?
Asked Answered
C

5

8

I created a simple "hello world" servlet in Eclipse (helios) + Glassfish v3. I am using Glassfish's plugin for eclipse It seems there is no web.xml but a sun-web.xml in the WEB-INF/ folder. This is my first time with glassfish but was a bit surprised at the absence of web.xml - so here are some of the problems:

  1. Where do I check for url-mappings for the servlet? On creating a new Servlet in Eclipse it asks me for a URL-mapping but I'm unable to find it anywhere in any .xml file where I can tweak the settings.
  2. If there isn't any web.xml, creating it from scratch will be quite error prone. What do you suggest? Google for a sample and play around? Shouldn't one be auto-created?
  3. Has anyone encountered this? I tried looking up the difference between web.xml and sun-web.xml but the results weren't at all enlightening. I wouldn't want to learn another xml for configuration purposes and that too glassfish specific.

We have to configure servlet contexts, mappings etc especially during development/testing but the sheer absence of web.xml has me stumped.

Collaborate answered 24/2, 2011 at 2:34 Comment(0)
C
12

Eclipse allows you to not create a web.xml file when you create Dynamic Web Project for Java EE 6, since the Java EE 6 spec (in general) and Servlet 3.0 spec (in particular) attempt to de-emphasize deployment descriptors.

You can use annotation to provide all the data that had been included in the web.xml file. The Javadoc for the Servlet 3.0 annotations is pretty obtuse. You should read through the Servlet 3.0 spec from the jcp.org site to get a bit more explanatory text.

To change the url-mapping for a Servlet 3.0 servlet, the first place to look is in the source code for the servlet. Look for (and change) the value of the urlPatterns element.

If you are trying to create a web app based on Servlet 3.0, try to avoid creating a web.xml file.

The sun-web.xml/glassfish-web.xml file is used to 'finish' the description of a war file for deployment into a GlassFish container.

One other note about the annotations like WebServlet... they do not integrate your annotated class into the class hierarchy, so the correct use of @WebServlet would look like

@WebServlet(
    name = "MyServlet", 
    urlPatterns = {"/path_to_servlet"}
)
public class MyServlet extends HttpServlet {}
Coniine answered 24/2, 2011 at 7:30 Comment(3)
That's exactly what I was looking for! I did find it in bits and pieces but a cohesive explanation is more welcome! Thanks!Collaborate
You can right-click the eclipse project and select "Java EE Tools" -> "Generate Deployment Descriptor Stub" to generate it.Stockpile
From Servlet 3 specification-> 10.13 Inclusion of a web.xml Deployment Descriptor: web application is NOT required to contain a web.xml if it does NOT contain any Servlet, Filter, or Listener components or is using annotations to declare the same. In other words an application containing only static files or JSP pages does not require a web.xml to be present.Jara
A
10

If you find you do need a web.xml file, you can context-click on the deployment descriptor in the Project Explorer view and there should be an option "Generate Deployment Descriptor Stub". That will create a web.xml for you with the display-name and welcome-file-list elements.

Ageold answered 25/5, 2012 at 15:28 Comment(0)
C
4

It seems that it's a bad habit to click "Finish" when you create a 'New > Dynamic Web Project' - You should keep clicking 'next' and go the last window-pane where you select "generate web.xml deployment descriptor" - seems it's unchecked by default.

Well that says I've been a bit rusted with creating web-apps. And here I thought it was a glassfish specific issue.

Collaborate answered 24/2, 2011 at 3:25 Comment(2)
I was going to post an answer with a link to this screenshot :) Good you found it out yourself.Injun
@BalusC: I knew I could count on you :DCollaborate
P
3

Because of Glassfish 3.x is fully certified Java EE 6 server, it supports Servlets 3.0. Starting from Servlets 3.0, it is possible to specify web.xml settings through annotations.

For example

@WebServlet(
    name = "MyServlet", 
    urlPatterns = {"/path_to_servlet"}
)
public class MyServlet {}
Pelagia answered 24/2, 2011 at 3:22 Comment(0)
A
0

To add to what TMN said, I noticed that the project explorer would not show the Deployment Descriptor until I performed an SVN update for some reason. If you have that problem, try updating your code. I was on the HEAD revision already but for some reason the update showed that view.

Actinic answered 25/9, 2012 at 14:17 Comment(1)
This should be a comment, @Clete2.Triggerfish

© 2022 - 2024 — McMap. All rights reserved.