Solved

How to add to .properties file for a servlet?

Posted on 2002-06-18
11
163 Views
Last Modified: 2010-04-01
I'm kind of new in writing the servlets, and I have a question, I'm using tomcat 4.0, and I want to add a default property of age=30; for my servlet. How do I add this to the .properties file? To which .properties file do I add this to?
Can someone give me a really detailed step by step procedure please?
thanks.
0
Comment
Question by:tomboman
11 Comments
 
LVL 92

Expert Comment

by:objects
ID: 7091733
You can add it to your servlet definition in web.xml:

<servlet>
  <servlet-name>MyServlet</servlet-name>
  <servlet-class>au.com.objects.MyServlet</servlet-class>
    <init-param>
      <param-name>age</param-name>
      <param-value>30</param-value>
    </init-param>
</servlet>
0
 
LVL 92

Expert Comment

by:objects
ID: 7091735
You can then access it from your servlet using the getInitParameter method.

int age = Integer.parseInt(getInitParameter("age"));
0
 
LVL 6

Expert Comment

by:kotan
ID: 7091808
Properties file method:

Let's say the properties file is name.properties which contain

age=30


Place that properties under  {Your webapps}/WEB-INF/classes/

In the servlet,

import java.util.ResourceBundle;


ResourceBundle rb = ResouceBundle.getBundle("name");
int age = Integer.parseInt(rb.getString("age"));
0
 
LVL 19

Accepted Solution

by:
cheekycj earned 50 total points
ID: 7092746
I use Properties API to read properties files not the resourcebundle.. here is the code for that (just an alternative):

using the same convention as above your properties file will contain:
age=30

    java.util.Properties properties = new java.util.Properties();
    try {
        // reading in properties file
        properties.load(new java.io.FileInputStream("yourpropertyfilename.properties"));
    } catch (IOException e) {
        // handle IO exception
    }
    // now you can fetch and retrieve any number of values
    int age;
    try {
      // retrieve age property (all properties are retrieved as string values
      String ageProp = properties.getProperty("age");
      if (ageProp != null) {
        // get integer value from the retrieved string
        age = Integer.parseInt(ageProp);
      }
      else {
        // print out that age property was not found/stored
      }
    } catch (NumberFormatException nfe) {
        // handle any number format exception in case age value is not an int
    }

HTH,
CJ
0
 

Author Comment

by:tomboman
ID: 7095568
Thanks. CJ. and others.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 19

Expert Comment

by:cheekycj
ID: 7096250
Glad I could help and Thanx for the "A".

CJ
0
 
LVL 92

Expert Comment

by:objects
ID: 7097490
Picked the least platform independant solution though.

0
 
LVL 19

Expert Comment

by:cheekycj
ID: 7098828
objects: why do you say that?

The Properties API works in Windows.  I have used it.

CJ
0
 
LVL 92

Expert Comment

by:objects
ID: 7099722
Becuase you need to specify the location of the the properties file on disk. If you move your webapp then this location may well change, and require a code change.
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 7100791
true.  But you can keep your prop file under the classes directory, you can include as a part of your deployment process.

I just find it easier to maintain a simple prop file that I can replace easily.

CJ
0
 
LVL 92

Expert Comment

by:objects
ID: 7101145
> But you can keep your prop file under the classes directory

If you install tomcat in a different directory, or move your webapp the the directory will change.

> I just find it easier to maintain a simple prop file that I can replace easily.

That's fine, just more portable to use the classloader to open it.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Owning a franchise can be the dream of a lifetime. It provides a chance for economic growth. You can be as successful as you want.  To make your franchise successful, you need to market it successfully. Here are six of the best marketing strategies …
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now