ServletContext is one of pre-defined interface
available in javax.servlet.*; Object of ServletContext interface
is available one per web application. An object of ServletContext is
automatically created by the container when the web application is deployed.
Assume there exist a web application with 2 servlet classes, and they need to
get some technical values from web.xml, in this case ServletContext
concept will works great, i mean all servlets in the current web application can
access these context values from the web.xml but its not the case in
ServletConfig, there only particular servelet can access the values from the
web.xml which were written under <servlet> tag, hope you remember. Have
doubt ? just check Example of ServletConfig
How to Get ServletContext Object into Our Servlet Class:
In servlet programming we have 3 approaches for obtaining an object of
ServletContext interface:
Way 1:
ServletConfig conf = getServletConfig();
ServletContext context = conf.getServletContext();
First obtain an object of ServletConfig interface
ServletConfig interface contain direct method to get Context object,
getServletContext();
Way 2
Direct approach, just call getServletContext() method available in
GenericServlet [pre-defined]. In general we are extending our class with
HttpServlet, but we know HttpServlet is the sub class of GenericServlet.
public class r4r extends HttpServlet { public void doGet/doPost(-,-) { //…. } ServletContext ctx = getServletContext(); } |
Way 3:
We can get the object of ServletContext by making use of HttpServletRequest
object, we have direct method in HttpServletRequest interface.
public class r4r extends HttpServlet
{
public void doGet/doPost(HttpServletRequest req,-)
{
ServletContext ctx = req.getServletContext();
}
}
How to Retrieve Data from ServletConfig Interface Object:
ServletContext provide these 2 methods, In order to retrieve the data
from the web..xml [ In web.xml we have write <context-param>tag to
provide the values, and this <context-param> should write outside of <servlet>
tag as context should be accessed by all servlet classes ].
In general database related properties will be written in this type of
situation, where every servlet should access the same data.
public String getInitParameter(“param name”);
public Enumeration getInitParameterNames();
I am not going to explain about these methods, these are similar to ‘Retrieve
Client Input Data in Servlet‘ but here we are retrieving values from web.xml
that’s it.
Example of ServletContext:
Directory Structure:
Servlet on servlet context
settings
bulid
src
r4r.in
on servlet context.java
web content
META-INF
WEB-INF
lib
web.xml
index.html
classpath
project
Files Required:
index.html
OnServletContext.java
web.xml
index.html <font face="verdana" size="2px"> <form action="onContext" method="post"> Example on ServletContext<br> <input type="submit" value="Click Here"> </form> </font> |
OnServletContext.java:
package r4r.in; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class OnServletContext extends HttpServlet { protected void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException { PrintWriter pw=res.getWriter(); res.setContentType("text/html"); // I am using 2nd way to create Context object ServletContext context=getServletContext(); String s1=context.getInitParameter("n1"); String s2=context.getInitParameter("n2"); pw.println("n1 value is " +s1+ " and n2 is " +s2); pw.close(); } } web.xml <web-app> <context-param> <param-name> n1 </param-name> <param-value> 120 </param-value> </context-param> <context-param> <param-name> n2 </param-name> <param-value> 200 </param-value> </context-param> <servlet> <servlet-name>onServletContext</servlet-name> <servlet-class>r4r.OnServletContext</servlet-class> </servlet> <servlet-mapping> <servlet-name>onServletContext</servlet-name> <url-pattern>/onContext</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app> |