ServletConfig

tutorial and example
tutorial and example

Servlet container uses the object of ServletConfig interface to provide the information to servlet during initialization. ServletConfig object fetch this information from web.xml file. So if we have to provide some dynamic information to servlet then we can pass it from web.xml file without making any change in our servlet class.

From servlet 3.0 we can also declare this information in servlet class itself. This can be done via annotations. Now we just required to pass the name with its value within @WebInitParam annotation. This annotation is present in javax.servlet.annotation.WebInitParam package.

Methods of ServletConfig

             Method

              Description

String getInitParameter(String name)

This method returns the value of specific parameter name.

Enumeration getInitParameterNames()

This method provides the enumeration of names.

ServletContext getServletContext()

This method returns the object of ServletContext.

String getServletName()

This method returns the name of servlet instance.

Example of ServletConfig interface

In this example, we are using annotations that contains subject as name and marks as it’s specific value.

DemoServletConfig.java

Output: