Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error messages are not being displayed in jsp

Status
Not open for further replies.

gwu

MIS
Dec 18, 2002
239
US
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:

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);
  }

}
 
Perhaps not specific to your problem, but there are two problems immediately visiable :

- In this line :

Code:
        try{
            Context ctx = new InitialContext();
        }
... this will not set your class variable "ctx" - so it will remain null.

- In this line :
Code:
String errorMsg =(String)errors.get(s.trim());
... if "s" is null, your will get a NullPointerException.


--------------------------------------------------
Free Database Connection Pooling Software
 
I am not even useing this code so i took it out:
Code:
 try{
            Context ctx = new InitialContext();
        }

I changed the jsp page to look like this instead. I used jstl. For our purpouses i got rid of the error msg code. but i still am not getting any output in the <c:eek:ut> code.

Code:
<jsp:include page="/header.jsp" />
<%@ taglib prefix="c" uri="[URL unfurl="true"]http://java.sun.com/jstl/core"%>[/URL]
<c:set var="formToValidate" value="${formToValidate}"/>
<table>
	<tr>
		<td><strong>Add Member</strong></td>
	</tr>
	<tr>
		<td>&nbsp;</td>
	</tr>
</table>
<form action="/MemberServlet" 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>
<jsp:include page="/footer.jsp" />
 
I don't think you can have nested tag:

<input type="text" name="firstName" value="<c:eek:ut value="${formToValidate.firstName}"/>">

can you?
 
i am not sure..how else would i do it?

further more how would i display the return of a function in a c out tag. For example, when i do this, i get an error:

<c:eek:ut value="${formToValidate.getErrorMsg("firstName"}"/>

thanks

 
Code from your first post works with minor fix. In the MemberServlet.java, the attribute name for the MemberValidateForm object must match the id of the jsp:bean inteneded to be used for the form. i.e.

Code:
        if(allOk){
            toPage = "/MemberAdd";
        }else{
            req.setAttribute("[COLOR=red]formHandler[/color]",formToValidate);
        }
 
One more things. The field value in the MemberValidateForm are never initialized to the submitted values, that's why the form always show empty values. I would initialize the field value in the validate mehtod, something like:

Code:
public boolean validate(String firstName,
                        String lastName,
                        String emailAddress,
                        String homePhone) {
[COLOR=red]
    this.firstName = firstName;
    this.lastName = lastName;
    this.emailAddress = emailAddress;
    this.homePhone = homePhone; 
[/color]
    boolean allOk=true;
    if (firstName.equals("")) {
      .......
      ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top