Servlet Config by R4R Team

ServletConfig:

Here let us see 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 object 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:

                          Servlet on servlet config

                                                          settings
                                                                      bulid
                                                                               src
                                                                                      r4r.in 
                                                                                                 on servlet config.java                                  
                                                                                                                                 web content
                                                                                                                                                META-INF
                                                                                                                                                    WEB-INF
                                                                                                                                                                 lib
                                                                                                                                                                  web.xml
                                                                                                                                                                    classpath
                                                                                                                                                                                project

                                                                                                                                             
Files Required:

index.html
OnServletConfig.java
web.xml


index.html:

<font face="verdana" size="2px">

<form action="onSCG" method="post">

Example on ServletConfig<br>

<input type="submit" value="Calculate Sum">

</form>

</font>


OnServletConfig.java:

package r4r.in;

import java.io.IOException;

import java.io.PrintWriter;

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();

res.setContentType("text/html");

ServletConfig conf=getServletConfig();

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

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

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

pw.close();

}

}

web.xml

<web-app>

<servlet>

<servlet-name>onServletConfig</servlet-name>

<servlet-class>r4r.OnServletConfig</servlet-class>

<init-param>

<param-name> n1 </param-name>

<param-value> 100 </param-value>

</init-param>

<init-param>

<param-name> n2 </param-name>

<param-value> 200 </param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>onServletConfig</servlet-name>

<url-pattern>/onSCG</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.html</welcome-file>

</welcome-file-list>

</web-app>

Leave a Comment:
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!