Home arrow Java & J2EE arrow Page 7 - Overview of Java Web Technologies, Part 1

Retrieving Context Parameters - Java

Today, Budi walks us through a refresher and brief overview of server JSP programming. Today's portion covers Servlet technologies, including servlets and Tomcat. This excerpt comes from chapter one of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983-7, 2004).

  1. Overview of Java Web Technologies, Part 1
  2. Methods
  3. Creating a Servlet Directory Structure
  4. Reviewing Deployment Descriptors
  5. Servlet Mapping
  6. Defining Context Parameters
  7. Retrieving Context Parameters
  8. Listening to Application Events
  9. Packaging and Deploying a Web Application
By: McGraw-Hill/Osborne
Rating: starstarstarstarstar / 25
March 01, 2004

print this article



To retrieve a context parameter name and value defined in the deployment descriptor, you first need to obtain the ServletContext object using the getServletContext method of the ServletConfig object. You then call two methods of the ServletContext interface:

  • The getInitParameterNames method, which does not take an argument and returns a java.util.Enumeration containing all the context parameter names.

  • The getInitParameter method, which takes a String argument containing the parameter name and returns a String containing the value of the parameter.

The code in Listing 3 is an init method of a servlet called ContextParamDemoServlet. It loops through the Enumeration object called parameters, which is returned from the getInitParameterNames method. For each parameter, it outputs the parameter name and value. The parameter value is retrieved using the getInitParameter

Listing 3 Retrieving Context Parameters

public void init(ServletConfig config) throws ServletException {
  ServletContext servletContext = servletConfig.getServletContext();
Enumeration parameters = servletContext.getInitParameterNames();
  while (parameters.hasMoreElements()) {
    String parameter = (String) parameters.nextElement();
    System.out.println("Parameter name : " + parameter);
    System.out.println("Parameter value : " +

The output of the code in the console is as follows:

Parameter name : userName
Parameter value : budi
Parameter name : password
Parameter value : secret

Buy this book now!Remember: This is part one of the first chapter of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983). Stay tuned for part 2 of "Overviews of Java Web Technologies," where we learn about JSP, JavaBeans, and Model 2. 
Buy this book!

>>> More Java & J2EE Articles          >>> More By McGraw-Hill/Osborne

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: