Anyway I am using the latest Eclipse (Juno) for Java EE developers. The problem is that I can't get the servlet mapping right and I don't really know whether it is my fault or something is wrong with the eclipse.
Anyway I've started with creating a Dynomic Web project and creating a Hello World servlet which automatically is placed in the /Java Resources/src/
HelloWorld.java
package com.myservlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class HelloWorld */ @WebServlet("/HelloWorld") public class HelloWorld extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public HelloWorld() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>Hello World!</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Hello World!</h1>"); out.println("</body>"); out.println("</html>"); } }
So everything is okay there. Then I wanted to configure the mapping so I am looked at Deployment Descriptor: HelloWorld/Servlet Mappings which told me that HelloWorld/webContent/WEB-INF-web.xml does not exists.
here is where it gets tricky: when I create a web.xml file at that location the Deployment Descriptor disappears. Well no biggie I go ahead and map everything manually: which at least according to me and some friends seems to be right. Here is what I am doing just in case:
web.xml:
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4"> <servlet> <servlet-name>HelloWorld</servlet-name> <servlet-class>com.myservlet.HelloWorld</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/HelloWorld</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>welcome.html</welcome-file> </welcome-file-list> </web-app>
So from here on I can't get it to map correctly. I've tried http://localhost:8080/HelloWorld, http://localhost:808...vlet/HelloWorld etc.. all of these return 404.
(I am using tomcat seven which is proven to be configured correctly because I got satisfying results when trying a simple HelloWorld with .jsp.
I have read that if I create a servlet with the wizard (I used the "create servlet" wizard the whole time) the Eclipse will automatically edit the web.xml for me so the servlet will get mapped right. The next part lets me to believe that something is wrong with the Eclipse since when I try to create a servlet after I've manually created a web.xml Eclipse has a strange way of denying me. When I fill in the package and the servlet name the next or finish buttons remain unclickable. When I check in the box that says "Choose an already existing servlet" or something similar it disables all the fields and when I uncheck the box all of the fields remain disabled (uneditable).
So I am kind of stuck and have next to none ideas of what to do next so any solutions/attempts/suggestions are appreciated. Thanks.
[UPDATE]:
Created an identical project with an identical servlet, now without manually creating the web.xml I've right-clicked on the Deployment Descriptor and chose "Generate Deployment Descriptor Stub" not sure of what exactly it did, I am not sure, but I think it has generated some Welcome Pages. It seems to have also generated a web.xml within the WEB-INF (the required location), when I try to look at servlet mappings or the web.xml itself it still says "Resource '/HelloWorld/WebContent/WEB-INF/web.xml' does not exist".
[UPDATE 2]:
Appearently the web.xml has been created (took itself some time to realize that), there was no servlets or servlet mappings in it. So I have generated another servlet (no change to web.xml) so I have manually added the servlet and the mapping. Still have 404 when going to http://localhost ...
[UPDATE 3]:
After editing the web.xml manually the Deployment Descriptor disappeared.
I have realized that I might not have cleaned the Tomcat working directory properly and it might have been configured for an older project... so I am going to try cleaning it and re-build the project.
[UPDATE 4]:
Cleaning directory and remaking the whole project did not affect anything, still having same problems.
Edited by Roman Y, 29 January 2013 - 10:13 AM.