Saturday 7 April 2018

Introduction to ServletContext object

Introduction to ServletContext object,ServletContext ,getInitParameterNames(),ServletContext ,getServletContext() ,Usage of ServletContext Interface,Advantage of ServletContext,Example of ServletContext to get all the initialization parameters,Example of ServletContext,Usage of ServletContext Interface,Advantage of ServletContext,

One ServletContext is created by the web application for all servlets and this object lets the servlets talk to the servlet container. 

The web app parameters that are configured in the deployment descriptor (web.xml) in the <context-para,> elements as shown in the xml below, can be retrieved by the servlet using the methods getInitParameter(String) & getInitParameterNames() of the ServletContext object. 

The ServletContext object itself can be obtained by the servlets using the method getServletContext() which they inherit from their superclass GenericServlet

Advantage of ServletContext

Easy to maintain if any information is shared to all the servlet, it is better to make it available for all the servlet. We provide this information from the web.xml file, so if the information is changed, we don't need to modify the servlet. Thus it removes maintenance problem.

Usage of ServletContext Interface

There can be a lot of usage of ServletContext object. Some of them are as follows:

  1. The object of ServletContext provides an interface between the container and servlet.
  2. The ServletContext object can be used to get configuration information from the web.xml file.
  3. The ServletContext object can be used to set, get or remove attribute from the web.xml file.
  4. The ServletContext object can be used to provide inter-application communication.
 <web-app>  
    <context-param>  
        <param-name>someParam1</param-name>  
        <param-value>1000</param-value>  
    </context-param>  
  
    <context-param>  
        <param-name>someParam2</param-name>  
        <param-value>true</param-value>  
    </context-param>  
  </web-app

 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  
 {  
    getServletContext().getInitParameter("someParam1");  
    getServletContext().getInitParameterNames();  
 }  

Example of ServletContext to get all the initialization parameters

In this example, we are getting all the initialization parameter from the web.xml file. For getting all the parameters, we have used the getInitParameterNames() method in the servlet class

Demo.java


          import java.io.*;
          import javax.servlet.*;  
         import javax.servlet.http.*;  

         public class Demo 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.xml
      <web-app>  
  
      <servlet>  
      <servlet-name>Dashzin</servlet-name>  
      <servlet-class>Demo</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>Dashzin</servlet-name>  
       <url-pattern>/context</url-pattern>  
      </servlet-mapping>  
  
      </web-app>  








DashZin

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

0 comments:

Post a Comment