File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes form validation (stateform) and response.redirect() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "form validation (stateform) and response.redirect()" Watch "form validation (stateform) and response.redirect()" New topic
Author

form validation (stateform) and response.redirect()

Sana Ali
Greenhorn

Joined: Oct 08, 2004
Posts: 14
Hi, I have a form which takes input of memberID, LastName, FirstName.

Im using sessions and JavaBeans.

If memberID or LastName is not entered the user is to be prompted otherwise sent to the next form step.

parts of my code are:

<%@page import="java.io.*"%>
<%@page import="sana.member.MemberBean"%>

<jsp:useBean id="myMemberBean" class="sana.member.MemberBean" scope="session" />

<% myMemberBean.clear(); %>
<%!
//Define constants to represent states
public static final int DATA_ENTRY = 1;
public static final int MISSING_DATA = 2;
public static final int PROCEED_LOGIN = 3;

//Keep track of current state
int current_state;

String action = "",
id = "",
lastname = "",
firstname = "",
dateofbirth = "";
%>

<% action = request.getParameter("action");
id = request.getParameter("memberid");
lastname = request.getParameter("lastName");
firstname = request.getParameter("firstName");
dateofbirth = request.getParameter("dateofbirth");

// Figure out current state of system, and next permissible action

current_state = DATA_ENTRY; //default state

if (action != null && action.equals("PROCEED"))
{
current_state = PROCEED_LOGIN;

if (id == null || id.equals("") ||
lastname == null || lastname.equals(""))
{
current_state = MISSING_DATA;
}
}
%>

<html>....

<head>...</head>

<body>
<%
if (current_state == DATA_ENTRY || current_state == MISSING_DATA) {
%>
<table width="700" border="0" cellpadding="4" cellspacing="0">
<tr>
<th> <b>Member Details Input Step 1. . .</b></th>
</tr>

<%
if (current_state == MISSING_DATA) {
%>
<tr>
<td bgcolor="#FFFF66">
<p><strong>Fields Member ID and Last Name are mandatory</strong></p>
</td>
</tr>
<% } %>

</table>
<form method="post" action="memberinput2.jsp">
<input type="hidden" name="action" value="PROCEED" />

//Other code

<td><input type="submit" name="submit1" value=" Proceed to Step 2 "></td>
</tr>
</table>
</form>

<% } else if(current_state == PROCEED_LOGIN) {
response.sendRedirect("action");
%>

<% } else {
// Unknown state - should not occuru
%>

<table width="500" border=0
cellpadding=4 cellspacing=0>
<tr>
<td bgcolor="lightblue"><font color="white" face="Arial"><b>A system error has occurred.</b></font></td>
</tr>
</table>

<% } %>
//Other code

</body></html>

However, instead of the page getting reloaded and prompting the user to enter the missing data, the user is sent to the next page. :'(
Does any1 have a clue here?
Please help.
Sana
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: form validation (stateform) and response.redirect()
 
Similar Threads
blank page returned from validate
JSP form to javabean
Display results with next and previous buttons
javascript error: object expected
When Validation Error I Lose My Bean Value-Struts 2