• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet method execution sequence

 
Steven Marco
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have the following two classes:




Now when I have a POST request coming in from a JSP form where "action" is ServletTwo, is the following sequence of method calls correct?

1. init() of ServletTwo is called which in turns calls init() in ServletOne which in turns calls init() in HttpServlet
2. service() of ServletTwo is called which in turns calls service() in ServletOne which in turns calls service() in HttpServlet
3. doPost() of ServletOne is called.
4. methodOne() is called in ServletTwo ???
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65119
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few logging statements will help you track the execution.
 
Jignesh Patel
Ranch Hand
Posts: 626
Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems your assumption is techically correct, but why don't you follow Bear.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now when I have a POST request coming in from a JSP form where "action" is ServletTwo, is the following sequence of method calls correct?

No.
[ October 13, 2006: Message edited by: sven studde ]
 
Jignesh Patel
Ranch Hand
Posts: 626
Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said your assumption is correct as per j2ee specification but almost all server has a different way to handle the request.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jignesh Patel:
As I said your assumption is correct as per j2ee specification...

Incorrect. This is wrong per specification of Java 1.5:

init() of ServletTwo is called

[ October 14, 2006: Message edited by: sven studde ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you follow Bear's advice; if for no other reason that doing so would point out any other issues that your code may have.

The first issue is that ServletTwo is abstract so will not run at all.
You can not instanciate abstract classes.

If you remove the abstract keyword and recompile, then yes, number one is correct.

Number two is also correct (but you do realize that there is no need to override the service method, correct?).

Number three is correct, methodOne will be called.
It will then be called again, and again, until your app blows up with a StackOverflowError.


Sven,
"No" and "Incorrect. This is wrong per specification of Java 1.5" aren't really helpful answers. If there is something that we're missing, maybe you could point out what it is?
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first issue is that ServletTwo is abstract so will not run at all.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic