ServletConfig Interface

ServletConfig interface belongs to javax.servlet package. It is per servlet created by web container.

Changes made in web.xml file will not require any change in the servlet class.

ServletConfig interface having four main method to access details from web.xml

It is easy to get ServletConfig object inside an Servlet class.

ServletConfig servletConfig =getServletConfig();

Below is the content from web.xml file. <servlet> tag denoted ServletConfigas as it is per servlet.

To access above content we have below methods:

getInitParameter(“country”)): This will return parameter value depending upon parameter name.

getInitParameterNames(); This will return all parameter name written inside <init-param> tag.

getServletContext(): This will return ServletConext Object.

getServletName()): This will return Servlet Name from <servlet-name> tag mentioned in above screenshot.

Example

Create a LoginServlet.java class with the below content mentioned in below screenshot:

Create a web.xml file with the below content to provide data related to the LoginServlet.java servlet class.

OUTPUT
Country = India
param = country
Servlet Name = loginServlet

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan