Friday, April 10, 2020

Servlet HttpServlet class

HttpServlet class


The java.servlet.http.HttpServlet class extends extends javax.servlet.GenericServlet class. This class also implements the Servlet, Serializable and ServletConfig interfaces.

httpservlet


HttpServlet class is an abstract class, and we need to create a subclass of this class to create an HTTP servlet capable of handling HTTP requests and generate HTTP responses (processing the HTTP requests). The extended class of javax.servlet.http.HttpServlet must override at least one method (doXXX()) listed as,

  • doGet(HttpServletRequest request, HttpServletResponse response), for handling HTTP GET requests
  • doPost(HttpServletRequest request, HttpServletResponse response), for handling HTTP POST requests
  • doPut(HttpServletRequest request, HttpServletResponse response), for handling HTTP PUT requests
  • doDelete(HttpServletRequest request, HttpServletResponse response), for handling HTTP DELETE requests

The init() and destroy() method are used to manage resources that are held for the life of the servlet.

The getServletInfo() method is used to provide information about the servlet itself.

We do not need to override the service() method since this method manages standard HTTP requests. The service() method dispatches the HTTP requests to the handler methods for any of the doXXX() methods.

In the Servlet class hierarchy, HttpServlet is one of the most important and commonly used classes. The servlets can run on the multithreaded servers, so the Servlets must have the capability to address multiple concurrent requests. Access to the shared resources must be synchronized. These resources can be such as for instance variables, class variables, any external files, the database connections or the network connections.

Methods

protected void doGet(HttpServletRequest request, HttpServletResponse response)


This method is called by the server (invoked by service() method) to enable the servlet to handle a GET request.

protected void doPost(HttpServletRequest request, HttpServletResponse response)


This method is called by the server (invoked by service() method) to enable the servlet to handle a POST request.

protected void doDelete(HttpServletRequest request, HttpServletResponse response)


This method is called by the server (invoked by service() method) to enable the servlet to handle a DELETE request.

protected void doHead(HttpServletRequest request, HttpServletResponse response)


This method receives an HTTP HEAD request from the service() method and handles the request.

protected void doOptions(HttpServletRequest request, HttpServletResponse response)


This method is called by the server (invoked by the service() method) to enable the servlet to handle OPTIONS requests.

protected void doPut(HttpServletRequest request, HttpServletResponse response)


This method is called by the server (invoked by service() method) to enable the servlet to handle a PUT request.

protected void doTrace(HttpServletRequest request, HttpServletResponse response)


This method is called by the server (invoked by service() method) to allow a servlet to handle a TRACE request.

protected long getLastModified(HttpServletRequest request)


This method returns the time the HttpServletRequest object was last modified, in milliseconds since epoch midnight  January 1, 1970 GMT.

protected void service(HttpServletRequest request, HttpServletResponse response)


This method receives HTTP requests from the public service() method and dispatches to the doXXX() methods declared in the class.

void service(ServletRequest request, ServletResponse response)


This method dispatches client requests to the doXXX() methods.

Other methods are inherted from GenericServlet class are destroy(), getInitParameter(), getInitParameterNames(), getServletConfig(), getServletContext(), getServletInfo(), getServletName(), init(), init(), log().

HttpServlet doGet() method Example


Create a new dynamic web project, having index.html and MyServlet.java placed in the project as



index.html


<html>
<head>
<title>Welcome</title>
</head>
<body bgcolor="yellow">
<form method="get" action="MyServlet">
<input type="text" name="name" placeholder="Enter your name"><br><br>
<textarea rows="20" cols="30" name="message"></textarea>
<br><br>
<input type="submit" value="submit">
</form>
</body>
</html>

MyServlet.java


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {
     private static final long serialVersionUID = 1L;
    public MyServlet() {
        super();
    }

     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

           PrintWriter pout=response.getWriter();
           response.setContentType("text/html");
           String name=request.getParameter("name");
           String message=request.getParameter("message");
           pout.append("<html><body><h3>"+message+"</h3><h2>from "+name+"</h2></body></html>");//embed HTML within Java
     }

}


Result:





Reference:https://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpServlet.html