user3732616
user3732616

Reputation: 13

Cannot tell if my Servlet is being loaded by Tomcat under Eclipse

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.

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

Answers (1)

A Paul
A Paul

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

Related Questions