Sunday, January 17, 2016

ServletContext

Scope: All servlets all clients
Life cycle:
  1. created when webapp boots
  2. destroyed when webapp closed


 ServletContext servletContext = this.getServletContext();
 //ServletContext servletContext servletContext = this.getServletConfig().getServletContext();
 servletContext.setAttribute("ABC", new Object()); //(String, Object)

Init Parameter
  
  <context-param>
    <param-name>Country</param-name>
    <param-value>China</param-value>
  </context-param>


    request.getServletContext().getInitParameter("Country");

Get Resource
 Properties pp = new Properties();
 InputStream fis = getServletContext().getResourceAsStream("WEB-INF/settings.properties");
 pp.load(fis);

 getServletContext().getRealPath("WEB-INF/settings.properties");

No comments:

Post a Comment