The SimpleInitServlet servlet shows how to retrieve initialization parameters from the servlet configuration object ( See Example ). Serv...
The SimpleInitServlet servlet shows how to retrieve initialization parameters from the servlet configuration object (See Example).
ServletConfig object
The ServletConfig object is a parameter that can be passed into the init method of the servlet. You can also get the ServletConfig object from the request object through the getServletConfig method, but it is most commonly used in the init method to initialize the servlet’s instance variables.
Methods of the ServletConfig object allow us to extract the parameter information from this object. This parameter information is in a name/value pairs format, and can be stored in a file in XML format. We do not have to read the file, however, because the methods of the class provide us with some handy helper methods.
What this servlet does
This servlet simply extracts the parameter information from the configuration file, and stores those values in instance variables. It then echoes this information back to the client that invoked the servlet. In a real-life application, these variables would most likely be used to make a connection to the database, and this connection would be stored in a global instance variable for later use in the doGet method.
Example
package itso.servjsp.servletapi;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SimpleInitServlet extends HttpServlet {
protected String mydriver;
protected String myurl;
protected String myuserID;
protected String mypassword;
public void init(ServletConfig config) throws ServletException {
super.init(config);
mydriver = config.getInitParameter("driver");
myurl = config.getInitParameter("URL");
myuserID = config.getInitParameter("userID");
mypassword = config.getInitParameter("password");
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("TEXT/HTML");
PrintWriter out = res.getWriter();
out.println("<HTML>");
out.println("<TITLE>Date Display</TITLE>");
out.println("<BODY>");
out.println("<H2>Servlet Initialization Parameters (ServletConfig):
</H2><HR>");
out.println("<B>driver: </B>" + mydriver + "</BR>");
out.println("<B>url: </B>" + myurl + "</BR>");
out.println("<B>password: </B>" + mypassword + "</BR>");
out.println("<B>userID: </B>" + myuserID + "</BR>");
out.println("</BODY></HTML>");
out.close();
}
}
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SimpleInitServlet extends HttpServlet {
protected String mydriver;
protected String myurl;
protected String myuserID;
protected String mypassword;
public void init(ServletConfig config) throws ServletException {
super.init(config);
mydriver = config.getInitParameter("driver");
myurl = config.getInitParameter("URL");
myuserID = config.getInitParameter("userID");
mypassword = config.getInitParameter("password");
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("TEXT/HTML");
PrintWriter out = res.getWriter();
out.println("<HTML>");
out.println("<TITLE>Date Display</TITLE>");
out.println("<BODY>");
out.println("<H2>Servlet Initialization Parameters (ServletConfig):
</H2><HR>");
out.println("<B>driver: </B>" + mydriver + "</BR>");
out.println("<B>url: </B>" + myurl + "</BR>");
out.println("<B>password: </B>" + mypassword + "</BR>");
out.println("<B>userID: </B>" + myuserID + "</BR>");
out.println("</BODY></HTML>");
out.close();
}
}