0

What is the need of using init-parameters in servlet configuration? If answer is to let the servlet initialize it's parameters, then the question to this answer is "Does this initialized value persists when server boots next time to instantiate the Servlet again?". Storing these parameters on file can be a better option then. I just want to clarify if I can set the value of init-parameter in destroy() so that when server boots next time it will have updated value for this parameter in init().

2 Answers2

0

Yes to your first question, no for the second. Init parameters are supposed to be used within ServletConfig object, but they are immutable (how can you do setInitParameter?)

Andrea
  • 2,714
  • 3
  • 27
  • 38
  • Thanks Andrea. Could you please answer following query in the same context. How does value of init parameter persist? Suppose I have init parameter named EmpSalary with value 1000 in web.xml. I have fetched this value in init() and then at some point in my servlet I have updated this value to 2000 then how can I ensure that next time when server boots up I'll get 2000 in init()? (Storing this in file will do my job, but then what init-parameter are of use?) – Tushar Baviskar Apr 27 '14 at 17:34
  • The short answer is: you can't using servlet standard procedures. As I told you, parameters you set in web.xml are immutable. If you need that kind of persistence you shouldn't use them (think about them as final static constants) and instead you should use something like a database or a file or something like that. Best, Andrea – Andrea Apr 27 '14 at 18:38
  • Additionally, init() method is called once per servlet (this is not very exact but it covers 99.99%) so even if you would be able to update that value (and this is not possible) you would read again that value aftet the next startup, which sounds like strange (a production app shouldn't crash or shutdown) – Andrea Apr 27 '14 at 19:01
  • Yep, that's what I am wondering about, if they are immutable then why do we need them. I mean we can directly define final variables (constants)in class itself to have initialization parameters. I am not yet clear, with the need of init-parameter? – Tushar Baviskar Apr 28 '14 at 17:11
0

This is what I wanted to know http://wiki.metawerx.net/wiki/AdvantagesOfSpecifyingParameterValuesInWeb.xml