Why ServletContext.setAttribute used? While we accomplish same task with web.xml (with context-param).I tried same coding with both of the methods, I did not see any difference between them.
Asked
Active
Viewed 329 times
2 Answers
0
An application may wish to set or modify a ServletContext attribute at runtime. web.xml only sets the initial value.

Mark Thomas
- 16,339
- 1
- 39
- 60
-
can you give an example, what is the mean of "modify the value at runtime"? – sarojni Gupta Mar 12 '16 at 09:09
0
An example of usage of a ServletContext attribute would be to control the number of concurrent sessions. It allows the application to reject new connections when this number is above a value which has been benchmarked to be the maximum acceptable before performances downgrade too much or even the application could crash.
You can know the creation and destruction of sessions with a SessionListener, and the best place to store the value is a ServletContext attribute.

Serge Ballesta
- 143,923
- 11
- 122
- 252