About Question enthuware.jwpv6.2.1156 :
Moderator: admin
About Question enthuware.jwpv6.2.1156 :
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?
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.jwpv6.2.1156 :
You are right. It should be marked correct as well. This has now been fixed.
Thanks for your feedback!
Thanks for your feedback!
If you like our products and services, please help us by posting your review here.
-
- Posts: 358
- Joined: Fri Nov 29, 2013 8:26 pm
- Contact:
Re: About Question enthuware.jwpv6.2.1156 :
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 :
The output is :
It seems to me that the doGet () method must exist in order to make doHead() work.message HTTP method GET is not supported by this URL
description The specified HTTP method is not allowed for the requested resource.
-
- Posts: 358
- Joined: Fri Nov 29, 2013 8:26 pm
- Contact:
Re: About Question enthuware.jwpv6.2.1156 :
This is my demo of doHead:
But why my output on the screen is:
category : null
content-type:null ?
Why does it only call the doGet, but not the 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>
category : null
content-type:null ?
Why does it only call the doGet, but not the doHead.
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.jwpv6.2.1156 :
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.
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.
-
- Posts: 358
- Joined: Fri Nov 29, 2013 8:26 pm
- Contact:
Re: About Question enthuware.jwpv6.2.1156 :
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.
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.
-
- Posts: 197
- Joined: Mon Jun 20, 2016 5:06 pm
- Contact:
Re: About Question enthuware.jwpv6.2.1156 :
The HttpServlet class does not implement a dummy doHead() method.
Where do you get this information? How can we see this implementation?
Where do you get this information? How can we see this implementation?
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.jwpv6.2.1156 :
If you like our products and services, please help us by posting your review here.
-
- Posts: 197
- Joined: Mon Jun 20, 2016 5:06 pm
- Contact:
Re: About Question enthuware.jwpv6.2.1156 :
Thank you.
Who is online
Users browsing this forum: No registered users and 6 guests