Servlet initialization parameters

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.

package itso.servjsp.servletapi;
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 {
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 {
PrintWriter out = res.getWriter();
out.println("<TITLE>Date Display</TITLE>");
out.println("<H2>Servlet Initialization Parameters (ServletConfig):
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>");

Best Online Tutorials | Source codes | Programming Languages: Servlet initialization parameters
Servlet initialization parameters
Best Online Tutorials | Source codes | Programming Languages
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content