• Post Reply Bookmark Topic Watch Topic
  • New Topic

can Static code block be used in java servlets?  RSS feed

 
trupti nigam
Ranch Hand
Posts: 640
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a static java code block be used in servlet?
basicallly I want to read the properties from the properties file which is saved on local machine.
I want to write this in static block so that this can be executed first time when the servlet is invoked.

thanks
Trupti
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, of course static code blocks can be used in servlet code.

However, the normal practice is to initialize the kind of data you are talking about in the init() method for the excellent reason that the init method has access to the servlet context and can read init-params entries from the deployment descriptor.

Hard-coding the location of the properties file is bad practice.

Bill
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!