Example of ServletConfig in Java Servlet

Servlets » on Jan 28, 2013 { 15 Comments } By Sivateja

Let us see why/how to use ServletConfig interface in java servlet….

  • ServletConfig is one of the pre-defined interface.
  • ServletConfig object is used for developing flexible servlets.
  • ServletConfig objct exist one per servlet program.
  • An object of ServletConfig created by the container during its initialization phase.
  • An object of ServletConfig is available to the servlet during its execution, once the servlet execution is completed, automatically ServletConfig interface object will be removed by the container.
  • An object of ServletConfig interface contains <init-param> details at web.xml, of a particular servlet.
  • The moment when we are using an object of ServletConfig, we need to configure the web.xml by writing <init-param> tag under <servlet> tag of web.xml.
  • When ever compiler executes init() mehod then the ServletConfig will be created in general.
  • An object of ServletConfig contain the <init-param> data in the form of key,value pairs, here the keys represents init param names and values are its values, which are represented in the web.xml file

How to Get ServletConfig Object into Servelt

An object of ServletConfig can be obtained in 2 ways, they are…

Way 1

ServletConfig conf = getServletConfig();

In the above statement, we are directly calling getServletConfig() method as it is available in Servlet interface, inherited into GenericServlet and defined and further inherited into HttpServlet and later inherited into our own servlet class.

Way 2

ServletConfig object will be available in init() method of the servlet.

  public void init(ServletConfig config)
// …………………

So finally we are able to create ServletConfig object in our servlet class, then how to get the data from that object… ?

How to Retrieve Data from ServletConfig Interface Object

In order to retrieve the data of the ServletConfig we have two methods, which are present in ServletConfig interface..

  • public String getInitParameter(“param name”);
  • public Enumeration getInitParameterNames();

I am not going to explain about these methods, these are similar to ‘Retrieve Client Input Data in Servlet‘ but here we are retrieving values from web.xml that’s it.

Example of ServletConfig

Directory Structure

Files Required

  • index.html
  • web.xml


<font face="verdana" size="2px">
     <form action="onSCG" method="post">
        Example on ServletConfig<br>
        <input type="submit" value="Calculate Sum">

package java4s;


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 OnServletConfig extends HttpServlet  
    protected void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
    	PrintWriter pw=res.getWriter();

		ServletConfig conf=getServletConfig();

		String s1=conf.getInitParameter("n1");
		String s2=conf.getInitParameter("n2");

		pw.println("n1 value is " +s1+ " and n2 is " +s2);





        <param-name> n1 </param-name>
        <param-value> 100 </param-value>

        <param-name> n2 </param-name>
        <param-value> 200 </param-value>







You Might Also Like

  ::. About the Author .::

Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


15 Responses to “Example of ServletConfig in Java Servlet”
  1. Rish says:

    Awesome explanation! Could you elaborate just this one point:
    An object of ServletConfig interface contains details at web.xml, of a particular servlet
    Do you mean the reference of ServletConfig has access to the details in web.xml file contained in the section of the Servlet which initialized this ServletConfig reference?

  2. Sashi says:

    Examples with background things are very nice.

    I’m preparing some notes based on practical examples.

    I’m referring ur site. It gives very clear idea.


  3. Gajanan Chalke says:

    when we get some data suppose @login time we get userid and password

    we evaluate whether it is true info or not

    and if true the how to redirect him to new page and how to write some detail on that page if i want to

    like if user is admin i want to show him some control menu that i dont want to show to ordinary user?

  4. selvamptl says:

    this is good but you only mention that eclipse.
    i need netbeans preview pls developed.

  5. Sayak Ghosh says:

    Thanx. The explanation is simple and this simplicity is great. Boosted my confidence in study of servlets .

  6. jaswini says:

    Hi can any one explains this two lines

    SevletConfig cfg=getServletConfig();

    Thanks in advance

    • Piyush says:

      The code written above is just to create an object of ServletConfig so that it can store the details regarding specific servlet.If you are not familiar with it,then go through the concept of servlet config as well as ServletContext.

  7. kunal says:

    wrt : I think recommended way to use it is using init(ServletConfig config) method.

  8. Udit Sharma says:

    Can please give me a practical example for servlet config and servlet context. SO that I can get a clear idea and use them appropriately!

  9. chandrashekhar says:

    thanks for giving such a valuable information

  10. Thangapalani says:

    This sesstion very useful know about servlets context….thanks for sharing….

  11. bheemashekhar says:

    we are supporting to all and thanks

  12. dinesh says:

    what is container

  13. Anshu says:

    Good Explanation….

  14. Gayatri says:

    Hi, I am new to servlet.But I was clearly understand after reading this page.
    Good explaination….thanks.


By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.