Methods | Description |
---|---|
Object getAttribute(String name) | returns the container attribute with the given name, or null if there is no attribute by that name. |
String getInitParameter(String name) | returns parameter value for the specified parameter name, or null if the parameter does not exist |
Enumeration getInitParameterNames() | returns the names of the context's initialization parameters as an Enumeration of String objects |
void setAttribute(String name,Object obj) | set an object with the given attribute name in the application scope |
void removeAttribute(String name) | removes the attribute with the specified name from the application context |
ServletContext app = getServletContext(); ServletContext app = getServletConfig().getServletContext();
getServletContext() method of ServletConfig interface returns the object of ServletContext. getServletContext() method of GenericServlet class returns the object of ServletContext.Syntax of getServletContext() method
public ServletContext getServletContext()Example of getServletContext() method
//We can get the ServletContext object from ServletConfig object ServletContext application=getServletConfig().getServletContext(); //Another convenient way to get the ServletContext object ServletContext application=getServletContext();
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloServlet extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { res.setContentType("text/html"); PrintWriter pw=res.getWriter(); //creating ServletContext object ServletContext context=getServletContext(); //Getting the value of the initialization parameter and printing it String driverName=context.getInitParameter("driverName"); pw.println("driver name is="+driverName); pw.close(); } }
<web-app> <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>HelloServlet</servlet-class> </servlet> <context-param> <param-name>driverName</param-name> <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value> </context-param> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/context</url-pattern> </servlet-mapping> </web-app>
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class DemoServlet extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { res.setContentType("text/html"); PrintWriter out=res.getWriter(); ServletContext context=getServletContext(); Enumeration<string> e=context.getInitParameterNames(); String str=""; while(e.hasMoreElements()){ str=e.nextElement(); out.print("<br> "+context.getInitParameter(str)); } } }
<web-app> <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>HelloServlet</servlet-class> </servlet> <context-param> <param-name>driverName</param-name> <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value> </context-param> <context-param> <param-name>name</param-name> <param-value>dinesh</param-value> </context-param> <context-param> <param-name>email</param-name> <param-value>admin@dineshonjava.com</param-value> </context-param> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/context</url-pattern> </servlet-mapping> </web-app>
Labels: Servlet