ServletContext (one per WEB APPLICATION):
- ServletContext is an interface which is present in javax.servlet.* package.
- Whenever we want to give a common data or global data to the group of servlets which belongs to same web application then we must create an object of ServletContext interface.
- An object of ServletContext will be created by servlet container (server) whenever we deploy into the server.
- In order to provide a common data to a group of servlets, we must write that data into web.xml file with the tag called <context-param>...</context-param>. This tag must be written with in <web-app>...</web-app> before <servlet>.
- xml entries related to ServletContext interface.
<web-app>
<context-param>
<param-name>Name of the param</param-name>
<param-value>Value of the param</param-value>
</context-param>
<servlet>
.........
.........
</servlet>
<servlet-mapping>
..........
..........
</servlet-mapping>
</web-app>
- Whatever the data we write with in <context-param>...</context-param> that data will be paste automatically in the object of ServletContext interface and this object contains the in the form of (key, value) pair. Here, key represents context parameter name and value represents context parameter value.
- The value of key must be always unique; if duplicate values are placed we get recent duplicate value for the key by overlapping previous values.
For example:
<web-app>
<context-param>
<param-name>driver</param-name>
<param-value>oracle.jdbc.driver.OracleDriver</param-value>
</context-param>
<context-param>
<param-name>url</param-name>
<param-value>jdbc:oracle:thin:@localhost:1521:Hanuman</param-value>
</context-param>
<servlet>
..........
..........
</servlet>
<servlet-mapping>
..........
..........
</servlet-mapping>
</web-app>