Example Using Initialization Parameters

By: aathishankaran Emailed: 1644 times Printed: 2111 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.

ShowMessage.java

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

super.init(config);

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 {

response.setContentType("text/html");

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

}

out.println("</BODY></HTML>");

}

}

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"

"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">

<web-app>

<servlet>

<servlet-name>

ShowMsg

</servlet-name>

<servlet-class>

coreservlets.ShowMessage

</servlet-class>

<init-param>

<param-name>

message

</param-name>

<param-value>

Shibboleth

</param-value>

</init-param>

<init-param>

<param-name>

repeats

</param-name>

<param-value>

5

</param-value>

</init-param>

</servlet>

</web-app>


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

Comments(0)


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
JSP Program for display Date
What are the different scopes in JSP?
Sending Email using JSP
Uploading an Image to a Database using JSP
JSP Alert Example
Writing your first JSP page
JSP CheckBox Example
Example Using Initialization Parameters
Basic Servlet Structure
Arithmetic Evaluation Using the Expression Language in JSP
Tags using in jsp
Enable/Disable Scripting Elements in JSP
The Request Object in JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
Most Emailed Articles (in JSP)
Tag libraries
What is JSP?
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
What is JSP?
Click to Activate and Use this control
Tags using in jsp
Introduction to JSP expression language
Disabling Scriptlets in JSP using web.xml
Cookies using JSP or Java Bean
Closing Windows
Frame Object
Retrieving a Portion of a String
Getting HTTP Request Headers in a JSP
Syntax For JSP Declaratives
JSP Program for display Date