Head Method with Java Servlet
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Raj_Aryan
Posted On:   Sunday, February 6, 2005 02:20 AM

Hello I have just started with java servlets and so this question may be stupid. Head method should return "HEAD" when we use req.getMethod() in the servlet. But I tried lot of time, it returns GET always. Is there something wrong I am doing ? I want servlet to return after setting content type to "text/html" if it is Head Request. Here is my Servlet Code import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class temp extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {    More>>

Hello


I have just started with java servlets and so this question may be stupid.

Head method should return "HEAD" when we use req.getMethod() in the servlet. But I tried lot of time, it returns GET always. Is there something wrong I am doing ? I want servlet to return after setting content type to "text/html" if it is Head Request.

Here is my Servlet Code









import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;


public class temp extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {


// Set the Content-Type header

PrintWriter out = res.getWriter();

res.setContentType("text/html");


// Return early if this is a HEAD

if (req.getMethod().equals("HEAD"))

{

out.println(req.getMethod());

}

else

{

// Proceed otherwise


out.println("NotHead");

}
}
}











My HTML File is as usual with Method="HEAD" and Action="Servlet"

Thanks

Raj

   <<Less

Re: Head Method with Java Servlet

Posted By:   Anonymous  
Posted On:   Sunday, February 6, 2005 10:59 PM

Overriding doget() method to support a GET request also automatically supports an HTTP HEAD request. A HEAD request is a GET request that returns no body in the response, only the request header fields.
doHead method executes the doGet method but returns headers only.



for more detials:


http://java.sun.com/products/servlet/2.2/javadoc/


http://livedocs.macromedia.com/jrun/4/Programmers_Guide/techniques_servlet4.htm
About | Sitemap | Contact