Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

servlets - Why use ServletContext.setAttribute()?

Why would we set ServletContext parameters using the setAttribute() method, as we can accomplish the same thing by setting parameters in web.xml and fetching them using getInitParameter()?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The servletContext.setAttribute() is dynamic which can be set and reset during runtime.

Where as init-parameter specified in web.xml is static which will not change during the lifetime of application.

Example:

  • The database properties like database name configuration propety. It will be mostly configured as context init-param
  • And if you want to set property value which derives based on runtime changes, like "last user logged in" or "number of failures" should be context attributes.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...