Tomcat and servlets beginner question
Hi all,
I am a recently graduated compsci student and am applying for a job which requires me to write a servlet as part of the application. I have written most of the program and only need to create a servlet of a few lines to finish. The problem is I am having trouble getting Tomcat 7.0 to work and hence cannot test. I would very much appreciate some help with this problem.
I have installed Tomcat successfully and when I go to the url http://localhost:8080 the Tomcat welcome screen appears. However, when I try an click on the link to the example servlets I get a 404 not found error. I am running Windows 7 and have set my classpath variable to point to the servlet-api.jar file. The classpath reads as follows:
classpath=.;c:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar
I have placed a compiled Hello World servlet under the directory:
c:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT\WEB-INF\classes\HelloServlet.class
And have tried to access it with the url:
http://localhost:8080/servlet/HelloServlet
But I get the 404-not found error. I have tried for many hours to get it working to no avail. Is there something else I need to do to get servlets working with Tomcat 7.0?
Thanks
Re: Tomcat and servlets beginner question
When creating a servlet, beside creating the servlet class itself you must register the servlet in your web application. This can be done by either registering your servlet in the web.xml or using the @WebServlet annotation.
Re: Tomcat and servlets beginner question
Thankyou very much wsaryada. I now have it working by defining a servlet-mapping in the web.xml file. Previous versions of Tomcat I have used had an invoker servlet which I was tearing my hair out trying to find in Tomcat 7, but I guess this invoker is no longer used and servlets must be mapped manually. Thanks again.