Reputation: 13
I'll apologize in advance. I'm definitely not a Java/Eclipse/Tomcat expert at all. I love the environment once it is working and I can just write code, but getting things set up in the first place... yeah. That's why I'm here.
So I've been trying for a while to get a Servlet loaded and running within Tomcat from inside Eclipse. There are a ton of tutorials on the web for this, but they all seem to break down at some point; I expect a lot of this code/technology changes over time. In my current state, Tomcat will start cleanly without errors. But when I try to actually access my Servlet via URL, I always 404. I figure there could be a few things going on here.
Maybe my Servlet isn't actually being loaded. I'm not sure how exactly to tell this. I learned that Tomcat has a manager Servlet which might prove useful, but it looks to me like when you run Tomcat within Eclipse, it copies much of the Tomcat environment, and does not include Servlets like the manager. Is that correct? If so, how can I tell if my Servlet is being loaded at all?
Maybe my Servlet is loaded and running just fine, and I'm hitting the wrong URL. I hope this is the answer, because it will just become a stupid question from a guy who hasn't done much/any work with Servlets before. Per another question asked here, I added this to the web.xml for my Servlet project:
<servlet>
<servlet-name>SimpleServlet</servlet-name>
<servlet-class>com.pattyanddave.edu.SimpleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SimpleServlet</servlet-name>
<url-pattern>/SimpleServlet</url-pattern>
</servlet-mapping
When I do that, Tomcat fails to start, claiming two different servlets are mapped to that url-pattern. So I figure Eclipse tried to already do this for me, but I can't figure out where/how it did so. To duck this problem, I changed the url-pattern to /SimpleServlet2. Then Tomcat starts fine, but I haven't yet found any URL that will actually hit my Servlet.
Perhaps related, I note that the Servlet class that Eclipse created for me includes an annotation like:
@WebServlet("/SimpleServlet")
prior to the class definition. I guessed this was somehow causing the duplicate binding to the url-pattern, but if I also change that to SimpleServlet2, I don't get the duplicate error upon startup. So I don't know if that annotation is related, or how a pattern put there relates to the patterns in web.xml.
Sorry if the question isn't clear. My hunch is that something funny is happening in the way Eclipse sets up the Tomcat environment within Eclipse.
Upvotes: 1
Views: 3081
Reputation: 8251
web.xml is one way of defining the servlet mapping with the URL and with new version of J2EE you can use the annotation to map the servlet, that is other way of defining servlet mapping. But you should always use any one of them. If you define the web.xml then it overrides the annotation config.
so your url will be
http://{ip}:8080/{projectName}/SimpleServlet
Here /SimpleServlet is the mapping from web.xml. In web.xml if you change the urlpattern like below
<url-pattern>/SimpleServlet2</url-pattern>
then the url will be
http://{ip}:8080/{projectName}/SimpleServlet2
Let me know if this answers your question.
Upvotes: 1