Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlets and properties file

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends,
Follwing is a code for a servlet which tries to
add a property to a properties file placed in the web server. I am testing
this out in Java web server and has placed the properties file
in public_html directory and it doesn't work (of course, I can simply write a value to the file
using an outputstream, but i wanted to know why it doesn't work? it works if i use the code as
a standalone application)
Any suggestions how to make this thing work?
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
FileOutputStream fw=null;
response.setContentType("text/html");
//get a handle to the output stream
PrintWriter out = response.getWriter();

try
{

Properties propTopics = new Properties();
String s=getServletContext().getRealPath("/testing.properties");

fw = new FileOutputStream(s, true); //get output stream to file
propTopics.setProperty("easter", "JSP vs servlets");
propTopics.store(fw,"no comments");

out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Just testing</TITLE>");
out.println("</HEAD>");
out.println("<BODY>done</body></html>");

}
catch (Exception e)
{
System.out.println("exception!");
//send stack trace back to client
// e.printStackTrace();
}
finally
{
try
{
//close file stream
fw.close();
out.close();
}
catch (Exception e) {}

}
}
}
 
Cynthia Yao
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I usually put property file with all other class file instead of public_html. You should print out string s in u code to see where it is looking at
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic