aspose file tools*
The moose likes Servlets and the fly likes Servlet method execution sequence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet method execution sequence" Watch "Servlet method execution sequence" New topic
Author

Servlet method execution sequence

Steven Marco
Greenhorn

Joined: Jul 23, 2002
Posts: 21
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

Joined: Jan 10, 2002
Posts: 61233
    
  66

A few logging statements will help you track the execution.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jignesh Patel
Ranch Hand

Joined: Nov 03, 2001
Posts: 626

It seems your assumption is techically correct, but why don't you follow Bear.
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
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

Joined: Nov 03, 2001
Posts: 626

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

Joined: Sep 26, 2006
Posts: 148
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

Joined: Dec 11, 2004
Posts: 13410

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?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
sven studde
Ranch Hand

Joined: Sep 26, 2006
Posts: 148
The first issue is that ServletTwo is abstract so will not run at all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet method execution sequence