javax.servlet.ServletContext

A web application can have multiple servlets to process client requests. Each Servlet will have individual ServletConfig object. However, all the servlets are running in the same context. This context is unique for the web application. ServletConfig instance holds the reference to the same context. When the web application is installed in the web container, an instance that implements ServletContext interface is created. This instance is the abstraction of the web:application itself. A ServletContext object is the Servlet’s connection to both the container and other parts of the web application, multiple servlets can share information and resources by using ServletContext object. With the help of this object one Servlet can communicate with another Servlet running in the same web application.

List of important methods in ServletContext interface Methods to deal with attributes and init parameters:-

•     public String getInitParameter(String name):- Returns a String containing the value of the named context-wide initialization parameter, or null if the parameter does not exist. This method can make available configuration information useful to an entire "web application". For example, it can provide a webmaster’s email address or the name of a system that holds critical data.

•     public Enumeration getInitParamNames():-Returns the names of the context’s initialization parameters as an Enumeration of String objects, or an empty Enumeration if the context has no initialization parameters.

•     public void setAttribute(String name, Object value):- Binds an object to a given attribute name in this servlet context, if the name specified is already used for an attribute, this method will replace the attribute.

•     public Object getAttribute(String name):- Returns the attribute bound to the ServletContext object with the given name, or null if there is no attribute by that name.

•     public Enumeration getAttributeName():- Returns an Enumeration containing the attribute names available within this Servlet context.

Methods to get info about Servlet container and logging information:

•     public void log(String info):- Writes the specified message to a Servlet log file, usually an event log. The name and type of the Servlet log file is specific to the Servlet container.

•     public String getServerInfo():-Returns the name and version of the Servlet container on which the Servlet is running.

•     public int getMajorVersion():-Returns the major version of the Java Servlet API that this Servlet container supports. It returns a number 2. (In Servlet 2.4 version 2 is major version)

•     public int getMinorVersion():- This method returns a number 4 (if contains supports Serviets 2.4).

Method to communicate with other servlets in the web application

•     RequestDispatcher getRequestDispatcher(String name):- Returns the RequestDispatcher object.

Subscribe / Share

Article by Admin

Authors bio is coming up shortly. Read 3 articles by Admin