< Prev
Next >



HttpServlet class



Using the HttpServlet class, we could make a Servlet able to handle HTTP requests made by the user and revert back with an appropriate HTTP response. HttpServlet is an abstract class which extends abstract class GenericServlet. HttpServlet class can also be used to create a Servlet. HttpServlet class is part of the Servlet API and the full path to import this class is javax.servlet.http.HttpServlet.




HttpServlet methods


HttpServlet abstract class extends GenericServlet abstract class(which implements Servlet and ServletConfig interface) . HttpServlet methods allow us to handle any sort of HTTP request made by the user, whether its GET, POST, DELETE etc by providing appropriate methods to handle each of these HTTP requests.


Let's take a look at the methods part of javax.servlet.http.HttpServlet class.

Methods Description
void doGet(HttpServletRequest req, HttpServletResponse res) This method allows a Servlet to handle the get request.
void doPost(HttpServletRequest req, HttpServletResponse res) This method allows a Servlet to handle the post request.
void doPut(HttpServletRequest req, HttpServletResponse res) This method allows a Servlet to handle the put request.
void doTrace(HttpServletRequest req, HttpServletResponse res) This method allows a Servlet to handle the trace request.
void doHead(HttpServletRequest req, HttpServletResponse res) This method allows a Servlet to handle the HTTP head request.
void doDelete(HttpServletRequest req, HttpServletResponse res) This method allows a Servlet to handle the delete request.
void doOptions(HttpServletRequest req, HttpServletResponse res) This method allows a Servlet to handle the options request.





Creating a webpage which calls the Servlet


We are creating a webpage which calls the Servlet when user submits a form with a Get request.
<html>

<head>
<title> HttpServlet example </title>
</head>


<body>
Please enter your name in the box :

<form action = "MyServlet" method = "Get" >
<input type = "text"   name = "username" />
<input type = "submit" value = "submit" />
</form>

</body>
</html>





Creating the Servlet class by extending HttpServlet


We are creating a Servlet extending the HttpServlet abstract class. HttpServlet class extends GenericServlet abstract class. This class implements doGet() method of HttpServlet class to handle get request of the client.
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 extends HttpServlet
{

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
{
PrintWriter out = response.getWriter();
out.println("Hello from the Servlet made by extending HttpServlet abstract class.");
System.out.println("Putting the Servlet in service");
out.println("Welcome " + request.getParameter("name"));
}

}

}






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-






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>HttpServletEx</servlet-name>
	<servlet-class>MyServlet1</servlet-class>
</servlet>

<servlet-mapping>
	<servlet-name>HttpServletEx</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> :






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 -




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 MyServlet1.java





Executing the Servlet


First we execute the webpage containing the form, asking the user to enter the name and press submit button.



As soon as the user enters the name and submits the form, the request goes to the Servlet file (stored in the folder created by us named HTTPServ within webapps folder).

Hence, you will see the URL containing the full path to the file with the request parameter -




This Servlet displays a personalised welcome message to the user, by accessing the value of parameter username through request object.




Why to use HttpServlet to create a Servlet?







Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< Servlet Context Attributes
Request Dispatching >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2