logo

ServletContext Interface


Show

An item of ServletContext is created with the aid of using the web container at time of deploying the project. This item may be used to get configuration statistics from the net.xml report. There is the simplest one ServletContext item consistent with Web application.

If any statistics is shared to many servlets, it's far higher to offer it from the net.xml to report the use of the element.

Benefits of Servlet Context

It is easy to carry on if any information is shared to all servlets. It's miles higher to make it available for all the servlets. We offer these records from the web.xml file, so if the records are changed, we do not want to adjust the servlet. Thus it gets rid of the maintenance problem.

Usage of Servlet Context interface

Servlet context objects have a lot of uses. Some of them are given below:

  1. The item of ServletContext gives an interface among the box and servlet.
  2. The ServletContext item may be used to get configuration facts from the web.xml file.
  3. The ServletContext item may be used to set, get or dispose of characteristics from the web.xml file.
  4. The ServletContext item may be used to offer inter-software communication.

The most commonly used methods of ServletContext Interface

Some Commonly used methods of servletContext interface are:

  1. Public String getInitParameter(String Name):Returns the parameter usefulness for the required parameter name.
  2. public Enumeration getInitParameterNames():Returns the names of the context's initialization parameters.
  3. public void setAttribute(String call,Object item):units the given item withinside the software scope.
  4. public Object getAttribute(String Name):Returns the characteristic for the required call.
  5. public Enumeration getInitParameterNames():Returns the names of the context's initialization parameters as an Enumeration of String objects.
  6. public void removeAttribute(String call):Removes the characteristic with the given call from the servlet context.

How to get the object of ServletContext interface

  1. getServletContext() method of ServletConfig interface gives back the object of ServletContext.
  2. getServletContext() method of GenericServlet class gives back the object of ServletContex.

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();   

Syntax to provide the initialization parameter in Context scope

The context-param detail, subelement of web-app, is used to outline the initialization parameter withinside the utility scope. The param-call and param-fee are the sub-factors of the context-param. The param-call detail defines parameter call and and param-fee defines its usefulness.

<web-app>  
 ......  
  <context-param>  
    <param-name>parametername</param-name>  
    <param-value>parametervalue</param-value>  
  </context-param>  
 ......  
</web-app>

Example of Servlet Context to get the initialization parameter

In this example, we are getting the initialization parameter from the web.xml record and printing the price of the initialization parameter. Notice that the item of ServletContext represents the utility scope. So if we extrade the price of the parameter from the web.xml record, all of the servlet training gets the modified price. So we do not want to alter the servlet. So it's far higher to have the not unusual place records for maximum of the servlets withinside the web.xml record via way of means of context-param element. Let's see the easy example:

DemoServlet.java

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 pw=res.getWriter();  

//creating ServletContext object  
ServletContext context=getServletContext();   

//Getting the value of the initialization parameter and printing it  
String driverName=context.getInitParameter("dname");  
pw.println("driver name is="+driverName); 

pw.close();  

}}  


Web.xml

<web-app> 
  
<servlet>  
<servlet-name>sonoojaiswal</servlet-name>  
<servlet-class>DemoServlet</servlet-class>  
</servlet>   

<context-param>  
<param-name>dname</param-name>  
<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>  
</context-param>    

<servlet-mapping>  
<servlet-name>sonoojaiswal</servlet-name>  
<url-pattern>/context</url-pattern> 
</servlet-mapping>  

</web-app>

Example of ServletContext to get all the initialization parameters

In this example, we are getting all of the initialization parameters from the web.xml file. For getting all of the parameters, we've got used the getInitParameterNames() approach withinside the servlet class.


DemoServlet.java

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("
"
+context.getInitParameter(str)); } }}

Web.xml

<web-app>    

<servlet>  
<servlet-name>sonoojaiswal</servlet-name>  
<servlet-class>DemoServlet</servlet-class>  
</servlet>   

<context-param>  
<param-name>dname</param-name>  
<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>  
</context-param>    

<context-param>  
<param-name>username</param-name>  
<param-value>system</param-value>  
</context-param>   

<context-param>  
<param-name>password</param-name>  
<param-value>oracle</param-value>  
</context-param>   

<servlet-mapping>  
<servlet-name>sonoojaiswal</servlet-name>  
<url-pattern>/context</url-pattern>  
</servlet-mapping>  

</web-app>