ServletConfig and ServletContext Interface

  • By
  • September 28, 2022
  • JAVA
ServletConfig and ServletContext Interface

ServletConfig and ServletContext Interface

Whenever we develop the web application we can initialize/configure information from web.xml to the servlet using two objects: config, and context. This config is an object of the ServletConfig interface whereas the context is an object of the ServletContext interface. 

 

The config and context object is created by the Web Server at the time of the creation of an object of Servlet which is going to run and be destroyed at the time of destroying the servlet object. The web server creates a config object separately for each servlet, if we have 10 servlets in one application then the Web Server creates 10 config objects whereas it creates only one context object for one application. In short, the config object is one per servlet and the context object is one per web application. After creating the servlet object Web server/web container immediately calls init() method of the servlet lifecycle and passes that ServletConfig object as a parameter like init(ServletConfig config). Then that config object came to servlet and within  the servet it can get using the getServletConfig() method like,

 

    ServletConfig config =getServletConfig();

 

This config and context object can be used by the servlets to get configuration information from the web.xml file.  To set configuration  information for the config object we can use <init-param> tag in web.xml file and To set configuration  information for the context object we can use <context-param> tag in web.xml file. Servlets then can get the configuration information inside it using

method getInitParameter(Parameter name) like,

 

        E.g config. getInitParameter(“database_name”)

 

If the configuration information is changed from the web.xml file, we don’t need to change the servlet. So it is easier to manage the web application if any specific content is modified from time to time. SevenMentor is one of the best Java Training in Pune which will provide you information with practical experience.

 

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

 

A program that demonstrates the working of ServletConfig and ServletContext

  • ServletConfig 

 

<?xml version=”1.0″ encoding=”UTF-8″?>

<web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns=”http://xmlns.jcp.org/xml/ns/javaee” xsi:schemaLocation=”http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd” id=”WebApp_ID” version=”3.1″>

  <display-name>ServletProj</display-name>

  

    

<servlet>

   <servlet-name>config1</servlet-name>

    <servlet-class>serv.ServConfig1</servlet-class>

 

   <init-param>

     <param-name>dburl</param-name>

     <param-value>jdbc:mysql://localhost:3306/sample</param-value>

   </init-param>

 

    <init-param>

      <param-name>dbuname</param-name>

      <param-value>root</param-value>

    </init-param>

 

  <init-param>

   <param-name>dbpassword</param-name>

   <param-value>12345</param-value>

  </init-param>

  

  <init-param>

   <param-name>dbdriver</param-name>

   <param-value>com.mysql.jdbc.Driver</param-value>

  </init-param>

 

</servlet>

 

<servlet-mapping>

   <servlet-name>config1</servlet-name>

   <url-pattern>/config1</url-pattern>

  </servlet-mapping>

 

<servlet>

<servlet-name>config2</servlet-name>

<servlet-class>serv.ServConfig2</servlet-class>

 

<init-param>

  <param-name>name</param-name>

  <param-value>reena</param-value>

</init-param>

<init-param>

  <param-name>address</param-name>

  <param-value>mumbai</param-value>

</init-param>

 

<init-param>

   <param-name>phone</param-name>

   <param-value>iphone</param-value>

</init-param>

 

</servlet>

 

<servlet-mapping>

<servlet-name>config2</servlet-name>

<url-pattern>/config2</url-pattern>

 

</servlet-mapping>

 

</web-app>

 

The above information can be written for the config object which is separate for each servlet. So writing it within servlet tag inside <init-param> . And then this information can be sent to servlet through config object. Now different Servlet can get that information from the config object

  • First Servlet

 

import java.io.IOException;

import java.io.PrintWriter;

 

import javax.servlet.ServletConfig;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

public class ServConfig1 extends HttpServlet {

 

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException

{

PrintWriter pw =  res.getWriter();

ServletConfig cg= getServletConfig();

 

pw.println(“database url is : “+cg.getInitParameter(“dburl”));

pw.println(“database username  is : “+cg.getInitParameter(“dbuname”));  //getInitPArameter() does not print space, it prints null

 

pw.println(“database password is : “+cg.getInitParameter(“dbpassword”));

 

pw.println(“database driver name is : “+cg.getInitParameter(“dbdriver”));

pw.println(“logical name of servlet id : “+cg.getServletName());

}

}

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

 

  • Second Servlet 

import java.io.IOException;

import java.io.PrintWriter;

 

import javax.servlet.ServletConfig;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

public class ServConfig2 extends HttpServlet {

 

public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException

{

PrintWriter pw =  res.getWriter();

ServletConfig cg= getServletConfig();

pw.print(“Helllo “);

pw.print(cg.getInitParameter(“name”));

 

pw.print(” you are from “+cg.getInitParameter(“address”));

 

pw.print(” and you are using phone is of “+cg.getInitParameter(“phone”)+” type”);

}

}

  • ServletCotext

 

<?xml version=”1.0″ encoding=”UTF-8″?>

 

<web-app version=”3.1″ id=”WebApp_ID” xsi:schemaLocation=”http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd” xmlns=”http://xmlns.jcp.org/xml/ns/javaee” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”>

 

<context-param>

 

  <param-name>name</param-name>

 

  <param-value>Pooja</param-value>

 

</context-param>

 

<context-param>

 

<param-name>address</param-name>

 

<param-value>Mumbai</param-value>

 

</context-param>

 

<context-param>

 

<param-name>Mobno</param-name>

 

<param-value>6543434537</param-value>

 

</context-param>

 

<servlet>

 

<servlet-name>kl</servlet-name>

 

<servlet-class>Serv1</servlet-class>

 

</servlet>

 

<servlet-mapping>

 

<servlet-name>kl</servlet-name>

 

<url-pattern>/kl</url-pattern>

 

</servlet-mapping>

 

<servlet>

 

<servlet-name>kll</servlet-name>

 

<servlet-class>Serv1</servlet-class>

 

</servlet>

 

<servlet-mapping>

 

<servlet-name>kll</servlet-name>

 

<url-pattern>/kll</url-pattern>

 

</servlet-mapping>

 

</web-app>

 

The above information can be written for a context object which is common for each servlet. So writing it outside the servlet tag and inside <context-param>. And then this information can be sent to servlet through config object. Now different Servlet can get that information from the context object

 

First ServletContext

 

import java.io.IOException;

import java.io.PrintWriter;

 

import javax.servlet.ServletContext;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

public class Serv1 extends HttpServlet {

 

    public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException

    {

    PrintWriter out = res.getWriter();

    ServletContext ct = getServletContext();

   

    out.println(“hi”);

    out.println(ct.getInitParameter(“name”));

     

    out.println(“you are from “+ct.getInitParameter(“address”));

   

    }

}

For Free, Demo classes Call: 020-71173125
Registration Link: Click Here!

Second ServletContext

 

import java.io.IOException;

import java.io.PrintWriter;

 

import javax.servlet.ServletContext;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

public class Serv2 extends HttpServlet {

 

    public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException

    {

    PrintWriter out = res.getWriter();

    ServletContext ct = getServletContext();

   

    out.println(“hi”);

    out.println(ct.getInitParameter(“name”));

     

    out.println(ct.getInitParameter(“address”));

   

    }

}

 

Author:-

Pooja Nandode-Bhavsar

SevenMentor Pvt Ltd

Call the Trainer and Book your free demo Class for now!!!
call icon

© Copyright 202! | Sevenmentor Pvt Ltd.

Submit Comment

Your email address will not be published.

*
*