HttpServlet Example

HttpServlet Class

Httpservlet is an abstract class exist on javax.servlet.http package and extends GenericServlet class.

When the servlet container uses HTTP protocol to send request, then it creates HttpServletRequest and HttpServletResponse objects.

In HttpServlet class you don't need to override service(-,-) method.

Httpservlet class provide following methods:

  • protected void doGet(HttpServletRequest req,HttpServletResponse resp);
  • This method is called by servlet service method to handle the HTTP GET request from client. This method is used for fetching data because this is Idempotent method.

  • protected void doPost(HttpServletRequest req,HttpServletResponse resp);
  • This method is called by servlet service method to handle the HTTP POST request from client. This method is used to insert data because this method is non-idempotent.

  • protected void doDelete(HttpServletRequest req,HttpServletResponse resp);
  • This method is called by servlet service method to handle the HTTP DELETE request from client. This method is to for delete a resource.

  • protected void doPut(HttpServletRequest req,HttpServletResponse resp);
  • This method is called by servlet service method to handle the HTTP PUT request from client. This method is used to update a resource.

  • protected void doOptions(HttpServletRequest req,HttpServletResponse resp);
  • This method is called by servlet service method to handle the HTTP OPTIONS request from client. This method is used to determines which HTTP methods the server supports and returns an appropriate header.

  • protected void doTrace(HttpServletRequest req,HttpServletResponse resp);
  • This method is called by servlet service method to handle the HTTP TRACE request from client. This method is used to trace a resource (debugging).

  • protected void doHead(HttpServletRequest req,HttpServletResponse resp);
  • This method is called by servlet service method to handle the HTTP HEAD request from client. This method is used to see only the headers of a response, such as Content-Type or Content-Length etc.

Note : In Web Application Mostly you used doGet() and doPost() other methods you can use in WebService.

Hello World Example using HttpServlet class:

package com.deepsingh44.blogspot.cont;
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 First extends HttpServlet {

 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
 throws ServletException, IOException {
 
  doPost(req, resp);
 
 }

 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
 throws ServletException, IOException {
 
 resp.setContentType("text/html");
 
 PrintWriter pw = resp.getWriter();
 
 pw.print("<h1>Hello Http Servlet Example</h1>");
 
 }

}

Mapping HttpServlet in to web.xml file to create url for accessing from the browser.

        <web-app>

          <servlet>
          <servlet-name>first</servlet-name>
          <servlet-class>com.deepsingh44.blogspot.cont.First</servlet-class>
          </servlet>
        
          <servlet-mapping>
          <servlet-name>first</servlet-name>
          <url-pattern>/first</url-pattern>
          </servlet-mapping>
        
        </web-app>
    

Then run this code and get result as below:

No comments: