Get all updates of Java4s
Follow Us: Email Newsletter

Example of ServletConfig in Java Servlet

Servlets » On Jan 28, 2013 { 14 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




You Might Also Like

  ♦ About Author ♦

Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.


14 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

  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.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

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