Error messages and form values (formHandler) are not being displayed after post in jsp can anyone please advise? The System.out.println(formToValidate.getErrorMsg("lastName"));
thank a bunch!!!
memberAddForm.jsp:
MemberSerlvet:
MemberValidateForm.java:
thank a bunch!!!
memberAddForm.jsp:
Code:
<jsp:useBean id="formHandler" class="com.bdi.[URL unfurl="true"]www.members.MemberValidateForm"[/URL] scope="request">
<jsp:setProperty name="formHandler" property="*"/>
</jsp:useBean>
<form action="/MemberSerlvet" method="post">
<table>
<tr>
<td>First Name:</td>
<td>
<input type="text" name="firstName" value="<%=formHandler.getFirstName()%>">
<sub><font color="red">
<%=formHandler.getErrorMsg("firstName")%>
</font></sub>
</td>
</tr>
<tr>
<td>Last Name:</td>
<td>
<input type="text" name="lastName" value="<%=formHandler.getLastName()%>">
<sub><font color="red">
<%=formHandler.getErrorMsg("lastName")%>
</font></sub>
</td>
</tr>
<tr>
<td>Email Address:</td>
<td>
<input type="text" name="emailAddress" value="<%=formHandler.getEmailAddress()%>">
<sub><font color="red">
<%=formHandler.getErrorMsg("emailAddress")%>
</font></sub>
</td>
</tr>
<tr>
<td>Home Phone:</td>
<td>
<input type="text" name="homePhone" value="<%=formHandler.getHomePhone()%>">
<sub><font color="red">
<%=formHandler.getErrorMsg("homePhone")%>
</font></sub>
</td>
</tr>
<tr>
<td colspan=2><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
MemberSerlvet:
Code:
package com.bdi.[URL unfurl="true"]www.members;[/URL]
import javax.servlet.*;
import javax.naming.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class MemberSerlvet extends HttpServlet {
private ServletContext context;
private Context ctx;
public void init(ServletConfig config) throws ServletException {
super.init(config);
context = config.getServletContext();
try{
Context ctx = new InitialContext();
}
catch(Exception e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
String firstName = req.getParameter("firstName");
String lastName = req.getParameter("lastName");
String emailAddress = req.getParameter("emailAddress");
String homePhone = req.getParameter("homePhone");
String toPage = "/memberAddForm.jsp";
boolean allOk=true;
MemberValidateForm formToValidate = new MemberValidateForm();
allOk = formToValidate.validate(firstName,lastName,emailAddress,homePhone);
System.out.println(formToValidate.getErrorMsg("lastName"));
if(allOk){
toPage = "/MemberAdd";
}else{
req.setAttribute("formToValidate",formToValidate);
}
RequestDispatcher dispatcher;
dispatcher = context.getRequestDispatcher(toPage);
dispatcher.forward(req, res);
}
}
MemberValidateForm.java:
Code:
package com.bdi.[URL unfurl="true"]www.members;[/URL]
import java.util.*;
public class MemberValidateForm {
private String firstName;
private String lastName;
private String emailAddress;
private String homePhone;
private Hashtable errors;
public boolean validate(String firstName,String lastName,String emailAddress,String homePhone) {
boolean allOk=true;
if (firstName.equals("")) {
errors.put("firstName","Please enter your first name");
firstName="";
allOk=false;
}
if (lastName.equals("")) {
errors.put("lastName","Please enter your last name");
lastName="";
allOk=false;
}
if (emailAddress.equals("") || (emailAddress.indexOf('@') == -1)) {
errors.put("emailAddress","Please enter a valid email address");
emailAddress="";
allOk=false;
}
if (homePhone.equals("")) {
errors.put("homePhone","Please enter a valid home phone number");
homePhone="";
allOk=false;
}
return allOk;
}
public String getErrorMsg(String s) {
String errorMsg =(String)errors.get(s.trim());
return (errorMsg == null) ? "":errorMsg;
}
public MemberValidateForm() {
firstName="";
lastName="";
emailAddress="";
homePhone="";
errors = new Hashtable();
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmailAddress() {
return emailAddress;
}
public String getHomePhone() {
return homePhone;
}
public void setFirstName(String fname) {
firstName =fname;
}
public void setLastName(String lname) {
lastName =lname;
}
public void setEmailAddress(String eml) {
emailAddress=eml;
}
public void setHomePhone(String hp) {
homePhone=hp;
}
public void setErrors(String key, String msg) {
errors.put(key,msg);
}
}