Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What is the difference between ServletContext parameter and setting attributes for ServletContext  RSS feed

 
Mahtab Alam
Ranch Hand
Posts: 391
1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why we need to use ServletContext attribute when we already have ServletContext parameters.

Whats the difference between ServletContext Attributes and ServletContext parameters.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!