Kcpl
asked on
New to servlets and properties files!!!
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.propertie s";
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("s erver");
}
How do I get the info from the C:\\JAVA\\mypet.properties and store it in the web.xml file?
Properties props = new Properties();
props.put("server", "100.00.00.00");
String propFile = "C:\\JAVA\\mypet.propertie
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("s
}
How do I get the info from the C:\\JAVA\\mypet.properties
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");
}
ASKER
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.
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.
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().getR esourceAsS tream("/my Pet.proper ties")); //in classes folder
//props.load(application.g etResource AsStream(" /myPet.pro perties")) ; // in root folder
}catch(Exception e){System.out.print("can't get pass props");}
application.setAttribute(" server",pr ops.getPro perty("ser ver")) ;
}
you could use
ServletContext application = getServletContext();
public void init() throws ServletException{
Properties props = new Properties();
try{
props.load(getClass().getR
//props.load(application.g
}catch(Exception e){System.out.print("can't
application.setAttribute("
}
To access in a JSP, you could use
${server}
or
application.getAttribute(" server");
or in a servlet
getServletContext.getAttri bute("serv er");
${server}
or
application.getAttribute("
or in a servlet
getServletContext.getAttri
ASKER
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.getAttri bute("serv er");
//file = config.getInitParameter("s erver");
file = cont.getInitParameter("ser ver");
}
public void init(ServletConfig config)
throws ServletException
{
super.init(config);
ServletContext cont = new ServletContext();
cont = getServletContext.getAttri
//file = config.getInitParameter("s
file = cont.getInitParameter("ser
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.