About Question enthuware.jwpv6.2.1156 :

Moderator: admin

Post Reply
ETS User

About Question enthuware.jwpv6.2.1156 :

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1156 :

Post by admin »

You are right. It should be marked correct as well. This has now been fixed.

Thanks for your feedback!
If you like our products and services, please help us by posting your review here.

himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1156 :

Post 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.

himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1156 :

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1156 :

Post 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.
If you like our products and services, please help us by posting your review here.

himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1156 :

Post 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.

johnlong
Posts: 197
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

Re: About Question enthuware.jwpv6.2.1156 :

Post by johnlong »

The HttpServlet class does not implement a dummy doHead() method.

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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.jwpv6.2.1156 :

Post by admin »

If you like our products and services, please help us by posting your review here.

johnlong
Posts: 197
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

Re: About Question enthuware.jwpv6.2.1156 :

Post by johnlong »

Thank you.

Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests