?
Solved

How to add to .properties file for a servlet?

Posted on 2002-06-18
11
Medium Priority
?
175 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Most folks would know the basics of how Dropbox works, so that’s not the purpose of this article. Security is what it’s all about, so here I’ll share how I choose to secure my Dropbox Account and the Data it contains.
Social messanging services like WhatsApp and Facebook can help businesses in ways that many owners don't even imagine, giving new opportunities to connect with customers. Discover some of the most innovative things they can do for your company.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

864 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