I am trying to get this servlet /jsp pair working. the c
ut tags are not displaying anything. he problem lies with returning formToValidate with this statement:
req.setAttribute("formToValidate",formToValidate);
memberAddForm.jsp:
Code:
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<c:set var="formToValidate" value="${formToValidate}"/>
<table>
<tr>
<td>Add Member</td>
</tr>
<tr>
<td></td>
</tr>
</table>
<form action="/MemberSerlvet" method="post">
<table>
<tr>
<td>First Name:</td>
<td>
<input type="text" name="firstName" value="<c:out value="${formToValidate.firstName}"/>">
<sub><font color="red">
</font></sub>
</td>
</tr>
<tr>
<td>Last Name:</td>
<td>
<input type="text" name="lastName" value="<c:out value="${formToValidate.lastName}"/>">
<sub><font color="red">
</font></sub>
</td>
</tr>
<tr>
<td>Email Address:</td>
<td>
<input type="text" name="emailAddress" value="<c:out value="${formToValidate.emailAddress}"/>">
<sub><font color="red">
</font></sub>
</td>
</tr>
<tr>
<td>Home Phone:</td>
<td>
<input type="text" name="homePhone" value="<c:out value="${formToValidate.homePhone}"/>">
<sub><font color="red">
</font></sub>
</td>
</tr>
<tr>
<td colspan=2><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
MemberSerlvet:
Code:
package com.bdi.www.members;
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:
package com.bdi.www.members;
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);
}
}
Bookmarks