Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
josephine chen
Ranch Hand
Posts: 216
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

<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
Posts: 3226
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Frank Carver
Sheriff
Posts: 6920
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
josephine chen
Ranch Hand
Posts: 216
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3226
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic