Thursday, June 17, 2010

Tomcat Servlets

Getting a Servlet Up-and-Running

1. Servlets consist of a java class typically derived from HttpServletClass and they implement the doPost and doGet methods of this class.

2. Create the class file for your java servlet class, for example a class called com.mine.test.HelloWorld - the file is located in the directory com/mine/test/HelloWorld.class

3. In webapps directory in the tomcat folder, create a new directory e.g. "helloservlet". This is the application root directory for the servlet.

4. In this directory create the following directory
WEB-INF
WEB-INF \ classes

5. Copy the entire class file directory structure under WEB-INF \ classes. The result is:
WEB-INF \ classes \ com \ mine \ test \ HelloWorld.class

5. Create the file web.xml in the WEB-INF directory. It should contain the following:

< web-app >
< servlet >
< servlet-name >HelloServlet< /servlet-name >
< servlet-class >com.sams.test.HelloServlet< /servlet-class >
< load-on-startup >1< /load-on-startup >
</servlet >

< servlet-mapping >
< servlet-name >HelloServlet< /servlet-name >
< url-pattern >/hs< /url-pattern >
< /servlet-mapping >
< /web-app >

6. Start the tomcat server

7. Launch a web browser and navigate to the following url:
http://localhost:8080/helloservlet/hws