Advanced Java and Web Technologies for JNTUK
Blog providing beginner tutorials on different web technologies like HTML, CSS, Javascript, PHP, MYSQL, XML, Java Beans, Servlets, JSP and AJAX

02/10/2016 Categories: Servlets. No Comments on Creating and Executing a Servlet Manually

Here we will learn how to create and execute a servlet manually i.e., without using any IDEs. For developing web applications using servlets you need a application server like Apache Tomcat, and a text editor like notepad.


Video: Apache Tomcat Installation


A servlet is a normal java class which follows these set of rules:

  • Should be a public non-abstract class.
  • Should be a subtype of servlet.Servlet interface or  HttpServlet  class.
  • Should contain a zero or no argument constructor.
  • The inherited methods from the Servlet interface should not be declared as final.


Following are the basic steps for creating and executing a servlet:

  • Create a HTML page (optional)
  • Create the Servlet file
  • Create the web.xml (deployment descriptor) file
  • Deploy the application


Creating a HTML Page


First thing we will do is create a HTML page called index.html with the following code in it:



The above page displays a button with Invoke Servlet! text on it. When the user clicks on that button, server executes the servlet named HelloServlet.


Creating Servlet File


Next step is to create a servlet file called with the following code in it:



In the above code we created a class HelloServlet which extends the sub class HttpServlet. Our class contains the service method named doGet(). In this method we are printing the message Hello World! on the page using the method write().


Creating web.xml File


Next step is to create the deployment descriptor file named web.xml with the following code:



This file contains XML code. The root tag is web-app. It contains two main sub tags: servlet and servlet-mapping. The servlet tag specifies the name of the servlet class file and the servlet-pattern tag contains information on which URL pattern the servlet should be invoked.


Deploying the Application


At this point you will have three files:

  • index.html
  • web.xml


Now, compile the file. To compile it, we have to set the CLASSPATH environment variable. Open command prompt (cmd) and type the following:


set classpath=C:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.27\lib\servlet-api.jar


Now, compile the file with javac command like:




After successful compilation, a class file will be created, HelloServlet.class. Now, arrange the 4 files according to the following directory structure:




The workfolder is the top-level directory which contains .html, .css, .jsp, .java, etc., files. In the workfolder we should create a sub folder named WEB-INF. Again in WEB-INF, we need to create classes sub folder and lib sub folder. The classes folder contains the compiled .class files and lib folder contains any third-party .jar files (ex: database connectivity .jar files). The WEB-INF folder also contains the web.xml file.


Create a WAR file using the jar tool. Navigate to your work folder and type the following command at the command prompt:


jar  -cvf  helloworld.war  *


A helloworld.war file will be created in the work folder.


Executing the Application


Create a environment variable JAVA_HOME with value set to the JDK home directory. This is not need for the latest version of apache tomcat server. Start tomcat server. Copy the helloworld.war file into tomcat’s webapps folder.


Open a browser like firefox and in the address bar type:




That’s it! Your application is running.


Video: Creating and Executing Servlet Manually


Video: Creating and Executing Servlet using Eclipse IDE


Video: Servicing GET and POST Requests

Related Links:

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up