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

From doPost() to doGet()?  RSS feed

 
Mike Gerd
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Im a newbie to J2EE. I was experimenting with doGet and doPost. and to my surprise I discovered that doPost was delegating? to doGet. I tried searching on the net for this but didnt find any such thing. so what going on here?

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doPost(req, res);
PrintWriter out = res.getWriter();
out.println("This is doGet");


This prints "This is doGet" on to the webpage. But the strange thing is that Im calling doPost first!! The doPost is empty!. Is the doPost turning around and calling doGet?
[ July 14, 2007: Message edited by: Mikey Gerard ]
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
doPost(req, res);
PrintWriter out = res.getWriter();
out.println("This is doGet");
}

This prints "This is doGet" on to the webpage. But the strange thing is that Im calling doPost first!! The doPost is empty!. Is the doPost turning around and calling doGet?



Mike, even servlets are Java classes and they follow the same flow that other Java classes follow. In this case consider doGet as just another method calling doPost. The control is first going to come to doGet which invokes the doPost. The doPost in your case is empty and does nothing and the control *returns back* in the doGet method. The next statements in your doGet method are then excetued. In this case, the doGet method is then getting hold of the PrintWriter and writing out a message.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66205
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you not expect the code after the method call to execute?
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets are also java classes , so once the doPost is popped off the stack , the remaining lines of code from doGet would be called ,

that is ,

PrintWriter out = res.getWriter();
out.println("This is doGet");
 
praveen sharma
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you must be aware that from within a function when you call another function stack will be maintained.so curently your stack hold get method but from there you are calling post() .so now on top of stack post() method is about to execute once it will finish executing post() method control will be transfered to get() again and rest of the code will be executing after words.....


Bye
Praveen
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by praveen sharma:
As you must be aware that from within a function when you call another function stack will be maintained.so curently your stack hold get method but from there you are calling post() .so now on top of stack post() method is about to execute once it will finish executing post() method control will be transfered to get() again and rest of the code will be executing after words.....


Bye
Praveen

Isn't that what Raul said an hour earlier?
Isn't that also what Bear alluded to two days ago?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!