wellnwill

ServletContext

3. ServletContext parameters

  • ServletContext is an interface in javax.servlet package
  • Web container vendor provides implementing class for the same
  • One Web application has only one ServletContext object i.e. one for all servlets
  • Data is shared among all web components
  • Data that is common among all users and across all servlet can be stored in ServletContext
  • ServletContext object can be obtained from ServletConfig.getServletContext
  • This data can be specified in web.xml

...
  <context-param>
    <param-name>institute</param-name>
    <param-value>Info Campus</param-value>
  </context-param>
...

Fetch ServletContext parameters using getInitParameter

//Get context parameter values
ServletContext ctx = cfg.getServletContext();
String instituteName = ctx.getInitParameter("institute");

Fetch ServletContext parameters using getInitParameterNames

ServletContext ctx = cfg.getServletContext();
   Enumeration ctxParamEnum = ctx.getInitParameterNames();
  List ctxParamList = Collections.list(ctxParamEnum);
  for (String ctxParam : ctxParamList) {
   out.print(" 
"); }
 
Footer with Map