• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

servlet constructor?

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx Mr.Reddy
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 28321
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
That's a very big dog. I think I want to go home now and hug this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic