0

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.

sarojni Gupta
  • 21
  • 1
  • 7

2 Answers2

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
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