ServletConfig Interface

Advertisements

Prev Tutorial Next Tutorial

ServletConfig Interface in Servlet

An object of ServletConfig is created by the web container for each servlet using its initialization phase. This object can be used to get configuration information from web.xml file.

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() method then the ServletConfig will be created in general. An object of ServletConfig contain the

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

Advantage of ServletConfig

If the configuration information is modified 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.
The core advantage of ServletConfig is that you don't need to edit the Servlet file if information is modified from the web.xml file.

Methods of ServletConfig interface

  • public String getInitParameter(String name):Returns the parameter value for the specified parameter name.
  • public Enumeration getInitParameterNames():Returns an enumeration of all the initialization parameter names.
  • public String getServletName():Returns the name of the Servlet.
  • public ServletContext getServletContext():Returns an object of ServletContext.

How to Get ServletConfig Object into Servelt

An object of ServletConfig can be obtained in 2 ways,

Way 1.

Syntax

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.

Syntax

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.

Syntax

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.


Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements