ServletConfig Interface

ServletConfig Interface in Java

In this article, I am going to discuss the ServletConfig Interface in Java Application. Please read our previous article where we discussed Request Redirection in Servlet Web Application. At the end of this article, you will understand the following pointers.

  1. ServletConfig Interface
  2. Advantage of ServletConfig
  3. ServletConfig methods
  4. How to get ServletConfig object?
  5. Example of ServletConfig to get the initialization parameters
ServletConfig Interface in Java

ServletConfig is an object, it’ll store all the configuration details of a specific servlet, where the configuration details include the logical name of the servlet, initialization parameters, reference of ServletContext object, and so on. ServletConfig is an object, it’ll provide the entire view of a specific servlet. In the web application, the container will prepare ServletConfig objects individually to every and each servlet. In web application execution, the container will prepare ServletCofig object immediately after servlet instantiation and just before calling init() method in servlet initialization. The container will destroy the ServletConfig object just before servlet de-instantiation. Due to the above reasons, the life cycle of the ServletConfig object is up to a specific servlet.

If we declare any data in the ServletConfig object then that data is going to be shared up to the respective servlet. Due to the above reasons, the scope of the ServletConfig object is up to a particular servlet.

In web applications, the ServletConfig object will allow only parameter data, it’ll not allow attributes data.

ServletConfig Interface

ServletConfig object can be created and removed only by the server, not by the programmer. ServletConfig object is created when the client sends the request to the particular servlet and when the servlet object is created. ServletConfig object will be removed by the server when we close the server or when the servlet is deleted from the project. One ServletConfig object is created for one Servlet it means in one project we can contain multiple ServletConfig objects based on the multiple servlets available. This servletConfig object can be used only by the particular servlet which contains the ServletConfig object. But the ServletConfig object of one servlet cannot be used by any other servlet which maybe belongs to the same or other projects.

Advantage: No need to update in web.xml if any changes occur.

ServletConfig methods
  1. getInitParameter(java.lang.String name): It is used to return a String containing the value of the named initialization parameter, or null if the parameter does not exist.
  2. getInitParameterNames(): It returns the names of the servlet’s initialization parameters as an Enumeration of String objects, or an empty Enumeration if the servlet has no initialization parameters.
  3. getServletContext(): It returns a reference to the ServletContext in which the caller is executing.
  4. getServletName(): It returns the name of this servlet instance.
How to get ServletConfig object?

In web applications, there are two ways to get ServletConfig object:

Use getServletConfig() method from Servlet Interface
Example: ServletConfig config = getServletConfig();

Override init() method
Example:
public class MyServlet extends HttpServlet
{
       ServletConfig config;
       public void init (ServletConfig config)
       {
            this.config = config;
       }
}

To get logical name of the servlet from its ServletConfig object we have to use the following method:
public String getServletName()
Example: String Iname = congif.getServletName();

If we want to provide initialization parameters inServletConfig object then first we have to declare them in the web.xml file. To declare initialization parameters in the web.xml file we have to use the following XML tags.

<web-app>
<servlet>
<init-param>
<param-name>name</param-name>
<param-value>value</param-value>
</init-param>
 ……….
</servlet>
 ……….
</web-app>

If we declare initialization parameters with the above approach then the container will read them and store onto ServletConfig object at the time of creation when it receives a request from the client.

To get a particular initialization parameter from the ServletConfig object we have to use the following method:
public String getInitParameter(String name)
Example: String a = config.getInitParameter(“a”);

To get all the initialization parameters from the ServletConfig object we have to use the following method:
public Enumeration getInitParameterNames()
Example: Enumeration e=config.getInitParameterNames();

Example of ServletConfig to get the initialization parameters

In this example, we are getting all the initialization parameters from the web.xml file and printing this information in the servlet.

web.xml

<web-app>
 <servlet>
  <servlet-name>MyServlet</servlet-name>
  <servlet-class>MyServlet</servlet-class>
  <init-param>
   <param-name>driver</param-name>
   <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
  </init-param>
  <init-param>
   <param-name>url</param-name>
   <param-value>jdbc:odbc:sri</param-value>
  </init-param>
  <init-param>
   <param-name>user</param-name>
   <param-value>system</param-value>
  </init-param>
  <init-param>
   <param-name>password</param-name>
   <param-value>servlet</param-value>
  </init-param>
 </servlet>
 <servlet-mapping>
  <servlet-name>MyServlet</servlet-name>
  <url-pattern>/config</url-pattern>
 </servlet-mapping>
</web-app>
MyServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet
{
    protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        response.setContentType ("text/html");
        PrintWriter out = response.getWriter ();
        ServletConfig config = getServletConfig ();
        String logicalName = config.getServletName ();
        String driver = config.getInitParameter ("driver");
        String url = config.getInitParameter ("url");
        String user = config.getInitParameter ("user");
        String password = config.getInitParameter ("password");
        out.println ("<html><body><h1>");
        out.println ("Logical Name : " + logicalName + "<br><br>");
        out.println ("Driver : " + driver + "<br><br>");
        out.println ("Url : " + url + "<br><br>");
        out.println ("Password : " + password + "<br><br>");
        out.println ("</h1></body></html>");
    }
}

Output

ServletConfig Interface in Java with Example

In the next article, I am going to discuss the ServletContext Interface. Here, in this article, I try to explain ServletConfig Interface in Java Web Application. I hope you enjoy this ServletConfig Interface in Java article.

Leave a Reply

Your email address will not be published.