Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet constructor?

 
vishvar hariraman
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everybody

This s Vishnuvardhan
I m new to Javaranch

I ve a doubt on servlet.

Can i have a constructor for a servlet?
Can i create any object for servlet class?
if can have then wer i shud write the constructor?within init()?


pls clarify to me

Thx
Vishnuvardhan.H


 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JR.

As per Servlet's design and life cycle of servlets constructors are not advisable. The servlet container uses default constructor, so if we are writing constructors we need to provide default constructor.

You can create a object from you servlet but it behaves like a ordinary class so it wont helpful.

For more details you can refer Servlets FAQ
[ April 04, 2006: Message edited by: KJ Reddy ]
 
vishvar hariraman
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx Mr.Reddy
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64692
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by KJ Reddy:

As per Servlet's design you can not have constructor, if you provide constructor it becomes another Java class and not servlet.


That is not correct. A servlet can have a constructor just like any other class. Constructors in servlets are pretty useless so you don't see them very often, the servlet life-cycle init() method being the proper place to perform setup code.

But adding a constructor does not make the servlet "not a servlet".
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:


That is not correct. A servlet can have a constructor just like any other class. Constructors in servlets are pretty useless so you don't see them very often, the servlet life-cycle init() method being the proper place to perform setup code.

But adding a constructor does not make the servlet "not a servlet".


Hi Bear thanks for correcting me. I just edited my previous message to correct my statements.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18094
48
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that in the early days, you didn't have a guarantee that the same instance of a servlet would be used every time, as it tried to take advantage of multithreading and pooling. Some of that has since changed.

There are some very valid things you can do in a servlet constructor, but do them carefully. You are dealing with a sharable, multhreaded object, and in a clustered environment, there may be cases where the user's next request wouldn't come back to the same servlet in the same VM.
 
Jass Singh
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can i have a constructor for a servlet?


Has already been answered. For more clarity: Every java class has a default conctructor. So Servlet also has one. Like other java classes you can also define other constructors for Servlet (remember to provide default one also), but that will be of no use since servlet runs in a servlet container and conatiner uses only default constructor to create servlet instance

Can i create any object for servlet class?


You can, but that will be of little use as Servlet's real capabilties are used only when it is run by container.


if can have then wer i shud write the constructor?within init()?


You can not define constructor of any class in any of its method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic