Java

How to Create and Execute a Servlet Manually7 min read

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.

To create a servlet manually, you will need to follow these steps:

  1. Create a new Java class and extend it from the javax.servlet.http.HttpServlet class.
  2. Override the doGet() or doPost() method (depending on the HTTP method you want to handle) in the class.
  3. In the doGet() or doPost() method, write the code that should be executed when the servlet is called.
  4. Compile the servlet class and create a deployment descriptor (web.xml) file that maps the URL pattern to the servlet class.
  5. Package the servlet class and the deployment descriptor in a web archive (WAR) file.
  6. Deploy the WAR file to a servlet container, such as Apache Tomcat.
  7. Start the servlet container and test the servlet by sending an HTTP request to the mapped URL.




To execute a servlet, you would need to deploy the servlet on a servlet container (such as Apache Tomcat, Jboss, Glassfish etc) and then access it by hitting the URL pattern mapped in the web.xml file.

Example 1, Here is an example of a simple servlet class that extends the HttpServlet class and overrides the doGet() method:

To deploy the servlet, you would need to package it in a WAR file along with a web.xml deployment descriptor file that maps the servlet to a specific URL pattern. For example, the following web.xml file maps the servlet to the URL pattern “/hello”:

Once you have packaged the servlet class and the web.xml file in a WAR file, you can deploy the WAR file to a servlet container, such as Apache Tomcat. After starting the servlet container, you can test the servlet by sending an HTTP GET request to the URL “http://localhost:8080/context-root/hello” (assuming that the servlet container is running on the localhost and on port 8080). This will display a “Hello World!” message in the browser.

In order to continue deploying the servlet manually, you will need to take the following steps:

  1. Compile the servlet class using the Java compiler. For example, you can use the command “javac HelloWorldServlet.java” to compile the class.
  2. Create a new directory named “WEB-INF” in the same directory where the servlet class is located. Inside the WEB-INF directory, create a new directory named “classes” and move the servlet class file (HelloWorldServlet.class) to the classes directory.
  3. Create a new file named “web.xml” in the WEB-INF directory and paste the code provided in the previous answer.
  4. Create a new WAR file by using the command “jar -cvf HelloWorld.war *” in the directory where you have the WEB-INF directory.
  5. Deploy the WAR file to a servlet container such as Apache Tomcat by copying the WAR file to the webapps directory of the Tomcat installation.
  6. Start the servlet container by running the startup script located in the bin directory of the Tomcat installation.
  7. Test the servlet by accessing the URL “http://localhost:8080/HelloWorld/hello” in a web browser. This should display the “Hello World!” message.

Note that the steps and commands may vary depending on the operating system and the servlet container you are using.

Also, deploying servlet manually is not recommended in production environment and you can use tools like maven, gradle, ant etc to automate the process.

Example 2, 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 HelloServlet.java 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 andservlet-mapping. The servlet tag specifies the name of the servlet class file and the servlet-patterntag contains information on which URL pattern the servlet should be invoked.

Deploying the Application

At this point you will have three files:

  • index.html
  • HelloServlet.java
  • web.xml

Now, compile the HelloServlet.java 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:

javac  HelloServlet.java

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 theworkfolder we should create a sub folder named WEB-INF. Again in WEB-INF, we need to createclasses sub folder and lib sub folder. The classes folder contains the compiled .class files and libfolder 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:

http://localhost:8080/helloworld/

That’s it! Your application is running.

Take your time to comment on this article.

Leave a Comment