An object of ServletConfig can be obtained in two ways, they are by calling getServletConfig () method and by calling init (ServletConfig).
getServletConfig () is the method which is available in javax.servlet.Servlet interface. This method is further inherited and defined into a class called javax.servlet.GenericServlet and that method is further inherited into another predefined class called javax.servlet.http.HttpServlet and it can be inherited into our own servlet class.
For example:
public class serv1 extends HttpServlet { public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { ......... ......... ServletConfig config=this.getServletConfig (); ......... ......... } };
In the above example an object config contains (key, value) pair data of web.xml file which are written under <init-param> tag of <servlet> tag.
For example:
public class serv2 extends HttpServlet { ServletConfig sc; public void init (ServletConfig sc) { Super.init (sc); // used for calling init (ServletConfig) method of HttpServlet this.sc=sc; // ServletConfig object sc is referenced } ........ ........ };
In order to get the data from ServletConfig interface object we must use the following methods:
public String getInitParameter (String); - 1 public Enumeration getInitParameterNames (); - 2
Method-1 is used for obtaining the parameter value by passing parameter name.
String val1=config.getInitParameter ("v1"); String val2=config.getInitParameter ("v2"); String val3=config.getInitParameter ("v3");
Method-2 is used for obtaining all parameter names and their corresponding parameter values.
For example:
Enumeration en=config.getInitParameterNames (); while (en.hasMoreElements ()) { Object obj=en.nextElement (); String pname= (String) obj; String pvalue=config.getInitParameter (pname); out.println (pvalue+" is the value of "+pname); }
Serv1.java:
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public class Serv1 extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter pw = res.getWriter(); ServletConfig config = getServletConfig(); String val1 = config.getInitParameter("v1"); String val2 = config.getInitParameter("v2"); String val3 = config.getInitParameter("v3"); String val4 = config.getInitParameter("v4"); pw.println("<h3> Value of v1 is " + val1 + "</h3>"); pw.println("<h3> Value of v2 is " + val2 + "</h3>"); pw.println("<h3> Value of v3 is " + val3 + "</h3>"); pw.println("<h3> Value of v4 is " + val4 + "</h3>"); } };
Serv2.java:
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public class Serv2 extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter pw = res.getWriter(); ServletConfig config = getServletConfig(); Enumeration en = config.getInitParameterNames(); while (en.hasMoreElements()) { Object obj = en.nextElement(); String pname = (String) obj; String pvalue = config.getInitParameter(pname); pw.println("</h2>" + pvalue + " is the value of " + pname + "</h2>"); } } };
<web-app> <servlet> <servlet-name>abc</servlet-name> <servlet-class>Serv1</servlet-class> <init-param> <param-name>v1</param-name> <param-value>10</param-value> </init-param> <init-param> <param-name>v2</param-name> <param-value>20</param-value> </init-param> </servlet> <servlet> <servlet-name>pqr</servlet-name> <servlet-class>Serv2</servlet-class> <init-param> <param-name>v3</param-name> <param-value>30</param-value> </init-param> <init-param> <param-name>v4</param-name> <param-value>40</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>abc</servlet-name> <url-pattern>/firstserv</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>pqr</servlet-name> <url-pattern>/secondserv</url-pattern> </servlet-mapping> </web-app>
Develop a flexible servlet that should display the data of the database irrespective driver name, table name and dsn name?
Answer:
DbServ.java
:import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; import java.io.*; public class DbServ extends HttpServlet { ServletConfig sc = null; public void init(ServletConfig sc) throws ServletException { super.init(sc); this.sc = sc; } public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter pw = res.getWriter(); String dname = sc.getInitParameter("dname"); String url = sc.getInitParameter("url"); String tab = sc.getInitParameter("tab"); try { Class.forName(dname); Connection con = DriverManager.getConnection(url, "scott", "tiger"); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("select * from " + tab); while (rs.next()) { pw.println("<h2>" + rs.getString(1) + "" + rs.getString(2) + "" + rs.getString(3) + "</h2>"); } con.close(); } catch (Exception e) { res.sendError(503, "PROBLEM IN DATABASE..."); } } };
<web-app> <servlet> <servlet-name>abc</servlet-name> <servlet-class>DbServ</servlet-class> <init-param> <param-name>dname</param-name> <param-value>oracle.jdbc.driver.OracleDriver ()</param-value> </init-param> <init-param> <param-name>url</param-name> <param-value>jdbc:oracle:thin:@localhost:1521:Hanuman</param-value> </init-param> <init-param> <param-name>tab</param-name> <param-value>emp</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>abc</servlet-name> <url-pattern>/dbdata</url-pattern> </servlet-mapping> </web-app>