?
Solved

New to servlets and properties files!!!

Posted on 2009-05-04
6
Medium Priority
?
579 Views
Last Modified: 2013-11-24
I have a jsp/servlet application.  I would like to set a value in a file called mypet.properties.  I create the file using
Properties props = new Properties();
props.put("server", "100.00.00.00");
String propFile = "C:\\JAVA\\mypet.properties";
FileOutputStream out = new FileOutputStream(propFile);
 props.store(out, "MyPet");
So know I have a file in C:\\JAVA\\ called mypet.properties with server=100.00.00.00
I want to be able to get the info from the mypet.properties file and be able to use it an a servelt  that I am reading the web.xml file  using
public void init(ServletConfig config)
 throws ServletException
{
   super.init(config);
    file = config.getInitParameter("server");
}

How do I get the info from the C:\\JAVA\\mypet.properties and store it in the web.xml file?
public class PropBuilder
{
  public static void main(String[] args) throws Exception
  {
    Properties props = new Properties();
    props.put("server", "100.00.00.00");
    String propFile = "C:\\JAVA\\mypet.properties";
    FileOutputStream out = new FileOutputStream(propFile);
    props.store(out, "MyPet");
  }
}
<web-app>
  <servlet>
    <servlet-name>Pet</servlet-name>
    <servlet-class>pet.PetServlet</servlet-class>
	<init-param>
	 <param-name>server</param-name>
      	 <param-value>value from mypet.properties</param-value>
    </init-param>
  </servlet>
</web-app>
 
public void init(ServletConfig config)
	      throws ServletException
	{
 
	    super.init(config);
  	    file = config.getInitParameter("server");
	}

Open in new window

0
Comment
Question by:Kcpl
[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
  • 4
  • 2
6 Comments
 
LVL 28

Expert Comment

by:rrz
ID: 24301087
>I want to be able to get the info from the mypet.properties file and be able to use it an a servelt  
Why not create a instance variable in the servlet and store it there ? That way you wouldn't have to mess around with the web.xml file.
Also why not put the prop file in your web app ? If you put it into your web app's WEB-INF/classes folder then you access it as a resource.
0
 

Author Comment

by:Kcpl
ID: 24301116
When I put the prop file in the web-inf/classes folder I get null pointer exception say it can't find the file.

I need to store the ip address in one place so when the server ip address changes, I don't have to change it all over the application.  I want to be able to put the ip address and only change it there.  I don't want to hard code it in the web.xml file.  This application is a RMI application, so I need to be able to access the file from the server and also from the web application.  So I will already have to change the ip address in two locations.
0
 
LVL 28

Expert Comment

by:rrz
ID: 24301204
In a servlet that loads on start-up (add <load-on-startup/>  to <servlet> tag in web.xml file),
you could use  
ServletContext application = getServletContext();
public void init() throws ServletException{
                           Properties props = new Properties();
                           try{
                                 props.load(getClass().getResourceAsStream("/myPet.properties")); //in classes folder
                                 //props.load(application.getResourceAsStream("/myPet.properties")); // in root folder
                          }catch(Exception e){System.out.print("can't get pass props");}
                      application.setAttribute("server",props.getProperty("server")) ;

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

Expert Comment

by:rrz
ID: 24301219
To access in a JSP,  you could use
${server}
or
application.getAttribute("server");
or in a servlet
getServletContext.getAttribute("server");  
0
 

Author Comment

by:Kcpl
ID: 24301351
Now do I do the following to get the info:

public void init(ServletConfig config)
     throws ServletException
{
       super.init(config);
       ServletContext cont = new ServletContext();
      cont = getServletContext.getAttribute("server");
     //file = config.getInitParameter("server");
     file = cont.getInitParameter("server");
}
0
 
LVL 28

Accepted Solution

by:
rrz earned 1000 total points
ID: 24301410
In my last post I showed how to access the value.  
In a servlet  use  
ServletContext application = getServletContext();
String  ipAddress = (String)application.getAttribute("server");
0

Featured Post

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!

Question has a verified solution.

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

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

771 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