my dog learned polymorphism*
The moose likes Servlets and the fly likes Servlet  init() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet  init()" Watch "Servlet  init()" New topic
Author

Servlet init()

vigneswar rao
Greenhorn

Joined: Feb 12, 2006
Posts: 14
Hi friends!


can we declare init() method in service()method of the servlet.
Is there is any chance to declare that type.....please ...
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

can we declare init() method in service()method of the servlet.
Is there is any chance to declare that type.....please ...


Sorry, but I don't see what you mean.

1. init()
2. service()
3. destroy()

are three life-cycle methods of servlet.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
You can�t define method within a method in Java.

Why do you want to declare inti() within service()?


My blood is tested +ve for Java.
Ravi Kotha
Ranch Hand

Joined: Mar 02, 2006
Posts: 53
Reply from Chetan....

"You can�t define method within a method in Java."

But we write method System.out.println() with in other methods in Java. Need some clarification about this....
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
Originally posted by ravi kotha:
But we write method System.out.println() with in other methods in Java. Need some clarification about this....


Writing System.out.println() inside a method doesn't mean that we are defining that method inside another method. We are just calling that method, which is defined somewhere else.





So, if your question is
a) Can we declare init() method inside service() method, the answer is NO. It is illegal, and the code won't even compile.
b) Can we call init() method from service() method, the answer is yes, you can.


Mani
Quaerendo Invenietis
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by ravi kotha:
Reply from Chetan....

"You can�t define method within a method in Java."

But we write method System.out.println() with in other methods in Java. Need some clarification about this....


We don't (rather can't) define a method in another method, we just call it.

If you mean, calling init() method inside service() method, then it's a valid question.

And my answer is, servlet is a java class, so you can do in servlet whatever you do in java class. So yes, you can call.



Ravi Kotha
Ranch Hand

Joined: Mar 02, 2006
Posts: 53
Thanks for ur clarification Mani Ram n Rathi ji
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
But its better to avoid calling life cycle methods by your own. Some times you may end up with mess up.
vigneswar rao
Greenhorn

Joined: Feb 12, 2006
Posts: 14
Thanks a lot my clarification
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet init()
 
Similar Threads
Init()
Servlet constructor
init missing in servlet
How to write a bootstrap code
How do you pre initialize servlets in a webcontainer?