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
Contents
Creating a HTML Page
First thing we will do is create a HTML page called index.html with the following code in it:
<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:
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:
<?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 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
- 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 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:
http://localhost:8080/helloworld/
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
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Leave a Reply