?
Solved

How to add to .properties file for a servlet?

Posted on 2002-06-18
11
Medium Priority
?
172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Accepted Solution

by:
cheekycj earned 200 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
The top devops trends for 2017 are focused on improved deployment frequency, decreased lead time for change and decreased MTTR.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

801 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