0

I have question about doGet doPost priorities (if there are any). Here is my HelloServlet class:

public class HelloServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    doPost(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.getWriter().println("Hello, World Post!");
}


@Override
public void init() throws ServletException {
    System.out.println("Servlet " + this.getServletName() + " has started.");
}

@Override
public void destroy() {
    System.out.println("Servlet " + this.getServletName() + " has stopped.");
}

This class is mapped to the /greeting URL. When I try to access this page now, everything is fine. But when I change the doPost and doGet methods I gives me an error: HTTP Status 405 - HTTP method GET is not supported by this URL. Everytime I read about doGet and doPost I assume these methods are interchangeable. So what is the problem with this version of these methods?

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.getWriter().println("Hello, World Post!");
}

This of course caused no exception because doGet is present, but it will not do any work. When I remove doGet method it throws the exception.

Can you please tell me what exactly happens in the moment I use my code URL? http://localhost:8080/greeting

Why the client just cannot use the doPost method to obtain the data from the server when doGet is completely missing?

Thank you!

UPDATE WEB.xml file

<display-name>Hello World Application</display-name>

<servlet>
    <servlet-name>helloServlet</servlet-name>
    <servlet-class>com.wrox.HelloServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>helloServlet</servlet-name>
    <url-pattern>/greeting</url-pattern>
</servlet-mapping>
vviston
  • 145
  • 1
  • 10
  • https://stackoverflow.com/questions/12105584/http-status-405-http-method-get-is-not-supported-by-this-url Check this question . Are your entries in web.xml correct ? – Aarish Ramesh Dec 04 '18 at 13:14
  • I suppose they are because everything works fine with the doGet method. My question is more likely about principle how doGet and doPost are called when the client initiate some request. – vviston Dec 04 '18 at 13:20
  • Read the javadoc. It's all explained there. doGet() is called when you send an HTTP GET request to that URL; doPost() is called when you send an HTTP POST request. They're not interchangeable. – JB Nizet Dec 04 '18 at 13:21
  • Somewhere deep in my brain I know that there is a bigger difference between these two methods but I still can't see it. Everything I see now is that I am able to do the same things with these methods - I know about different parameters passing and all the basic stuff but still I don't know why my code doesn't work with doPost method only when I am able to call doPost from doGet, all the logic will by in the doPost and everything will be allright. So it means that doPost did all the work. – vviston Dec 04 '18 at 13:31
  • Or maybe someone could give me example where only the doPost method will be implemented and do some work without doGet? – vviston Dec 04 '18 at 14:07
  • You click a link whose href is /greeting. That will send a GET request to your servlet. Implementing doPost() to handle such a link is useless, and wrong, since you expect a GET request. You have a form to submut information and the method of the form is POST. That will send a POST request. Implementing doGet() is useless, and wrong in that case, since you expect a POST request. You should never have the same code for doGet and doPost, since those two methods have completely different semantics. GET is used to get info, and is idempotent. POST is used to send info, and is not idempotent. – JB Nizet Dec 04 '18 at 15:40
  • You're dealing with the HTTP protocolhere, so you need to understand its basic methods and principles. – JB Nizet Dec 04 '18 at 15:41

2 Answers2

1

If you do not specify the request method by default it will be GET which means doGet() will be called. example: http://www.anywebsite.com is a default GET request.

But you have to specify your request is a POST request to execute doPost() example:

<form action="/servlet" method="POST"> <input type="text" name="something" </form>

ErShakirAnsari
  • 617
  • 6
  • 17
  • Thank you it's my bad but I wasn't sure if the GET is default method to call. I did a little research since I posted my last question here and this is answer I expected. I usually get stuck with simple or obvious stuff...:) – vviston Dec 06 '18 at 23:29
1

if you have not mentioned in then default it call doGet method But if you have to specify your request is a POST in below code as like then tomcat call the service method where service method decide where request should go

<form action="/servlet" method="POST">
 <input type="text" name="something"
</form>

The protected service method checks the type of request, if request type is get, it calls doGet method, if request type is post, it calls doPost method, so on. Let's see the internal code:

protected void service(HttpServletRequest req, HttpServletResponse resp)  
        throws ServletException, IOException  
    {  
        String method = req.getMethod();  
        if(method.equals("GET"))  
        {  
            long lastModified = getLastModified(req);  
            if(lastModified == -1L)  
            {  
                doGet(req, resp);  
            }   
    //rest of the code  
        }  
    }