File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to call a doHead method inside a servlet

 
Malli Raman
Ranch Hand
Posts: 312
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am just trying to call a doHead() method inside the servlet. But in
the servlet it is calling only doGet method and it is not calling
doHead method. But in the Orielly book it has mentioned the service
method will call doHead method and followed by the doGet method and
returns only the header details and suppress all the body output. But it
is not happening. Whether I have missed something.
<------------------------DoHead.jsp-------------------------------------->
<HTML>
<HEAD>
<TITLE> Testing the doHead method </TITLE>
</HEAD>
<BODY>
<FORM METHOD = "HEAD" ACTION ="SimpleHeadServlet">
<INPUT TYPE = "SUBMIT" NAME= "SUBMIT" VALUE= "Click to call Head
method">
</FORM>
</BODY>
</HTML>
<------------------------- SimpleHeadServlet ----------------------------->
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SimpleHeadServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse
response)
throws ServletException,IOException
{
response.setContentType("Text/html");
PrintWriter out = response.getWriter();
out.println("Method Name: " + request.getMethod());
if (request.getMethod().equals("HEAD"))
{
out.println("Method Name is doHead");
return;
}
out.println("Inside the doGet method");
}
public void doHead()
{
System.out.println("Inside the doHead method");
}
}

Thanks & Regards,
M.S.Raman
 
Gayan Balasooriya
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no doHead() method defined in HttpServlet class in 2.2 API.
But it is there in 2.3 API.
And the correct method is
doHead(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,java.io.IOException
not doHead().
Gayan Balasooriya
SCJP,SCWCD
http://www.gayanb.com - Free Java/J2EE/J2ME books and mock exams
 
Malli Raman
Ranch Hand
Posts: 312
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gayan Balasooriya:
There is no doHead() method defined in HttpServlet class in 2.2 API.
But it is there in 2.3 API.
And the correct method is
doHead(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,java.io.IOException
not doHead().
Gayan Balasooriya
SCJP,SCWCD
http://www.gayanb.com - Free Java/J2EE/J2ME books and mock exams

Hi Gayan Balasooriya,
Thanks for replying me. I have changed the doHead() method to
public void doHead(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException
{
System.out.println("Inside the doHead method");
}
But still i am getting this resposne only "Method Name: GET Inside the doGet method ". I am using weblogic 7.1 application server which supports Servlet 2.3.
Regards,
M.S.Raman
 
Adnan Ahmed
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jojit
I think this is not right way to invoke doHead method of servlet. Because to invoke doGet u r using form tag of html and the attribute "method" of form just support two HTTP method either get or post.
And the reason the page invoking doget is that the form using invalid method thats why form default method will be called which is Get.
If i m right then tell me
okie
 
Adnan Ahmed
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jojit
I think this is not right way to invoke doHead method of servlet. Because to invoke doGet u r using form tag of html and the attribute "method" of form just support two HTTP method either get or post.
And the reason the page invoking doget is that the form using invalid method thats why form default method will be called which is Get.
If i m right then tell me
okie
 
Adnan Ahmed
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jojit
I think this is not right way to invoke doHead method of servlet. Because to invoke doGet u r using form tag of html and the attribute "method" of form just support two HTTP method either get or post.
And the reason the page invoking doget is that the form using invalid method thats why form default method will be called which is Get.
If i m right then tell me
okie
 
James Swan
Ranch Hand
Posts: 403
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sounds like telnet might be your friend here:
>telnet myserver.com myport
HEAD /SimpleHeadServlet HTTP/1.0
 
Gayan Balasooriya
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Adnan Ahmed:
Hi Jojit
I think this is not right way to invoke doHead method of servlet. Because to invoke doGet u r using form tag of html and the attribute "method" of form just support two HTTP method either get or post.
And the reason the page invoking doget is that the form using invalid method thats why form default method will be called which is Get.
If i m right then tell me
okie

I think you are correct.According to HTML specification METHOD attribute of
FORM tag can be only GET or POST.
Read this

Gayan Balasooriya
SCJP,SCWCD
http://www.gayanb.com - Free Java/J2EE/J2ME books and mock exams
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic