Thursday 17 July 2014

doHead (HttpServletRequest req, HttpServletResponse resp)

protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. Receives an HTTP HEAD request from the protected service method and handles the request.

The client sends a HEAD request when it wants to see only the headers of a response, such as document size, modification-time, Content-Type or Content-Length. The HTTP HEAD method counts the output bytes in the response to set the Content-Length header accurately. DoGet method automatically supports HEAD request also.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   
     <title>Filter Example</title>
    </head>
    <body>
        <form method="head" action="/servlet/HelloWorld">
            <input type="submit" value ="Get Header">
        </form>
    </body>
</html>

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
       
@WebServlet(urlPatterns="/HelloWorld")
public class HelloWorld extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
       try(PrintWriter out = res.getWriter()) {
            res.setContentType("text/html");
            System.out.println("I am here");
            String str = "<html>" +
                         "<head>" +
                         "<title> Hello World </title>" +
                         "</head>" +
                         "<body><h1> Hello World" +
                         "</h1> </body>" +
                         "</html>";
            out.println(str);
       }
   } 
  
}

@WebServlet Annotation used to declare a servlet. This annotation is processed by the container at deployment time, and the corresponding servlet made available at the specified URL patterns.



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment