Example Using Initialization Parameters

By: aathishankaran  

Example Using Initialization Parameters

This program shows a servlet that reads the message and repeats initialization parameters when initialized. It shows the result when message is Shibboleth, 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 java.io.*;

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

This second program shows the setup file used to supply initialization parameters to servlets used with Tomcat 3.0. 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.

The third program shows the properties file used to supply initialization parameters to servlets in the JSWDK. As with Tomcat, you first associate a name with the servlet class, then associate the initialization parameters with the name. The properties file is located in install_dir/webpages/WEB-INF.

for(int i=0; i<repeats; i++) {

out.println(message + "<BR>");





web.xml (for Tomcat)

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app

PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application





























Archived Comments

1. Write a Java program to use JTextField Swing Control to display your name.
View Tutorial          By: Saad Kazi at 2016-05-05 02:41:08

Most Viewed Articles (in JSP )

Latest Articles (in JSP)

Comment on this tutorial