Servlet Form validation
620837Sep 19 2008 — edited Sep 22 2008Here is where I am very fuzzy with Servlets. I am trying to do some server side form validation and pass any errors that were found back to the browser. Now, I have written a simple validation class that is called when a user changes his/her password. It works, however, it does not throw any errors tot he front end. Tomcat quietly swallows them and the page returns the user to the same page they just came from (if there was an error). How can I pass the errors that happen to the front end so the user can see what the problems were. That is my biggest problem with the software that I have written is. I cannot figure out how to pass errors from the back end (servlet) to the front end so the user can correct their problems, OR I cannot get custom error messages working with Tomcat.
I changed my web.xml file to include an error section:
<!--<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/Errors/Error.jsp</location>
</error-page>
<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/Errors/Error.jsp</location>
</error-page>-->
and when an error was thrown, it went to a blank page, but I don't know what to do from there (so I commented it out)
I have searched the web on using servlets to validate forms and I found a (what I think) is a really good article from Sun, but no real time examples that I could look at to maybe try to model my servlets off of. Can anyone explain how to throw errors from back to front(servlet to HTML)? As always thanks in advance!
Note:
Below I have included the validPwdChange class.
- Josh
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
public class ChangePasswordValidator{
private String errorMessages = "";
public String getMessages (){
return errorMessages;
}
public boolean validPwdChange(HttpServletRequest r) throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException{
validateOldPwd(r);
validatePwd(r);
validateNewPwd(r);
if (errorMessages.length() > 0){
return false; // validation failed
}
return true; // validation passed
}
private void validatePwd(HttpServletRequest r){
if (r.getParameter("password").equals(null) || r.getParameter("password").trim().length() == 0){
errorMessages += "Password cannot be empty.\n ";
}
else if (r.getParameter("password").length() < 6){
errorMessages += "Password must be at least 6 characters.\n ";
}
else if (r.getParameter("password").length() > 12){
errorMessages += "Password must be less than 12 characters.\n ";
}
}
private void validateOldPwd(HttpServletRequest r) throws SQLException, IllegalAccessException, InstantiationException, ClassNotFoundException{
final String extractedPassword = GeneralFunctions.getSingleValueFromDbByName(r.getParameter("tablename"), r.getRemoteUser(), "password");
if (r.getParameter("currentPassword").equals(null) || r.getParameter("currentPassword").equals("")){
errorMessages += "Current password cannot be blank.\n ";
}
else if (!r.getParameter("currentPassword").equals(extractedPassword)){
errorMessages += "Enter your valid current password.\n ";
}
}
private void validateNewPwd(HttpServletRequest r){
if (r.getParameter("password").equals(null) || r.getParameter("password").equals("")){
errorMessages += "New password cannot be blank.\n ";
}
else if (r.getParameter("confirmPassword").equals(null)){
errorMessages += "Confirm password cannot be blank.\n ";
}
else if (!r.getParameter("password").equalsIgnoreCase(r.getParameter("confirmPassword"))){
errorMessages += "New passwords do not match.\n ";
}
else if (r.getParameter("currentPassword").equalsIgnoreCase(r.getParameter("password"))){
errorMessages += "New password cannot be the same as current password.\n ";
}
}
}
Edited by: TheWhiteKnight on Sep 19, 2008 6:52 PM