Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling doPost() internally without doGet()  RSS feed

 
nancy andrew
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have couple of questions in servlets:

Q1) One request goes to doGet() method of a servlet. Is is possible to call goPost() internally without calling doPost() from doGet()?

Q2) I have seen this lot of times that doPost() is called within goGet(). Why cant we directly call goPost() on submit or let getGet() process the request?
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
#1: From where would you be calling doPost if not from doGet? It needs request and response objects as parameters.

#2: Both GET and POST (and consequently doGet and doPost) can be used to process requests. What makes you think otherwise? Note that GET and POST are meant for different purposes, though, and any web app that uses them interchangeably is technically violating the HTTP specification (and may run into practical difficulties).
 
Virendrasinh Gohil
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nancy andrew wrote:Q1) One request goes to doGet() method of a servlet. Is is possible to call goPost() internally without calling doPost() from doGet()?

You can override service() method to send GET request to doPost() if you want. (Just to provide hack). Nobody does that as there is no need to override service() method but just for hack.

Q2) I have seen this lot of times that doPost() is called within goGet(). Why cant we directly call goPost() on submit or let getGet() process the request?

This is typically done because the implementor want to have exact same implementation in that servlet for GET and POST. so, instead of repeating the code in both the methods, implementor may choose to call doPost() from doGet() or vice versa. I did not find any documentation categorizing this as a bad practice or something and hence people take this short-cut.
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Virendrasinh Gohil wrote:I did not find any documentation categorizing this as a bad practice or something and hence people take this short-cut.

It is quite bad practice since it interferes with the proper functioning of HTTP, REST, caches, proxies, browsers and other pieces of web infrastructure. Searching for "double submit problem" and "post redirect get" will find a lot of material on this.

The HTTP specification also makes it clear that GET is idempotent, while POST is not idempotent, thus they should not be used interchangeably.
 
Virendrasinh Gohil
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lester Burnham wrote:It is quite bad practice since it..........

I agree. Thanks for reminding.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!