Servlet Context
Defined in,
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.
To create context scoped (Application scoped) attributes.
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,
To add context scoped attributes
web.xml tags to create context parameters, no annotations available
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
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