File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes if (req.getMethod().equals( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "if (req.getMethod().equals("HEAD")) return" Watch "if (req.getMethod().equals("HEAD")) return" New topic
Author

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

josephine chen
Ranch Hand

Joined: Oct 29, 2002
Posts: 216

<pre>
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Hello extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException {
// Set the Content-Type header
res.setContentType("text/html");
// Return early if this is a HEAD
if (req.getMethod().equals("HEAD")) return;
// Proceed otherwise
PrintWriter out = res.getWriter();
String name = req.getParameter("name");
out.println("<HTML>");
out.println("<HEAD><TITLE>Hello, " + name + "</TITLE></HEAD>");
out.println("<BODY>");
out.println("Hello, " + name);
out.println("</BODY></HTML>");
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doGet(req, res);
}
}
</pre>
what does this st do??
if (req.getMethod().equals("HEAD")) return
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
If the request is HEAD, then there is no sense in sending data that will not be read in the 1st place. This is from O'Reilly's book on Servlets, right?


Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Perhaps a little more background might be useful.
HTTP (the protocol used for the web) consists of several named types of request, defined in RFC1945.
The most common by far is "GET". This is used every time you click an anchor or type in a URL; it typically just retrieves the contents of a static web page, but can call servlets, CGI scripts etc. The next most common is "POST"; This is typically used when forms are submitted.
Both GET and POST are so common that they have special methods in the servlet API. There are a also a handful of other request types, and HEAD is one of them. HEAD is sent by some clients to request just the headers from a page. This can be useful if the client caches pages, and needs to know if the modification date has changed, for example.
By default, the Servlet API passes HEAD requests to the doGet() method. A client won't complain if it also gets the whole page as well as the headers, but it can be inefficient, especially if the page is automatically generated. So it can be a neat trick in your servlet code to return early from HEAD requests.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
josephine chen
Ranch Hand

Joined: Oct 29, 2002
Posts: 216
I am new to servlet and HTML and I am trying to learn them on my own . So my Ques may sound silly!!!
What is header info for a particular page? Can u give me a small
example.
Thanx

Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
Shan: It has been said that "An Applet Is Worth 1K Words" :-)
To get a feel of what a HEAD request's response looks like, feel free to play around with my applet that does just that. I have documentation on it also, all public:
http://www.geocities.com/~tony-alicea/HttpHeaderApplet.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: if (req.getMethod().equals("HEAD")) return
 
Similar Threads
Progress bar on IE6
How to use body background in a HTML page thru servlet
what is the output of the code and why is it so????
Problems with getRemoteUser()
HTTP method GET is not supported by this URL