hi i have validated some fiels in jsp using useBean . But am nt able to get the values into another page.can anyone help me.This is my code.
jsp file
<%-- Instantiate the form validation bean and supply the error message map --%>
<%@ page import="com.mycompany.*" %>
<jsp:useBean id="form" class="com.mycompany.MyForm" scope="request">
<jsp:setProperty name="form" property="errorMessages" value='<%= errorMap %>'/>
</jsp:useBean>
<%
// If process is true, attempt to validate and process the form
if ("true".equals(request.getParameter("process"))) {
%>
<jsp:setProperty name="form" property="*" />
<%
// Attempt to process the form
if (form.process()) {
// Go to success page
response.sendRedirect("formDone.jsp");
return;
}
}
%>
<html>
<head><title>A Simple Form</title></head>
<body>
<%-- When submitting the form, resubmit to this page --%>
<form action='<%= request.getRequestURI() %>' method="POST">
<%-- email --%>
<font color=red><%= form.getErrorMessage("email") %></font><br>
Email: <input type="TEXT" name="email" value='<%= form.getEmail() %>'>
<br>
<%-- zipcode --%>
<font color=red><%= form.getErrorMessage("zipcode") %></font><br>
Zipcode: <input type="TEXT" name="zipcode" value='<%= form.getZipcode() %>'>
<br>
<input type="SUBMIT" value="OK">
<input type="HIDDEN" name="process" value="true">
</form>
</body>
</html>
<%!
// Define error messages
java.util.Map errorMap = new java.util.HashMap();
public void jspInit() {
errorMap.put(MyForm.ERR_EMAIL_ENTER, "Please enter an email address");
errorMap.put(MyForm.ERR_EMAIL_INVALID, "The email address is not valid");
errorMap.put(MyForm.ERR_ZIPCODE_ENTER, "Please enter a zipcode");
errorMap.put(MyForm.ERR_ZIPCODE_INVALID, "The zipcode must be 5 digits");
errorMap.put(MyForm.ERR_ZIPCODE_NUM_ONLY, "The zipcode must contain only digits");
}
%>
this is my java class
package com.mycompany;
import java.util.*;
public class MyForm {
/* The properties */
String email = "";
String zipcode = "";
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email.trim();
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode.trim();
}
/* Errors */
public static final Integer ERR_EMAIL_ENTER = new Integer(1);
public static final Integer ERR_EMAIL_INVALID = new Integer(2);
public static final Integer ERR_ZIPCODE_ENTER = new Integer(3);
public static final Integer ERR_ZIPCODE_INVALID = new Integer(4);
public static final Integer ERR_ZIPCODE_NUM_ONLY = new Integer(5);
// Holds error messages for the properties
Map errorCodes = new HashMap();
// Maps error codes to textual messages.
// This map must be supplied by the object that instantiated this bean.
Map msgMap;
public void setErrorMessages(Map msgMap) {
this.msgMap = msgMap;
}
public String getErrorMessage(String propName) {
Integer code = (Integer)(errorCodes.get(propName));
if (code == null) {
return "";
} else if (msgMap != null) {
String msg = (String)msgMap.get(code);
if (msg != null) {
return msg;
}
}
return "Error";
}
/* Form validation and processing */
public boolean isValid() {
// Clear all errors
errorCodes.clear();
// Validate email
if (email.length() == 0) {
errorCodes.put("email", ERR_EMAIL_ENTER);
} else if (!email.matches(".+@.+\\..+")) {
errorCodes.put("email", ERR_EMAIL_INVALID);
}
// Validate zipcode
if (zipcode.length() == 0) {
errorCodes.put("zipcode", ERR_ZIPCODE_ENTER);
} else if (zipcode.length() != 5) {
errorCodes.put("zipcode", ERR_ZIPCODE_INVALID);
} else {
try {
int i = Integer.parseInt(zipcode);
} catch (NumberFormatException e) {
errorCodes.put("zipcode", ERR_ZIPCODE_NUM_ONLY);
}
}
// If no errors, form is valid
return errorCodes.size() == 0;
}
public boolean process() {
if (!isValid()) {
return false;
}
// Process form...
// Clear the form
email = "";
zipcode = "";
errorCodes.clear();
return true;
}
}
am not able to get values in formDone.jsp page.plz help me