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:
- Create a new Java class and extend it from the javax.servlet.http.HttpServlet class.
- Override the doGet() or doPost() method (depending on the HTTP method you want to handle) in the class.
- In the doGet() or doPost() method, write the code that should be executed when the servlet is called.
- Compile the servlet class and create a deployment descriptor (web.xml) file that maps the URL pattern to the servlet class.
- Package the servlet class and the deployment descriptor in a web archive (WAR) file.
- Deploy the WAR file to a servlet container, such as Apache Tomcat.
- 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorldServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>Hello World!</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Hello World!</h1>"); out.println("</body>"); out.println("</html>"); } } |
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”:
1 2 3 4 5 6 7 8 9 10 11 12 |
<web-app> <servlet> <servlet-name>HelloWorld</servlet-name> <servlet-class>HelloWorldServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app> |
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:
- Compile the servlet class using the Java compiler. For example, you can use the command “javac HelloWorldServlet.java” to compile the class.
- 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.
- Create a new file named “web.xml” in the WEB-INF directory and paste the code provided in the previous answer.
- Create a new WAR file by using the command “jar -cvf HelloWorld.war *” in the directory where you have the WEB-INF directory.
- 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.
- Start the servlet container by running the startup script located in the bin directory of the Tomcat installation.
- 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<html> <head> <title>Hello Servlet</title> </head> <body> <form action="HelloServlet" method="get"> <input type="submit" value="Invoke Servlet!"> </form> </body> </html> |
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:
1 2 3 4 5 6 7 8 9 10 11 |
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().write("Hello World!"); } } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0" ?> <web-app> <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/HelloServlet</url-pattern> </servlet-mapping> </web-app> |
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.