" Servlet Technology is used to create secure and portable web application. "

Steps to create servlet using tomcat server

To create a servlet application you need to follow some steps. These steps are common for all the Web server. In our example we are using Apache Tomcat server. Apache Tomcat is an open source web server for testing servlets and JSP technology. Download latest version of Tomcat Server and install it on your machine.

After installing Tomcat Server on your machine follow the following steps.

  • Create directory structure for your application.
  • Create a servlet
  • Compile the servlet
  • Create Deployement Descriptor
  • Start the server and deploy the application

Create directory structure

The Sun Microsystem defines a unique directory structure that must be followed to create a servlet application.

servlet directory strucutre

Create the above directory structure inside Apache-Tomcat\webapps directory. All html, static files(images,css etc) are kept directly under Web application folder. All servlet classes are kept inside classes folder. The web.xml(deployement descriptor) file is kept under WEB-INF folder.

Create a servlet

There are three different ways to create a servlet.

  • By implementing Servlet interface
  • By extending GenericServlet class
  • By extending HttpServlet class

But mostly a servlet is created by extending HttpServlet abstract class. As discuss earlier HttpServlet gives the definition of service() method of the Servlet interface. The servlet class that we will create should not override service() method. Our servlet class will override only doGet() or doPost() method.

When a request comes in for the servlet, the Web Container calls the servlet's service() method and depending on the type of request the service() method calls either the doGet() or doPost() method.

NOTE: By default a request is Get request.

import javax.servlet.*;
import javax.servlet.http.*;

public MyServlet extends HttpServlet
public void doGet(HttpServletRequest request,HttpServletResposne response) 
                     throws ServletException
PrintWriter out = response.getWriter();
out.println("<h1>Hello Readers</h1>");

Write above code in a notepad file and save it as anywhere in your pc. Compile it from there and paste the class file into WEB-INF/classes/ directory that you will create inside Tomcat/webapps directory.

Compiling a servlet

To compile a servlet a jar is need to be loaded. Different server require different jar file. In Apache Tomcat server servlet-api.jar file is require to compile a servlet class.

Steps require to compile a servlet

  • Set class path
  • Download servlet-api.jar file
  • Paste servlet-api.jar file inside Java\jdk\jre\lib\ext directory
  • Compile servlet class

NOTE: After compiling your servlet class you will need to paste the class file into WEB-INF/classes/ directory.

Create Deployment Descriptor

Deployment Descriptor(DD) is a XML document that is used by Web Container to run servlets and JSPs. DD is used for several important purpose such as

  • mapping url to servlet class.
  • Initializing parameter.
  • Defining Error page.
  • Security roles.
  • Declaring tag libraries.

We will discuss about all this in detail later. Now we will see how to create a simple web.xml file for a web application

web.xml file

Start the server

Double click on startup.bat file to start Apache Tomcat Server.

Or, execute the following command on your window machine.


Starting Tomcat Server for the first time

If you are starting Tomcat Server for the first time you need to set JAVA_HOME in Enviroment variable. The following steps will show you how to set it.

  • Rigt Click on My Computer, go to Properites
  • setting JAVA_HOME for Tomcat

  • Go to Advance Setting and Click enviroment variable
  • setting JAVA_HOME for Tomcat

  • Click on New tab, enter JAVA_HOME inside Variable name column and path of JDK inside Variable value column. Click ok
  • setting JAVA_HOME for Tomcat

Run Servlet Application

Open Browser and type http:localhost:8080/First/hello

Running servlet application