Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

urgent-problem in accessing initialisation parameters

 
preeti dengri
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
this is an example from Marty Hall's core servlets and java server pages for restricted access to web pages(listing 4.3).there is a helper class for making a simple java properties file containing usernames and passwords
import java.util.*;
import java.io.*;
public class passwordbuilder
{
public static void main(String args[]) throws Exception
{
Properties passwords =new Properties();
passwords.put("marty","martypw");
passwords.put("bj","bjpw");

String passwordfile="c:\\javawebserver2.0\\data\\passwords.properties";
FileOutputStream out=new FileOutputStream(passwordfile);
passwords.save(out,"Passwords");
}
}
when i am using tomcat , i gave the String passwordfile to be
String passwordfile="c:\\tomcat\\webapps\\webclient\\passwords.txt";
i tried it with passwords.properties in the String ,but my servlet is not working
the steps i took is:
1) created passwordbuilder.java
2)made necessary changes to web.xml as follows
<web-app>
<servlet>
<servlet-name>
protectedpage
</servlet-name>
<servlet-class>
protectedpage
</servlet-class>
<init-param>
<param-name>passwordfile/param-name>
<param-value>passwords.txt</param-value>
</init-param>
</servlet>


</web-app>
2)created protectedpage.java which access the init parameter "passwordfile" from web.xml file.
public void init(ServletConfig config) throws ServletException
{
super.init(config);
try
{
passwordfile=config.getInitParameter("passwordfile");
passwords=new Properties();
passwords.load(new FileInputStream(passwordfile));
}
catch(IOException e){}
}
please suggest the changes to be made.
thank you
preeti
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you will have to provide a complete path in:
<param-name>passwordfile/param-name>
<param-value>passwords.txt</param-value> <-- change this
The form you used for accessing the properties file:
passwordfile=config.getInitParameter("passwordfile");
passwords=new Properties();
passwords.load(new FileInputStream(passwordfile));
requires that the current directory be the one that has password.txt, but you can't rely on the current directory in a servlet situation - you must give a complete path.
Bill

------------------
author of:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic