Page 1 of 1

About Question enthuware.jwpv6.2.1156 :

Posted: Tue Sep 06, 2011 1:42 pm
by ETS User
The only right answer is : "The default implementation of HttpServlet class's doHead() method calls the doGet() method.". But then, why the answer "If the servlet does not implement doHead(), doGet() will be called upon HTTP HEAD request." is not right?

Re: About Question enthuware.jwpv6.2.1156 :

Posted: Fri Sep 09, 2011 4:10 am
by admin
You are right. It should be marked correct as well. This has now been fixed.

Thanks for your feedback!

Re: About Question enthuware.jwpv6.2.1156 :

Posted: Sun May 22, 2016 9:57 pm
by himaiMinh
I tried to override the doHead() method and I do not implement the doGet() method , the container is looking for a doGet () method.
The output is :
message HTTP method GET is not supported by this URL
description The specified HTTP method is not allowed for the requested resource.
It seems to me that the doGet () method must exist in order to make doHead() work.

Re: About Question enthuware.jwpv6.2.1156 :

Posted: Sun May 22, 2016 10:04 pm
by himaiMinh
This is my demo of doHead:

Code: Select all

@WebServlet(name = "MyServlet", urlPatterns = {"/MyServlet"})
public class MyServlet extends HttpServlet {
   
   @Override
   public void doHead(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    
    resp.setContentLength(1024);
    resp.setContentType("text/plain");
    resp.setHeader("category", "magazine");
}
   @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
          PrintWriter writer = response.getWriter();
          writer.println("category header  : "+ response.getHeader("category"));
          writer.println("content type: "+ response.getContentType());
          
    }
   
}

Code: Select all

<html>
    
    <body>
        <body>
        <form method="HEAD" action="MyServlet">
             <input type="submit" value="do head">
         </form>   
    </body>
    </body>
</html>

But why my output on the screen is:
category : null
content-type:null ?

Why does it only call the doGet, but not the doHead.

Re: About Question enthuware.jwpv6.2.1156 :

Posted: Sun May 22, 2016 10:52 pm
by admin
You need to check the exact request that the browser has sent to the server. IFAIK, HEAD is not a valid method for form. So even though you have used HEAD, the browser might be sending something else.

Executing doGet for a HEAD request doesn't make sense because the whole purpose of HEAD is to avoid the computation expense of doGet. I suspect there is something else going on in your test which is causing doGet to be invoked.

-Paul.

Re: About Question enthuware.jwpv6.2.1156 :

Posted: Tue May 24, 2016 10:55 pm
by himaiMinh
Hi, Paul. I download curl and my command is :
curl -I http://localhost:8080/doHeadEx/MyServlet .
Then, it calls the doHead method.

I think the browser only works for GET and POST, but not the rest commands.

Re: About Question enthuware.jwpv6.2.1156 :

Posted: Tue Aug 16, 2016 12:38 pm
by johnlong
The HttpServlet class does not implement a dummy doHead() method.

Where do you get this information? How can we see this implementation?

Re: About Question enthuware.jwpv6.2.1156 :

Posted: Tue Aug 16, 2016 9:07 pm
by admin

Re: About Question enthuware.jwpv6.2.1156 :

Posted: Wed Aug 17, 2016 1:55 pm
by johnlong
Thank you.