Servlet Context
Defined in,
javax.servlet.ServletContext (i/f)
Instance of ServletContext interface's implementation class is created by the container at the application deployment time.
Only one instance is created per web application.
NOTE : The ServletContext object is contained within the ServletConfig object, which the WC provides to the servlet once the servlet is initialized.
Usages
Server side logging.
//Interface ServletContext public void log(String mesg)
To create context scoped (Application scoped) attributes.
public void setAttribute(String name,Object val)
NOTE : Access them always in thread safe manner. as they are mutable as it is a server side object.
To access global scoped (application scoped) attributes use,
//Interface ServletContext Object getAttribute(String name)
To add context scoped attributes
void setAttribute(String name, Object object)
web.xml tags to create context parameters, no annotations available
<context-param>
<param-name>name</param-name>
<param-value>value</param-value>
</context-param>
To access these parameters in a Servlet once the Servlet instance has initialized i.e init() method has been called.
Get ServletContext
API of GenericServlet
public ServletContext getServletContext() //method inherited from GenericServlet
ServletContext API String getInitparameter(String paramName) : returns the parameter value. eg : ctx param name : user_name value : abc In the Servlet : getServletContext().getInitparameter("user_name") ---abc
5.4 Creating request dispatcher H.W
Last updated