Example Using Initialization Parameters

By: aathishankaran Emailed: 1728 times Printed: 2331 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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





























JSP Home | All JSP Tutorials | Latest JSP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


Be the first one to add a comment

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by aathishankaran
Web Security Issues
The Web User's Perspective
Server-side plug-Ins
The best way to avoid security vulnerabilities with new server
JavaScript Security
Window Object
Working with Status Bar Messages
Retrieving a Portion of a String
Referencing Windows
Math Object
Frame Object
Document Object
Closing Windows
Built-in Object in Javascript
Textarea Object

More Tutorials in JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
JSP Alert Example
JSP CheckBox Example
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
A JSP page that gets properties from a bean
The page Directive in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Declarations in JSP
Scriptlets and Expressions in JSP
Tag Libraries in JSP
The Request Object in JSP
The Response Object in JSP
The Out Object in JSP
The Session Object in JSP

More Latest News
Most Viewed Articles (in JSP )
JSP Example to connect to MS SQL database and retrieve records
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
Automatically Refreshing a JSP
JSP CheckBox Example
JSP Alert Example
JSP Program for display Date
What are the different scopes in JSP?
Example Using Initialization Parameters
NotifyUtil::java.net.ConnectException: Connection refused: connect
Comparison operators in JSP
Sending Email using JSP
The Response Object in JSP
Embedding java codes in jsp sciptlets
JSP pages in servlet
Simple HTML-Building Utilities
Most Emailed Articles (in JSP)
Combining Scriptlets with HTML
Form processing in JSP
Protecting your website with a login page
The Servlet Life Cycle
Closing Windows
WebClient: Talking to Web Servers Interactively
Drawbacks of the JSP Model 1 Architecture
Select Object
Working with Status Bar Messages
NotifyUtil::java.net.ConnectException: Connection refused: connect
What are the different scopes in JSP?
Calling JSP from a batch file
Embedding a QuickTime movie in a JSP