|
|
abukta
super admin
profile |
|
Join Date: 29 Mar 2005 21:03
Posts: |
|
|
Tips from J2EE Servlets & JSP class |
1. For passwords, to reading to and writing from the password.properties, just use the java.util.Properties class, which is essentially a HashTable with the added capability of loading values from a properties file, and also storing values back to the properties-file using the load() and store() methods respectively. I am mentioning the relevant code-sections, in case you are stick working on those parts:
To load the password.properties file:
--------------------------------------------------------------------------------
| Code: | //Below, filepath is the full path string to the password-file.
this.passwords = new Properties ();
try {
FileInputStream fis = new FileInputStream (filepath);
this.passwords.load(fis);
fis.close();
}
catch (IOException exception) {
//
throw new RuntimeException ("Password file not found, or could not be read!", exception);
} |
--------------------------------------------------------------------------------
To store the passwords back to the password.properties file:
--------------------------------------------------------------------------------
| Code: | try {
FileOutputStream fos = new FileOutputStream(filepath);
this.passwords.store(fos, <table width='90%' cellspacing=1 align='center' cellpadding=4><tr><td class='small'><b>Quote:</b></td></tr><tr><td class='quote'>Username/Password pairs</td></tr></table>);
}
catch (IOException exception) {
throw new RuntimeException("Password file not found, or could not be written to!",
exception);
}
|
--------------------------------------------------------------------------------
2. You don not need to know regular-expressions to write the validator! The class java.lang.Character has simple methods to test whether a character is a digit, and if it is aletter. (Character.isDigit(char c) and Character.isLetter(char c) ). So for example to verify that a string has only alphabetical characters, the following code would help:
--------------------------------------------------------------------------------
| Code: | public boolean isAlphabeticalOnly (String string) {
char[] cbuf = string.toCharArray();
for (int i = 0; i < cbuf.length; i++) {
if (!Character.isLetter(cbuf[i]))
return false;
}
return true;
}
|
--------------------------------------------------------------------------------
Likewise to test that a string has both alphabetical and digit characters, and also some special characters, I was expecting simply that you would re-use the AlphanumericValidator, to write something like:
-------------------------------------------------------------------------
| Code: | // Three conditions must be fulfilled by the password-string:
// 1) Is not alphanumeric only, i.e. contains some special character.
// 2) Contains atleast one letter
// 3) Contains atleast one digit.
String string = (String) value;
char[] cbuf = string.toCharArray();
AlphanumericStringValidator alphanumericValidator = new
AlphanumericStringValidator();
boolean hasSpecialCharacter = ! alphanumericValidator.doValidate(string);
boolean containsDigit = false;
boolean containsLetter = false;
for (int i = 0; i < cbuf.length; i++) {
if (Character.isDigit(cbuf[i]))
containsDigit = true;
if (Character.isLetter(cbuf[i]))
containsLetter = true;
}
if (hasSpecialCharacter && containsDigit && containsLetter) {
//valid password...
}
else {
//invalid password
} |
message edited by abukta (19 Jan 2006 23:07)
|