posted 3 years ago
Because an init parameter of the ServletContext can only be String value, specified in the web.xml deployment descriptor, whereas an attribute can be any kind of Object set by the application at runtime. Let's say for (bad) example your application needs a JDBC connection to some relational database. You can configure the connection URL as an init parameter in web.xml. You can read the value of that init parameter (a String) in a ServletContextListener, and use it to establish a JDBC connection that you then make available as a ServletContext attribute (as a Connection object). Again, this isn't something you should do, but it does illustrate the basic point.