By Wael

Posted :

ServletConfig and the config implicit object , a tutorial

The ServletConfig interface , defines methods , which can be used to retrieve initialization parameters , passed to a specific servlet , by the servlet container catalina , when creating the servlet and calling its init method .

The config object , is an instance of ServletConfig . It is an implicit object , which is not necessary to declare , and which can be used inside a JSP page . A JSP page is converted , to a servlet .

The @WebServlet annotation used in a java source file , and the web.xml files located in : the WEB-INF folder of each web application , and in the apache-version-number\conf folder , can be used to pass init parameter , to a specific servlet .

what is ServletConfig , config ?

The ServletConfig interface defines the following methods :

As an example , the web.xml file , located under apache-tomcat-9.0.36\conf , defines the following information , related to a servlet . The defined servlet is mapped to any url , which ends with : jsp or jsx .

<!--apache-tomcat-9.0.36/conf/web.xml-->
<servlet>
    <servlet-name>jsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
    <init-param>
        <param-name>fork</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param>
        <param-name>xpoweredBy</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.jsp</url-pattern>
    <url-pattern>*.jspx</url-pattern>
</servlet-mapping>

A web application , is created under the webapps folder , of tomcat , with the name of test . It has the following structure :

# apache-tomcat-9.0.36/webapps/test
|-- WEB-INF
|   |-- classes
|   |   |-- com
|   |       |-- difyel
|   |           |-- test
|   |               |-- ServletGetInitParameter.class
|   |-- src
|   |   |-- ServletGetInitParameter.java
|   |-- web.xml
|-- jsp_page_get_init_parameters.jsp

The web.xml file , under the WEB-INF folder , has the following content :

<?xml version="1.0" encoding="UTF-8"?>


<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                        http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    version="4.0"
    metadata-complete="false">


    <display-name>A test Web application</display-name>

    <servlet>
        <servlet-name>jsp-page</servlet-name>
        <jsp-file>/jsp_page_get_init_parameters.jsp</jsp-file>
        <init-param>
            <param-name>an-init-parameter-name</param-name>
            <param-value>an-init-parameter-value</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>jsp-page</servlet-name>
        <url-pattern>/jsp-page-get-init-parameters</url-pattern>
    </servlet-mapping>


</web-app>

A jsp file is defined under the root of the web application with the name of jsp_page_get_init_parameters.jsp . It has the following content :

<html>
<head>
    <title>Jsp page , Get init parameters</title>
</head>
<body>
    <h3>Get init parameters using the config implicit object</h3>
    <ul>
    <%
        out.println("<li>Jsp Servlet name : "+ config.getServletName() +"</li>");
        /*Print the servlet name*/
        
        java.util.Enumeration<String> initParametersName = config.getInitParameterNames();
        /*Get the init parameter names*/
        if(initParametersName.hasMoreElements()){
            out.println("<li>");
            out.println("<p>Init Parameter Names : Init Parameter values<p>");
            out.println("<ul>");
            while(initParametersName.hasMoreElements()){
                String initParameterName = initParametersName.nextElement();
                String initParameterValue = config.getInitParameter(initParameterName);
                out.println("<li>" + initParameterName + " : " + initParameterValue + "</li>");
            }
            out.println("</ul>");
            out.println("</li>");
        }
        else{
            out.println("<li> No init parameters were passed to the jsp page . </li>");
        }
        
            out.println("<li>Servlet context name : "+ config.getServletContext().getServletContextName() +"</li>");
            /*Print the servlet context name*/
    %>
    </ul>
</body>
</html>	

When the web application is visited using the url : http://localhost:8080/test/jsp_page_get_init_parameters.jsp , the url pattern *.jsp is matched under apache-tomcat-9.0.36/conf/web.xml , as such http://localhost:8080/test/jsp_page_get_init_parameters.jsp will display the following information :

Get init parameters using the config implicit object
  o Jsp Servlet name : jsp
  o Init Parameter Names : Init Parameter values
     o fork : false
     o xpoweredBy : false	
  o Servlet context name : A test Web application

# The servlet context name is related to the web application 
# as a whole  , not to a specific servlet , it is gotten 
# from apache-tomcat-9.0.36/webapps/test/WEB-INF/web.xml

When the web application is visited using the url : http://localhost:8080/test/jsp-page-get-init-parameters , the url pattern *.jsp is matched under apache-tomcat-9.0.36/conf/web.xml , and the url pattern jsp-page-get-init-parameters is matched under apache-tomcat-9.0.36/webapps/test/WEB-INF/web.xml , as such , http://localhost:8080/test/jsp-page-get-init-parameters will display the following information :

Get init parameters using the config implicit object
  o Jsp Servlet name : jsp-page
  o Init Parameter Names : Init Parameter values
     o fork : false
     o an-init-parameter-name : an-init-parameter-value
     o xpoweredBy : false
     o jspFile : /jsp_page_get_init_parameters.jsp
   o Servlet context name : A test Web application

The ServletGetInitParameter.java file has the following content :

package com.difyel.test ;


import java.io.IOException;
import java.io.PrintWriter;

import java.util.Enumeration;

import javax.servlet.annotation.WebServlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet(
    urlPatterns = "/servlet-get-init-parameters",
    initParams = {
        @WebInitParam (name="an-init-param-name" , value="an-init-param-value")
    }
)

public class ServletGetInitParameter extends HttpServlet {
    
    protected void doGet(HttpServletRequest req  , HttpServletResponse resp) throws IOException , ServletException{
        PrintWriter out = resp.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title> Servlet page , Get init parameters</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<ul>");
        ServletConfig config = this.getServletConfig();
        /*Get the ServletConfig object*/
        out.println("<li>Servlet name : "+ config.getServletName() +"</li>");
        /*Print the servlet name*/
        Enumeration<String> initParametersName = config.getInitParameterNames();
        /*Get the init parameter names*/
        if(initParametersName.hasMoreElements()){
            out.println("<li>");
            out.println("<p>Init Parameter Names : Init Parameter values<p>");
            out.println("<ul>");
            while(initParametersName.hasMoreElements()){
                String initParameterName = initParametersName.nextElement();
                String initParameterValue = config.getInitParameter(initParameterName);
                out.println("<li>" + initParameterName + " : " + initParameterValue + "</li>");
            }
            out.println("</ul>");
            out.println("</li>");
        }
        else{
            out.println("<li> No init parameters were passed to the servlet . </li>");
        }
        
            out.println("<li>Servlet context name : "+ config.getServletContext().getServletContextName() +"</li>");
            /*Print the servlet context name*/
        out.println("</body>");
        out.println("</html>");
    }
}

When the web application is visited using the url : http://localhost:8080/test/servlet-get-init-parameters , the url pattern /servlet-get-init-parameters defined under @WebServlet urlPatterns in ServletGetInitParameter.java is matched , as such http://localhost:8080/test/servlet-get-init-parameters will display the following information :

  o Servlet name : com.difyel.test.ServletGetInitParameter
  o Init Parameter Names : Init Parameter values
     o an-init-param-name : an-init-param-value
  o Servlet context name : A test Web application

The source code for the test web application , can be download from here .