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

Final value in a Servlet

Kevin Nielsen
Greenhorn

Joined: Jul 27, 2005
Posts: 8
Hi guys,

In a normal Java class it states that the every final variables should be assigned values when the constructor ends. How does this compares in a Servlet?

Regards,
Kevin
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Servlets are normal Java classes.


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

Joined: Jan 05, 2007
Posts: 100

Originally posted by Ben Souther
Servlets are normal Java classes.




Witty Reply.


RAGHU<br /> <br />"When the going gets tough, the tough get going"
Kevin Nielsen
Greenhorn

Joined: Jul 27, 2005
Posts: 8
Hi,

Is the init() method for a servlet what a constructor is for normal Java class?

Regards,
Kevin
Rao Raghu
Ranch Hand

Joined: Jan 05, 2007
Posts: 100
You got it Kevin! Its the init() method.
There are two forms of init: init() and init(ServletConfig )
Kevin Nielsen
Greenhorn

Joined: Jul 27, 2005
Posts: 8
Hi again,

Do the two methods differ besides for the argument?

I have code like this, which the compiler says is not legal:



Why is this?

Regards,
Kevin
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

You need to initialize the final variable either at the point of declaration or in the constrcutor,else the compiler will complain.


Groovy
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Originally posted by Kevin Nielsen:
Hi,

Is the init() method for a servlet what a constructor is for normal Java class?

Regards,
Kevin


The init method will be invoked after the constrcutor gets called.
Kevin Nielsen
Greenhorn

Joined: Jul 27, 2005
Posts: 8
Hi,

Is this container specific behavior or is the implemented in a specification, like the Servlet spec?

Regards,
Kevin
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The init method is not the same as a constructor.
You can not use it to initialize final variables this way.

The init method is one of the servlet lifecycle methods called by the container. As far as the compiler is concerned, it's just another method.
Raghavendra Nittur
Greenhorn

Joined: Feb 19, 2007
Posts: 29
Originally posted by Ben Souther:
The init method is not the same as a constructor.
You can not use it to initialize final variables this way.




then how can the final variables be initialized other than initializing it during its declaration.

someone replyed as we cann't write consructor as well.

Regards, raghav.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Raghavendra Nittur:
then how can the final variables be initialized other than initializing it during its declaration.

someone replyed as we cann't write consructor as well.



It would have to either be initialized during its declaration or initialized in the no-arg contstructor.

The init method gets called by the container when the servlet is put into service, not when the servlet object is instanciated.
Raghavendra Nittur
Greenhorn

Joined: Feb 19, 2007
Posts: 29
Originally posted by Ben Souther:


It would have to either be initialized during its declaration or initialized in the no-arg contstructor.
Is it Allowed for us to write non-arg constructor for Servlets?

Originally posted by Ben Souther:
[QB]
The init method gets called by the container when the servlet is put into service, not when the servlet object is instanciated.


You mean init() method is called everytime a request is received for servlet? I don't think so. Init() and destroy() will be called only once in Servlet life Cycle.

Please Correct if am wrong me.

Regards, Raghav
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Originally posted by Raghavendra Nittur:


You mean init() method is called everytime a request is received for servlet? I don't think so. Init() and destroy() will be called only once in Servlet life Cycle.

Please Correct if am wrong me.

Regards, Raghav


You are right.They are called just once.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Raghavendra Nittur:


You mean init() method is called everytime a request is received for servlet?


No, that is not what I mean.
The init method is called when the servlet is 'put into service'.
In other words, when the servlet is loaded into the container.

For the most part, this means that the init method will only be called once but this is not guaranteed by the spec.

Look at SRV.2.3.4:

SRV.2.3.4 End of Service
The servlet container is not required to keep a servlet loaded for any particular
period of time. A servlet instance may be kept active in a servlet container for a
period of milliseconds, for the lifetime of the servlet container (which could be a
number of days, months, or years), or any amount of time in between.
When the servlet container determines that a servlet should be removed from
service, it calls the destroy method of the Servlet interface to allow the servlet to
release any resources it is using and save any persistent state. For example, the
container may do this when it wants to conserve memory resources, or when it is
being shut down.

Before the servlet container calls the destroy method, it must allow any
threads that are currently running in the service method of the servlet to complete
execution, or exceed a server-defined time limit.


So, it's perfectly allowable, according to the spec, for a servlet to be taken out of service and then put back into service within the lifecycle of the the application. When being put back into service, I would expect the container to call the init method again.
Raghavendra Nittur
Greenhorn

Joined: Feb 19, 2007
Posts: 29
Great.....!!!

Clap.... Clap.... clap....

Am Learning a lot in avery short period of time.
And I will continue to do it...

Thanks to JavaRanch. and all you guys.

Regards, Raghav.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Final value in a Servlet