This section contains the Introduction to Servlet Context.
Introduction to Servlet Context
For getting the information regarding the environment in which servlet are running, we incorporate ServletContext interface. The context is also called as servlet context or Web context and is created by the Web container as an object of the ServletContext interface.
For each web application, there is only one ServletContext and it is accessible to all the active resources of that application. This context is also used to share information with one another.
Every web application has exactly one instance of javax.servlet.ServletContext . The context is initialized at the time that the web application is loaded.
This object represents a context within which a Web application executes. You can use the ServletContext object to find path information of other files of the Web application, access other servlets of the Web application, and log messages to the application server log file. You can also use the ServletContext object to set attributes that other servlets of your application can access.
Given below some methods of Servlet Context interface :
Methods | Description |
public void setAttribute(String,Object) | Binds the object with a name and stores the name/value pair as an attribute of the ServletContext object. If an attribute already exists, then this method replaces the existing attribute. |
public Object getAttribute(String attrname) | Returns the object stored in the ServletContext object with the name passed as a parameter. |
public Enumeration getAttributeNames() | Returns an Enumeration of String object that contain names of all the context attributes. |
public String getInitParameter(String pname) | Returns the value of the initialization parameter with the name passed as a parameter. |
public Enumeration getInitParameterNames() | Returns an Enumeration of String object that contain names of all the initialization parameters. |
public int getMajorVersion() | Returns an integer value that specifies the major version of the Servlet API that the Web container supports. If your Web container supports the version 2.4 of the Servlet API, this method will return 2. |
public int getMinorVersion() | Returns an integer value that specifies the minor version of the Servlet API that the Web container supports. If your Web container supports the version 2.4 of the Servlet API, this method will return 4. |
[ 0 ] Comments