我终于找到问题所在了1、首先将程序放在一下没有空格的目录中(不能放在如Program Files中)
例如在D盘的根目录下新建目录:myregister.
在 server.xml中加入:
</Context> <Context path="/myregister"
docBase="d:\myregister\"
crossContext="false"
debug="0"
reloadable="true" > <Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_dbtest_log." suffix=".txt"
timestamp="true"/> </Context> 2、找到UserDirectory.java中下面代码:
public void setUser(String userId, String password) throws
UserDirectoryException { // no nulls
if ((null==userId) || (null==password)) {
System.out.print("userid is null");
throw new UserDirectoryException();
}
try { // conform userId to uppercase when stored
p.put(fixId(userId), password);
String o = this.getClass().getClassLoader().getResource(UserDirectoryFile).getFile();
p.store(new FileOutputStream(o), UserDirectoryHeader);
} catch (IOException e) {
throw new UserDirectoryException(); }
}
修改为:
public void setUser(String userId, String password) throws
UserDirectoryException { // no nulls
if ((null==userId) || (null==password)) {
System.out.print("userid is null");
throw new UserDirectoryException();
}
try { // conform userId to uppercase when stored
p.put(fixId(userId), password);
String o = this.getClass().getClassLoader().getResource(UserDirectoryFile).getFile();
System.out.println(o); //o is /D:/myregister/WEB-INF/classes/resources/users.properties
String ostr=o.substring(1); // the line must be add is to use to remove the "/" in String o
System.out.println(ostr); //ostr is D:/myregister/WEB-INF/classes/resources/users.properties
p.store(new FileOutputStream(ostr), UserDirectoryHeader);//modify o as ostr
} catch (IOException e) {
throw new UserDirectoryException(); }
}注意最关键的是将:this.getClass().getClassLoader().getResource(UserDirectoryFile).getFile();所获得的字符串进行处理因为它前面多了一个"/"。需要去掉。
例如在D盘的根目录下新建目录:myregister.
在 server.xml中加入:
</Context> <Context path="/myregister"
docBase="d:\myregister\"
crossContext="false"
debug="0"
reloadable="true" > <Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_dbtest_log." suffix=".txt"
timestamp="true"/> </Context> 2、找到UserDirectory.java中下面代码:
public void setUser(String userId, String password) throws
UserDirectoryException { // no nulls
if ((null==userId) || (null==password)) {
System.out.print("userid is null");
throw new UserDirectoryException();
}
try { // conform userId to uppercase when stored
p.put(fixId(userId), password);
String o = this.getClass().getClassLoader().getResource(UserDirectoryFile).getFile();
p.store(new FileOutputStream(o), UserDirectoryHeader);
} catch (IOException e) {
throw new UserDirectoryException(); }
}
修改为:
public void setUser(String userId, String password) throws
UserDirectoryException { // no nulls
if ((null==userId) || (null==password)) {
System.out.print("userid is null");
throw new UserDirectoryException();
}
try { // conform userId to uppercase when stored
p.put(fixId(userId), password);
String o = this.getClass().getClassLoader().getResource(UserDirectoryFile).getFile();
System.out.println(o); //o is /D:/myregister/WEB-INF/classes/resources/users.properties
String ostr=o.substring(1); // the line must be add is to use to remove the "/" in String o
System.out.println(ostr); //ostr is D:/myregister/WEB-INF/classes/resources/users.properties
p.store(new FileOutputStream(ostr), UserDirectoryHeader);//modify o as ostr
} catch (IOException e) {
throw new UserDirectoryException(); }
}注意最关键的是将:this.getClass().getClassLoader().getResource(UserDirectoryFile).getFile();所获得的字符串进行处理因为它前面多了一个"/"。需要去掉。
btw:这说明任何源码都不一定全都是对的哦,还是要自己动手才行。这样也能学到新东西。