• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Calling doPost() internally without doGet()

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lester Burnham wrote:It is quite bad practice since it..........


I agree. Thanks for reminding.
 
Is this the real life? Is this just fantasy? Is this a tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic