HTTP Servlet in Java

HTTP Servlet in Java

In this article, I am going to discuss HTTP Servlet in Java with an example. Please read our previous article where we discussed Generic Servlet in Java with an example. At the end of this article, you will understand the following pointers.

  1. What is HttpServlet?
  2. Methods of HttpServlet class
  3. Example to demonstrate HttpServlet
  4. Different approaches for using the get and post method
What is HttpServlet?

The HttpServlet class extends the GenericServlet class and implements the Serializable interface. It provides HTTP specific methods like doGet, doPost, doHead, doTrace, etc. Provides an abstract class to be subclassed to make an HTTP servlet suitable for an internet site. A subclass of HttpServlet must override a minimum of one method, usually one among these:

  1. doGet, when the servlet supports HTTP GET requests
  2. doPost, used for HTTP POST requests
  3. doPut, used for HTTP PUT requests
  4. doDelete, used for HTTP DELETE requests
  5. init and destroy, won’t manage resources that are held for the lifetime of the servlet
  6. getServletInfo, for the servlet, uses to provide information about itself

In Http Servlet it is not required to override the service() method because this method dispatches the Http Requests to the correct method handler.

Methods of Http Servlet class
  1. doDelete(HttpServletRequest req, HttpServletResponse resp) : It is called by the server (via the service method) to allow a servlet to handle a DELETE request.
  2. doGet(HttpServletRequest req, HttpServletResponse resp) : It is called by the server (via the service method) to allow a servlet to handle a GET request.
  3. doHead(HttpServletRequest req, HttpServletResponse resp) : It receives an HTTP HEAD request from the protected service method and handles the request.
  4. doOptions(HttpServletRequest req, HttpServletResponse resp) : It is called by the server to handle a OPTIONS request.
  5. doPost(HttpServletRequest req, HttpServletResponse resp) : It is called by the server(via the service method) to allow a servlet to handle a POST request.
  6. doPut(HttpServletRequest req, HttpServletResponse resp) : It is called by the server to allow a servlet to handle a PUT request.
  7. doTrace(HttpServletRequest req, HttpServletResponse resp) : It is called by the server to handle a TRACE request.
  8. getLastModified(HttpServletRequest req) : It returns the time the HttpServletRequest object was last modified, in milliseconds.
  9. service(HttpServletRequest req, HttpServletResponse resp) : It receives HTTP requests from the service method and dispatch them to the methods defined in the class.
  10. service(ServletRequest req, servletResponse res) : It dispatches client requests to the protected service method.
Example to demonstrate Http Servlet
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Http Servlet Demo</title>
</head>
<body>
<a href="welcome">Click to call Servlet</a>
</body>
</html>
ServletInterface.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletInterface extends HttpServlet
{
    private String mymsg;
    public void init () throws ServletException
    {
        mymsg = "Http Servlet Demo";
    }

    public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        // Setting up the content type of web page
        response.setContentType ("text/html");
        
        // Writing the message on the web page
        PrintWriter out = response.getWriter ();
        out.println ("<h1>" + mymsg + "</h1>");
        out.println ("<p>" + "Hello Friends!" + "</p>");
    }

    public void destroy ()
    {
        // Leaving empty. Use this if you want to perform
        // something at the end of Servlet life cycle.
    }
}
web.xml
<web-app>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>MyHttpServlet</servlet-name>
<servlet-class>ServletInterface</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>MyHttpServlet</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>

</web-app>

 

Output

Http Servlet in Java

Click the link on the above screen

Http Servlet in Java

Explain different approaches for using the get and post method.

Approach1:

By keeping the service method in our servlet program

public class MyServlet extends Httpservlet
{
    public void service (HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
    {
        //request procrssing logic
    }
}

The service(-,-) method of our servlet program can process both get and post methods based request. But keeping request processing logic in service(-,-) method is not industry-standard so try to keep request processing logic in doXXX methods.

Approach2:

By overriding both doGet() and doPost() methods but keep request processing logic in one method and call that method from another method.

public class MyServlet extends Httpservlet
{
    public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        //request procrssing logic
    }
    
    public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        doGet (request, response);
    }
}

Here servlet program can process both Get, Post method-based requests. Here request processing logic is not duplicated.

Approach3:

keep request processing logic in a user-defined method and call that method from both doGet() and doPost() method.

public class MyServlet extends Httpservlet
{
    public void xyz (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        //request procrssing logic
    }
    public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        xyz(request, response);
    }
    public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        xyz(request, response);
    }
}

Programmers generally prefer approach 2 or approach 3 to make their servlet programs as flexible servlet programs against the HTTP request methods get and post.

In the next article, I am going to discuss the Life Cycle of Servlet in Java. Here, in this article, I try to explain Http Servlet in Java. I hope you enjoy this HTTP Servlet in Java article.

Leave a Reply

Your email address will not be published.