*
The moose likes Servlets and the fly likes servlet constructor? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet constructor?" Watch "servlet constructor?" New topic
Author

servlet constructor?

vishvar hariraman
Greenhorn

Joined: Apr 04, 2006
Posts: 20
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

Joined: Sep 20, 2003
Posts: 1704
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

Joined: Apr 04, 2006
Posts: 20
Thx Mr.Reddy
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

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".


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
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

Joined: Jun 25, 2001
Posts: 16158
    
  21

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
Jass Singh
Ranch Hand

Joined: Mar 30, 2006
Posts: 52
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet constructor?