Solved

How to add to .properties file for a servlet?

Posted on 2002-06-18
11
168 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The business world is becoming increasingly integrated with tech. It’s not just for a select few anymore — but what about if you have a small business? It may be easier than you think to integrate technology into your small business, and it’s likely…
Starting your own business is always a daunting process, and for most people it is brand new experience. Avoid the common pitfalls by following these tips to start on the road to success.

790 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