Advertisement



< Prev
Next >



The First Servlet Program



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-






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" -






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 -


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  version="4.0"
  metadata-complete="true">

  <display-name>Welcome tomcat</display-name>
  <description>
     Welcome tomcat
  </description>

<servlet>
 	<servlet-name>FirstServlet</servlet-name>
	<servlet-class>MyServlet1</servlet-class>
</servlet>

<servlet-mapping>
	<servlet-name>FirstServlet</servlet-name>
	<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>

</web-app>


In deployment descriptor file, <servlet> has two child tags <servlet-name> and <servlet-class> :






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)-






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 -

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement