In this article, we are going to teach you how to run your first Servlet program.
Before we execute a Servlet program, we need to make sure that our Tomcat web server is up and running. Let's see how to do that.
We are going to start Tomcat. For this, you need to open the Command Prompt and
go to the bin directory of the Tomcat folder present in the C: Drive, such as C:\apache-tomcat-9.0.2\bin
and now you type startup(a command which starts the Tomcat Server) and press Enter.
Pressing the Enter after startup command starts the Tomcat web server as shown below.
That's it! Tomcat web server is up and running and we don't have to close the server window till the time we are working
on Tomcat.
Making sure that Tomcat is running
Just to make sure that Tomcat web server is running is to open your web browser and enter the URL -
http://localhost:8080/, which opens the window with a congratulatory message for successful installation and execution of Tomcat.
Now as our Tomcat web server is running, let's create a Servlet class.
Creating the Servlet class
A Servlet class is just a regular Java class which ends with a .java extension, hence we have named this file MyServlet1.java.
MyServlet1.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet1 implements Servlet
{
private ServletConfig config;
public void init(ServletConfig sc)
{
System.out.println("Initializing the Servlet");
}
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
{
PrintWriter out = response.getWriter();
out.println("Hello there!");
System.out.println("Putting the Servlet in service");
}
public String getServletInfo()
{
return "Our first Servlet";
}
public ServletConfig getServletConfig()
{
return config;
}
public void destroy()
{
System.out.println("Destroying the servlet");
}
}
Advertisement
Directory Structure of Servlet files
The diagram above depicts how to arrange the Servlet files in a specific directory structure, as per Java Servlet Specification-
Project Folder - This folder could be a root
folder already present within
the Tomcat webapps folder or it could be a folder created by you containing Servlet class(.java) file.
WEB-INF - This folder should contain the deployment descriptor file(web.xml) of your Java Servlet program.
classes - This folder should contain the compiled(.class) form of your Java Servlet class.
Where to save the Servlet(.java) file?
In order to allow Tomcat web server to easily find this Servlet class and execute it, we must store this class in any of the
two locations shown in the diagram displaying "Servlet Directory Structure" -
1) In the ROOT folder(stored within webapps folder of your Apache Tomcat directory).
In our case, the path to the ROOT folder is - C:\apache-tomcat-9.0.2\webapps\ROOT
2) Or, we could create a new folder(with any name) in the webapps folder of Tomcat and store our Servlet class MyServlet1.java in it.
For example, we can create a new folder Serv within the webapps folder of Tomcat and store MyServlet1.java within it.
Creating the Deployment Descriptor file
As per the Java Servlet specifications, every web application based on Servlet must have a Deployment Descriptor file(an XML file) named web.xml.
So, let's create one -
In deployment descriptor file, <servlet> has two child tags <servlet-name> and <servlet-class> :
<servlet-name> tag is used to specify a unique name for our Servlet class, we have given it a unique name FirstServlet.
<servlet-class> tag is used to specify the full qualified name of the Servlet class and in our example our Servlet class is named MyServlet1.
Where to save the (web.xml) file?
In order to allow Tomcat web server to easily find the Deployment Descriptor xml file, we must store it in any of the
two locations(depending upon where you stored your Servlet class file)-
1) If you stored your Servlet class file within the root folder then you should store your web.xml in the WEB-INF folder under the same root folder.
2) If you stored your Servlet class file in a newly created project folder under the webapps folder of Tomcat then we should store our web.xml file
within WEB-INF folder of this project folder.
Setting the classpath
Much of the support for developing the web applications based on the Java Servlet technology does not come with the core Java.
Hence, in order to compile the Servlet programs, we have to set the classpath to a jar file named
servlet-api.jar.
This jar file provides all the classes that are required for the Servlet programming and it comes within the lib Folder of
Tomcat installation folder.
For example, in our case we have installed Tomcat Web Server within the C: Drive, hence the path to our lib folder containing the servlet-api.jar is - C:\apache-tomcat-9.0.2\lib
There are two ways to set the classpath -
You could set the classpath by entering this command at the Command Prompt, but this only temporarily sets the classpath to the path of servlet-api.jar file. If you restart your system, you will have to set the classpath again.
set classpath=C:\apache-tomcat-9.0.2\lib\servlet-api.jar
In order to set the classpath permanently, you need to set your classpath system variables. A simple tutorial on how to permanently set the classpath system variables, you may read
How to permanently set the classpath system variable
Compiling the Servlet class
After setting the classpath, you need to compile the Servlet class by entering the command at the folder where you've stored the Servlet class file.
javac -d WEB-INF/classes MyServlet2.java
Executing the Servlet
Next, we need to enter the URL of this Servlet in the address bar of our web browser. This URL may look a little
different depending on where you store this file.
1) If the Servlet class file is stored in the ROOT folder at location - "C:\apache-tomcat-9.0.2\webapps\ROOT"
The URL to execute this JSP webpage would be http://localhost:8080/MyServlet.jsp, where MyServlet is the specified in the url-pattern tag within web.xml file.
2) If the Servlet file is stored in the a new folder created by you(within webapps folder) then we need to give the full path to the file(including
the new folder which contains the Servlet class file)
For example, if we stored MyServlet1.java in the folder Serv then the URL to execute this file would be
http://localhost:8080/Serv/MyServlet
Where, MyServlet is the specified in the url-pattern tag within web.xml file.
Serv is the name of the folder within webapps folder, in which you've stored the Servlet class.