Programming Tutorials

Example Using Initialization Parameters

By: aathishankaran in JSP Tutorials on 2007-02-14  

This program shows a servlet that reads the message and repeats initialization parameters when initialized. It shows the result when message is "Java Samples", repeats is 5, and the servlet is registered under the name ShowMsg. Remember that, although servlets read init parameters in a standard way, developers set init parameters in a server-specific manner. Please refer to your server documentation for authoritative details. The second program shows the configuration file used with Tomcat to obtain the result, the third program shows the configuration file used with the JSWDK, and it show how to set the parameters interactively with the Java Web Server. The result is identical with all three cases. Because the process of setting init parameters is server-specific, it is a good idea to minimize the number of separate initialization entries that have to be specified. This will limit the work you need to do when moving servlets that use init parameters from one server to another. If you need to read a large amount of data, I recommend that the init parameter itself merely give the location of a parameter file, and that the real data go in that file. An example of this approach is given in article (Restricting Access to Web Pages), where the initialization parameter specifies nothing more than the location of the password file.

package coreservlets;
import javax.servlet.*;
import javax.servlet.http.*;
public class ShowMessage extends HttpServlet {

  private String message;
  private String defaultMessage = "No message.";
  private int repeats = 1;
  public void init(ServletConfig config)
	throws ServletException {
	  // Always call super.init
	  message = config.getInitParameter("message");
	  if (message == null) {
	    message = defaultMessage;
	  try {
	    String repeatString = config.getInitParameter("repeats");
		repeats = Integer.parseInt(repeatString);
	  } catch(NumberFormatException nfe) {

	public void doGet(HttpServletRequest request,
		HttpServletResponse response)
		throws ServletException, IOException {
		  PrintWriter out = response.getWriter();
		  String title = "The ShowMessage Servlet";
		  out.println(ServletUtilities.headWithTitle(title) +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1 ALIGN=CENTER>" + title + "</H1>");

for(int i=0; i<repeats; i++) {
out.println(message + "<BR>");
out.println("</BODY></HTML>"); } }

This second program shows the setup file used to supply initialization parameters to servlets used with Tomcat. The idea is that you first associate a name with the servlet class file, and then associate initialization parameters with that name (not with the actual class file). The setup file is located in install_dir/webpages/WEB-INF.

web.xml (for Tomcat)

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" ""></br/> <web-app>
<param-name> message </param-name>
<param-value>Java Samples</param-value>

Add Comment

* Required information


No comments yet. Be the first!

Most Viewed Articles (in JSP )

Latest Articles (in JSP)