[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How to add to .properties file for a servlet?

Posted on 2002-06-18
11
Medium Priority
?
173 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Geo-targeting is the practice of distributing content based on a person’s location, as best as you can determine it. Let’s look at some ways you could successfully use this tactic. The following tips and case studies could lead to meaningful results.
Tech spooks aren't just for those who are tech savvy, it also happens to those of us running a business. Check out the top tech spooks for business owners.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

650 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