Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Final value in a Servlet

 
Kevin Nielsen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets are normal Java classes.
 
Rao Raghu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ben Souther
Servlets are normal Java classes.




Witty Reply.
 
Kevin Nielsen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Regards,
Kevin
 
Rao Raghu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got it Kevin! Its the init() method.
There are two forms of init: init() and init(ServletConfig )
 
Kevin Nielsen
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to initialize the final variable either at the point of declaration or in the constrcutor,else the compiler will complain.
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Regards,
Kevin
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic