Prepopulating and Redisplaying Input Forms
Once again, we are modifying that large struts example.
We now add three new actions called update, update2, and update3. They are different versions of an action to update information about a user.
In struts-config.xml, we'll change this forward
<forward name="update"
path="/update.jsp"/>
when we wish to have the update go to a different version of the update page.
struts-config.xml
<?xml version="1.0" encoding="windows-1252" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="userFormBean"
type="strutsproject5.UserFormBean"/>
<form-bean name="updateFormBean"
type="strutsproject5.UpdateFormBean"/>
<form-bean
name="updateFormBean3" type="strutsproject5.UpdateFormBean3"/>
</form-beans>
<global-forwards>
<forward name="updatesuccess"
path="/updateSuccess.jsp"/>
</global-forwards>
<action-mappings>
<action path="/login"
type="strutsproject5.RegisterAction"
parameter="operation">
<forward name="emptyUserName" path="/emptyusername.jsp"/>
<forward name="emptyPassword" path="/emptypassword.jsp"/>
<forward name="tooManyMatches" path="/tomanymatches.jsp"/>
<forward name="illegalUsernamePassword" path="/illegalentry.jsp"/>
<forward name="notStudent" path="/notstudent.jsp"/>
<forward name="notUser" path="/notuser.jsp"/>
<forward name="drop"
path="/drop.jsp"/>
<forward name="see"
path="/see.jsp"/>
<forward name="register"
path="/register.jsp"/>
<forward name="update"
path="/update.jsp"/>
</action>
<action path="/update"
type="strutsproject5.UpdateRegisterAction"
name="updateFormBean" scope="request">
<forward
name="missing-value" path="/updateFailure.jsp"/>
</action>
<action path="/update2"
type="strutsproject5.UpdateRegisterAction2"
name="updateFormBean" scope="request">
<forward
name="missing-value" path="/update2.jsp"/>
</action>
<action path="/update3" type="strutsproject5.UpdateRegisterAction3"
name="updateFormBean3"
scope="request"
parameter="choice">
<forward
name="missing-value" path="/update3.jsp"/>
<forward name="updatesuccess"
path="/updateSuccess3.jsp"/>
</action>
<action path="/register"
type="strutsproject5.BeanRegisterAction"
name="userFormBean" scope="request">
<forward name="badCourse" path="/badcourse.jsp"/>
<forward name="badSection" path="/badsection.jsp"/>
<forward name="notExists" path="/notexists.jsp"/>
<forward name="success"
path="/confirmregistration.jsp"/>
</action>
<action path="/choice2"
type="strutsproject5.NextChoice"
parameter="choice">
<forward name="another"
path="/register.jsp"/>
<forward name="see"
path="/seeall.jsp"/>
</action>
<action path="/goAhead"
type="strutsproject5.FinalChoice"
parameter="choice">
<forward name="another"
path="/register.jsp"/>
<forward name="see" path="/seeall.jsp"/>
<forward name="complete"
path="/complete.jsp"/>
</action>
</action-mappings>
<message-resources parameter="MessageResources" null="false" />
</struts-config>
MessageResources.properties
form.title=Enter your login
form.userNamePrompt=<I>Username:</I>
form.passwordPrompt=<I>Password:</I>
form.actionPrompt=Action:
form.registerMessage=Register for Class
form.dropMessage=Drop a Class
form.seeGradesMessage=See Grades
form.updateInfoMessage=Update Information
form.submitPrompt=Log in
form.errorTitle=No {0}
form.tryAgain=<A HREF={0}>Try
again</A>.
form.errorMessage=You forgot to enter the
{0}.
form.someOKMessage={0} {1}
And login.jsp has that extra option.
login.jsp
<%@
taglib prefix="bean" uri="/WEB-INF/struts-bean.tld" %>
<HTML>
<HEAD><TITLE><bean:message key="form.title" /> </TITLE></HEAD>
<BODY>
<H1
align="center">
<bean:message
key="form.title" />
</H1>
<FORM
ACTION="login.do"
METHOD="POST">
<bean:message
key="form.userNamePrompt" />
<INPUT TYPE="TEXT"
NAME="username">
<BR> <bean:message key="form.passwordPrompt"
/>
<INPUT TYPE="PASSWORD"
NAME="password">
<BR><bean:message key="form.actionPrompt"
/>
<select name="operation">
<option value="register">
<bean:message key="form.registerMessage"
/>
</option>
<option value="drop">
<bean:message key="form.dropMessage"
/>
</option>
<option value="see">
<bean:message key="form.seeGradesMessage"
/>
</option>
<option value="update">
<bean:message key="form.updateInfoMessage"
/>
</option>
</select>
<BR> <INPUT TYPE="SUBMIT"
VALUE="<bean:message
key="form.submitPrompt" />">
</FORM>
</BODY>
</HTML>
RegisterAction.java
package strutsproject5;
import javax.servlet.http.*;
import org.apache.struts.action.*; // Action, ActionForm,
etc.
import org.apache.struts.actions.*; // DispatchAction
import java.sql.*;
public class RegisterAction extends DispatchAction
{
public ActionForward update
(ActionMapping
mapping,
ActionForm
form,
HttpServletRequest
request,
HttpServletResponse
response)
throws
Exception
{
String answer1 = findInputError(request);
if (!
answer1.equalsIgnoreCase("ok"))
return (mapping.findForward(answer1));
String answer2 = findDataError(request);
if (!
answer2.equalsIgnoreCase("ok"))
return (mapping.findForward(answer2));
return (mapping.findForward("update"));
}
public ActionForward register
(ActionMapping
mapping,
ActionForm
form,
HttpServletRequest
request,
HttpServletResponse
response)
throws
Exception
{
String answer1 = findInputError(request);
if (!
answer1.equalsIgnoreCase("ok"))
return (mapping.findForward(answer1));
String answer2 = findDataError(request);
System.err.println(answer2);
if (!
answer2.equalsIgnoreCase("ok"))
return (mapping.findForward(answer2));
return (mapping.findForward("register"));
}
public ActionForward drop
(ActionMapping
mapping,
ActionForm
form,
HttpServletRequest
request,
HttpServletResponse
response)
throws
Exception
{
String answer1 = findInputError(request);
if (!
answer1.equalsIgnoreCase("ok"))
return (mapping.findForward(answer1));
String answer2 = findDataError(request);
if (!
answer2.equalsIgnoreCase("ok"))
return (mapping.findForward(answer2));
return (mapping.findForward("drop"));
}
public ActionForward see
(ActionMapping
mapping,
ActionForm
form,
HttpServletRequest
request,
HttpServletResponse
response)
throws
Exception
{
String answer1 = findInputError(request);
if (!
answer1.equalsIgnoreCase("ok"))
return (mapping.findForward(answer1));
String answer2 = findDataError(request);
if (!
answer2.equalsIgnoreCase("ok"))
return (mapping.findForward(answer2));
return (mapping.findForward("see"));
}
private String findInputError(HttpServletRequest
request)
{
String username = request.getParameter("username");
if (username
== null || username.trim().equals(""))
{
return
("emptyUserName");
}
HttpSession
session = request.getSession(true);
RegistrationDataBean
data;
if (session.getAttribute("data")
== null)
data = new RegistrationDataBean();
else
data = (RegistrationDataBean)session.getAttribute("data");
data.setUsername(username);
session.setAttribute("data", data);
String password = request.getParameter("password");
if (password
== null || password.trim().equals(""))
{
return
("emptyPassword");
}
return
("OK");
}
private String findDataError(HttpServletRequest
request)
{
String username = request.getParameter("username");
String password = request.getParameter("password");
String url =
"jdbc:odbc:registrar";
Connection con;
String query =
"select
COUNT(*) as MATCHES from USERS where USERID like ? " +
"
and PASSWORD like ?;";
String query2 =
"select
COUNT(*) from USERROLES where ROLEID = 1 and " +
"
USERID like ?;";
String query3 =
"select
COUNT(*) as MATCHES from USERS where USERID like ? ";
String query4 ="select FNAME,
LNAME from USERS where USERID like ? ";
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("ClassNotFoundException:
");
System.err.println(e.getMessage());
}
try
{
PreparedStatement
stmt;
PreparedStatement
stmt2;
PreparedStatement
stmt3;
PreparedStatement
stmt4;
con = DriverManager.getConnection(url,
"", "");
stmt3 = con.prepareStatement(query3);
stmt3.setString(1,
username);
ResultSet
rs3 = stmt3.executeQuery();
rs3.next();
int matches = rs3.getInt(1);
if
(matches != 1)
return
("notUser");
stmt = con.prepareStatement(query);
stmt.setString(1, username);
stmt.setString(2, password);
ResultSet
rs = stmt.executeQuery();
rs.next();
int match_count = rs.getInt("MATCHES");
if (match_count > 1)
return
("tooManyMatches");
else if (match_count == 0)
return
("illegalUsernamePassword");
else
{
stmt2 = con.prepareStatement(query2);
stmt2.setString(1,
username);
ResultSet
rs2 = stmt2.executeQuery();
rs2.next();
matches
= rs2.getInt(1);
if
(matches != 1)
return
("notStudent");
}
stmt4 = con.prepareStatement(query4);
stmt4.setString(1,
username);
ResultSet
rs4 = stmt4.executeQuery();
rs4.next();
HttpSession
session = request.getSession(true);
RegistrationDataBean
data;
data = (RegistrationDataBean)session.getAttribute("data");
data.setFirstName(rs4.getString("FNAME"));
data.setLastName(rs4.getString("LNAME"));
}
catch(SQLException ex)
{
System.err.println("SQLException: " + ex.getMessage());
}
return
"ok";
}
}
update.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@
page contentType="text/html;charset=windows-1252"%>
<html>
<head>
<title>Update
Page</title>
</head>
<body>
<h1 align="center">Update
Your Information</h1>
<%@ taglib
prefix="html" uri="/WEB-INF/struts-html.tld" %>
<html:form
action="update">
Date of Birth: <html:text property="dob"/><BR>
Phone: <html:text property="phone"/><BR>
Major: <html:text property="major"/><BR>
Address: <html:text property="address"/><BR>
City: <html:text property="city"/><BR>
State: <html:text property="state"/><BR>
Zip: <html:text property="zip"/><BR>
<html:submit
value="Sign Me Up!"/>
</html:form>
</BODY>
</HTML>
<form-beans>
<form-bean name="userFormBean"
type="strutsproject5.UserFormBean"/>
<form-bean name="updateFormBean"
type="strutsproject5.UpdateFormBean"/>
<form-bean
name="updateFormBean3" type="strutsproject5.UpdateFormBean3"/>
</form-beans>
We'll use UpdateFormBean in a later version of this project, but for now we'll look at UpdateFormBean.
The <form-bean> tag will declare a bean of name updateFormBean, of type UpdateFormBean.
This bean is an extension of ActionForm, which we have used before. Remember that an ActionForm reads data from a form and processes it before proceeding to the Action.
This particular ActionForm has properties phone, dob, major, address, city, state, and zip. These properties match the names of the text fields in update.jsp.
These properties have default values.
We also have a defaultValues property (a String array) which stores these values.
Finally, we have a warning property which is initially null string.
We have the usual get and set methods.
We also have setWarning, getWarning, clearWarning, reset, and isMissing, which we'll use later.
UpdateFormBean.java
package strutsproject5;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
public class UpdateFormBean extends ActionForm
{
private String
phone = "ddd-ddd-dddd";
private String dob = "M/D/YYYY";
private String
major = "Your Major";
private String
address = "Your Address";
private String
city = "City";
private String
state = "SS";
private String zip
= "ddddd";
private String[] defaultValues =
{ phone, dob, major, address,
city, state, zip };
private String
warning = "";
public String getDob()
{
return dob;
}
public void setDob(String dob)
{
this.dob = dob;
}
public String getPhone()
{
return phone;
}
public void setPhone(String phone)
{
this.phone =
phone;
}
public void setMajor(String major)
{
this.major =
major;
}
public String getMajor()
{
return major;
}
public String getCity()
{
return city;
}
public void setCity(String city)
{
this.city =
city;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public void setState(String state)
{
this.state =
state;
}
public String getState()
{
return state;
}
public void setZip(String zip)
{
this.zip = zip;
}
public String getZip()
{
return zip;
}
public void setWarning(String baseWarning)
{
this.warning =
"<H2><FONT
COLOR=RED>Missing or invalid " +
baseWarning
+ "!</FONT></H2>";
}
public String getWarning()
{
return warning;
}
public void clearWarning()
{
warning =
"";
}
// The reset
method below is ONLY needed if you make
// the bean session-scoped. See the section
on validation
// for more details.
public void reset(ActionMapping mapping,
HttpServletRequest
request)
{
clearWarning();
}
public boolean isMissing(String value)
{
if ((value ==
null) || (value.trim().equals("")))
{
return(true);
}
else
{
for(int i=0; i<defaultValues.length; i++)
{
if (value.equals(defaultValues[i]))
{
return(true);
}
}
return(false);
}
}
}
But because we are using the <html:form> and <html:text> tags, we have something else going on as well.
Since the form has an action that has an ActionForm associated with it,
<action path="/update"
type="strutsproject5.UpdateRegisterAction"
name="updateFormBean" scope="request">
<forward
name="missing-value" path="/updateFailure.jsp"/>
</action>
and since there are <html:text> tags inside that form, the initial values of the <html:text> nodes will be the default values of the ActionForm's properties!
The action of the form goes to UpdateRegisterAction. The execute() method will get the values from the ActionForm. If the isMissing() method return true for any property, we'll call the protected method makeWarning().
makeWarning() will create a new instance of a MessageBean, set the Message for it, and add the MessageBean as request attribute.
When we return, we'll map forward "missing-value".
If everything's OK, we'll map forward "updatesucess".
UpdateRegisterAction.java
package strutsproject5;
import javax.servlet.http.*;
import org.apache.struts.action.*; // Action, ActionForm,
etc.
public class UpdateRegisterAction extends
Action
{
public ActionForward execute(ActionMapping
mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
UpdateFormBean userBean = (UpdateFormBean)form;
String dob = userBean.getDob();
String phone = userBean.getPhone();
String major = userBean.getMajor();
String address = userBean.getAddress();
String city = userBean.getCity();
String state = userBean.getState();
String zip = userBean.getZip();
if (userBean.isMissing(dob))
{
makeWarning(request, "dob");
return(mapping.findForward("missing-value"));
}
else if ((userBean.isMissing(phone)))
{
makeWarning(request, "phone");
return(mapping.findForward("missing-value"));
}
else if ((userBean.isMissing(major)))
{
makeWarning(request, "major");
return(mapping.findForward("missing-value"));
}
else if ((userBean.isMissing(address)))
{
makeWarning(request, "address");
return(mapping.findForward("missing-value"));
}
else if ((userBean.isMissing(city)))
{
makeWarning(request, "city");
return(mapping.findForward("missing-value"));
}
else if ((userBean.isMissing(state)))
{
makeWarning(request, "state");
return(mapping.findForward("missing-value"));
}
else if ((userBean.isMissing(zip)))
{
makeWarning(request, "zip");
return(mapping.findForward("missing-value"));
}
else
{
return(mapping.findForward("updatesuccess"));
}
}
proected void makeWarning(HttpServletRequest
request,
String message)
{
MessageBean messageBean = new MessageBean();
messageBean.setMessage(message);
request.setAttribute("messageBean", messageBean);
}
}
Here's the MessageBean.
MessageBean.java
package strutsproject5;
public class MessageBean
{
private String
message = "";
public String getMessage()
{
return(message);
}
public void setMessage(String message)
{
this.message =
message;
}
}
<action path="/update"
type="strutsproject5.UpdateRegisterAction"
name="updateFormBean" scope="request">
<forward
name="missing-value" path="/updateFailure.jsp"/>
</action>
<global-forwards>
<forward name="updatesuccess"
path="/updateSuccess.jsp"/>
</global-forwards>
updateFailure.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@
page contentType="text/html;charset=windows-1252"%>
<HTML>
<%@
taglib prefix="bean" uri="/WEB-INF/struts-bean.tld" %>
<HEAD>
<TITLE>Missing or invalid
<bean:write name="messageBean"
property="message"/>
</TITLE>
</HEAD>
<BODY>
<H2 align="center">Missing
or invalid
<bean:write name="messageBean"
property="message"/>!
</H2>
Please <A HREF="update.jsp">try
again</A>.
</BODY>
</HTML>
updateSuccess.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN">
<HTML>
<HEAD><TITLE>Update
Success</TITLE></HEAD>
<BODY>
<H1
align="center">You have updated successfully.</H1>
<H1>Confirmation</H1>
Congratulations.
You have updated successfully!
<%@
taglib prefix="bean" uri="/WEB-INF/struts-bean.tld" %>
<UL>
<LI>Phone:
<bean:write
name="updateFormBean"
property="phone"/>
<LI>Major:
<bean:write
name="updateFormBean"
property="major"/>
<LI>Date of Birth:
<bean:write
name="updateFormBean" property="dob"/>
<LI>Address:
<bean:write
name="updateFormBean"
property="address"/>
<LI>City:
<bean:write
name="updateFormBean"
property="city"/>
<LI>State:
<bean:write
name="updateFormBean"
property="state"/>
<LI>Zip:
<bean:write
name="updateFormBean"
property="zip"/>
</UL>
</BODY></HTML>
<action path="/login"
type="strutsproject5.RegisterAction"
parameter="operation">
<forward name="emptyUserName" path="/emptyusername.jsp"/>
<forward name="emptyPassword" path="/emptypassword.jsp"/>
<forward name="tooManyMatches" path="/tomanymatches.jsp"/>
<forward name="illegalUsernamePassword" path="/illegalentry.jsp"/>
<forward name="notStudent" path="/notstudent.jsp"/>
<forward name="notUser" path="/notuser.jsp"/>
<forward
name="drop" path="/drop.jsp"/>
<forward
name="see" path="/see.jsp"/>
<forward
name="register" path="/register.jsp"/>
<forward
name="update" path="/update2.jsp"/>
</action>
UpdateRegisterAction2.java
package strutsproject5;
import javax.servlet.http.*;
import org.apache.struts.action.*; // Action, ActionForm,
etc.
public class UpdateRegisterAction2 extends UpdateRegisterAction
{
protected void makeWarning(HttpServletRequest
request,
String message)
{
UpdateFormBean updateFormBean =
(UpdateFormBean)request.getAttribute("updateFormBean");
updateFormBean.setWarning(message);
}
}
Now, update2.jsp is a bit different from update.jsp.
It still loads the default values into the form.
Note that the action is now update2, so it goes here:
<action path="/update2"
type="strutsproject5.UpdateRegisterAction2"
name="updateFormBean" scope="request">
<forward
name="missing-value" path="/update2.jsp"/>
</action>
Note that "missing-value" takes you back to update2.jsp (where you started from). But of course, the ActionForm now has a value for its warning property.
update2.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@
page contentType="text/html;charset=windows-1252"%>
<html>
<head>
<title>Update
Page</title>
</head>
<body>
<h1 align="center">Update
Your Information</h1>
<%@ taglib
prefix="html" uri="/WEB-INF/struts-html.tld" %>
<%@ taglib
prefix="bean" uri="/WEB-INF/struts-bean.tld" %>
<html:form
action="update2">
<bean:write name="updateFormBean"
property="warning"
filter="false"/>
Date of Birth: <html:text property="dob"/><BR>
Phone: <html:text property="phone"/><BR>
Major: <html:text property="major"/><BR>
Address: <html:text property="address"/><BR>
City: <html:text property="city"/><BR>
State: <html:text property="state"/><BR>
Zip: <html:text property="zip"/><BR>
<html:submit value="Sign Me Up!"/>
</html:form>
</body>
</html>
<bean:write name="updateFormBean"
property="warning"
filter="false"/>
<action path="/login3"
type="strutsproject5.RegisterAction"
parameter="operation">
<forward name="emptyUserName" path="/emptyusername.jsp"/>
<forward name="emptyPassword" path="/emptypassword.jsp"/>
<forward name="tooManyMatches" path="/tomanymatches.jsp"/>
<forward name="illegalUsernamePassword" path="/illegalentry.jsp"/>
<forward name="notStudent" path="/notstudent.jsp"/>
<forward name="notUser" path="/notuser.jsp"/>
<forward name="drop"
path="/drop.jsp"/>
<forward name="see"
path="/see.jsp"/>
<forward name="register"
path="/register.jsp"/>
<forward name="update"
path="/update3.jsp"/>
</action>
login3.jsp
<%@
taglib prefix="bean" uri="/WEB-INF/struts-bean.tld" %>
<HTML>
<HEAD><TITLE><bean:message key="form.title" /> </TITLE></HEAD>
<BODY>
<H1
align="center">
<bean:message
key="form.title" />
</H1>
<FORM
ACTION="login3.do" METHOD="POST">
<bean:message
key="form.userNamePrompt" />
<INPUT TYPE="TEXT"
NAME="username">
<BR> <bean:message key="form.passwordPrompt"
/>
<INPUT TYPE="PASSWORD"
NAME="password">
<BR><bean:message key="form.actionPrompt"
/>
<select name="operation">
<option value="register">
<bean:message key="form.registerMessage"
/>
</option>
<option value="drop">
<bean:message key="form.dropMessage"
/>
</option>
<option value="see">
<bean:message key="form.seeGradesMessage"
/>
</option>
<option value="update">
<bean:message key="form.updateInfoMessage"
/>
</option>
</select>
<BR> <INPUT TYPE="SUBMIT"
VALUE="<bean:message
key="form.submitPrompt" />">
</FORM>
</BODY>
</HTML>
<form-beans>
<form-bean name="userFormBean"
type="strutsproject5.UserFormBean"/>
<form-bean name="updateFormBean"
type="strutsproject5.UpdateFormBean"/>
<form-bean
name="updateFormBean3" type="strutsproject5.UpdateFormBean3"/>
</form-beans>
UpdateFormBean3.java
package strutsproject5;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
public class UpdateFormBean3 extends ActionForm
{
private String
phone = "ddd-ddd-dddd";
private String dob = "M/D/YYYY";
private String
major = "Your Major";
private String
address = "Your Address";
private String
city = "City";
private String
state = "SS";
private String zip
= "ddddd";
private String
username = "289384382";
private String
warning = "";
private String[] defaultValues =
{ phone, dob, major, address, city, state, zip };
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username =
username;
}
public String getDob()
{
return dob;
}
public void setDob(String dob)
{
this.dob = dob;
}
public String getPhone()
{
return phone;
}
public void setPhone(String phone)
{
this.phone =
phone;
}
public void setMajor(String major)
{
this.major =
major;
}
public String getMajor()
{
return major;
}
public String getCity()
{
return city;
}
public void setCity(String city)
{
this.city =
city;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public void setState(String state)
{
this.state =
state;
}
public String getState()
{
return state;
}
public void setZip(String zip)
{
this.zip = zip;
}
public String getZip()
{
return zip;
}
public void setWarning(String baseWarning)
{
this.warning =
"<H2><FONT
COLOR=RED>Missing or invalid " +
baseWarning
+ "!</FONT></H2>";
}
public String getWarning()
{
return warning;
}
public void clearWarning()
{
warning =
"";
}
// The reset
method below is ONLY needed if you make
// the bean session-scoped. See the section
on validation
// for more details.
public void reset(ActionMapping mapping,
HttpServletRequest
request)
{
clearWarning();
}
public boolean isMissing(String value)
{
if ((value ==
null) || (value.trim().equals("")))
{
return(true);
}
else
{
for(int i=0; i<defaultValues.length; i++)
{
if (value.equals(defaultValues[i]))
{
return(true);
}
}
return(false);
}
}
}
update3.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@
page contentType="text/html;charset=windows-1252"%>
<html>
<head>
<title>Update
Page</title>
</head>
<body>
<h1 align="center">Update Your
Information</h1>
<%@ taglib
prefix="html" uri="/WEB-INF/struts-html.tld" %>
<%@ taglib
prefix="bean" uri="/WEB-INF/struts-bean.tld" %>
<html:form
action="update3">
<bean:write name="updateFormBean3"
property="warning"
filter="false"/>
Date of Birth: <html:text property="dob"/><BR>
Phone: <html:text property="phone"/><BR>
Major: <html:text property="major"/><BR>
Address: <html:text property="address"/><BR>
City: <html:text property="city"/><BR>
State: <html:text property="state"/><BR>
Zip: <html:text property="zip"/><BR>
<br><input
type="radio" name="choice" value="usethis"
checked>Use This
<br><input
type="radio" name="choice" value="loadcurrent">
Load Current
<P><html:submit value="Update"/>
</html:form>
</body>
</html>
<action path="/update3" type="strutsproject5.UpdateRegisterAction3"
name="updateFormBean3"
scope="request"
parameter="choice">
<forward
name="missing-value" path="/update3.jsp"/>
<forward name="updatesuccess"
path="/updateSuccess3.jsp"/>
</action>
updateSuccess3.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN">
<HTML>
<HEAD><TITLE>Update
Success</TITLE></HEAD>
<BODY>
<H1
align="center">You have updated database successfully.</H1>
<H1>Confirmation</H1>
Congratulations.
You have updated database successfully!
<%@
taglib prefix="bean" uri="/WEB-INF/struts-bean.tld" %>
<UL>
<LI>Phone:
<bean:write
name="updateFormBean3" property="phone"/>
<LI>Major:
<bean:write
name="updateFormBean3" property="major"/>
<LI>Date of Birth:
<bean:write
name="updateFormBean3" property="dob"/>
<LI>Address:
<bean:write
name="updateFormBean3" property="address"/>
<LI>City:
<bean:write
name="updateFormBean3" property="city"/>
<LI>State:
<bean:write
name="updateFormBean3" property="state"/>
<LI>Zip:
<bean:write
name="updateFormBean3" property="zip"/>
</UL>
</BODY></HTML>
UpdateRegisterAction3.java
package strutsproject5;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.actions.DispatchAction;
public class UpdateRegisterAction3 extends DispatchAction
{
public ActionForward usethis(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws
Exception
{
UpdateFormBean3 userBean
= (UpdateFormBean3)form;
String dob = userBean.getDob();
String phone = userBean.getPhone();
String major = userBean.getMajor();
String address = userBean.getAddress();
String city = userBean.getCity();
String state = userBean.getState();
String zip = userBean.getZip();
if (userBean.isMissing(dob))
{
makeWarning(request, "dob");
return(mapping.findForward("missing-value"));
}
else if ((userBean.isMissing(phone)))
{
makeWarning(request, "phone");
return(mapping.findForward("missing-value"));
}
else if ((userBean.isMissing(major)))
{
makeWarning(request, "major");
return(mapping.findForward("missing-value"));
}
else if ((userBean.isMissing(address)))
{
makeWarning(request,
"address");
return(mapping.findForward("missing-value"));
}
else if ((userBean.isMissing(city)))
{
makeWarning(request, "city");
return(mapping.findForward("missing-value"));
}
else if ((userBean.isMissing(state)))
{
makeWarning(request,
"state");
return(mapping.findForward("missing-value"));
}
else if ((userBean.isMissing(zip)))
{
makeWarning(request,
"zip");
return(mapping.findForward("missing-value"));
}
else
{
HttpSession
session = request.getSession(true);
RegistrationDataBean
data;
data = (RegistrationDataBean)session.getAttribute("data");
String username = data.getUsername();
String url
= "jdbc:odbc:registrar";
String query1 = "UPDATE STUDENTS
SET DOB = ? WHERE USERID = ?";
PreparedStatement
stmt1;
String query2 = "UPDATE STUDENTS
SET MAJOR = ? WHERE USERID = ?";
PreparedStatement
stmt2;
String query3 = "UPDATE STUDENTS
SET ADDRESS = ? WHERE USERID = ?";
PreparedStatement
stmt3;
String query4 = "UPDATE STUDENTS
SET PHONE = ? WHERE USERID = ?";
PreparedStatement
stmt4;
String query5 = "
PreparedStatement
stmt5;
String query6 = "
PreparedStatement
stmt6;
String query7 = "UPDATE STUDENTS
SET ZIP = ? WHERE USERID = ?";
PreparedStatement
stmt7;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try
{
Connection con = DriverManager.getConnection(url, "", "");
stmt1 = con.prepareStatement(query1);
stmt1.setString(1,
userBean.getDob());
stmt1.setString(2,
username);
stmt1.executeUpdate();
stmt2 = con.prepareStatement(query2);
stmt2.setString(1,
userBean.getMajor());
stmt2.setString(2,
username);
stmt2.executeUpdate();
stmt3 = con.prepareStatement(query3);
stmt3.setString(1,
userBean.getAddress());
stmt3.setString(2,
username);
stmt3.executeUpdate();
stmt4 = con.prepareStatement(query4);
stmt4.setString(1,
userBean.getPhone());
stmt4.setString(2,
username);
stmt4.executeUpdate();
stmt5 = con.prepareStatement(query5);
stmt5.setString(1,
userBean.getCity());
stmt5.setString(2,
username);
stmt5.executeUpdate();
stmt6 = con.prepareStatement(query6);
stmt6.setString(1,
userBean.getState());
stmt6.setString(2,
username);
stmt6.executeUpdate();
stmt7 = con.prepareStatement(query7);
stmt7.setString(1,
userBean.getZip());
stmt7.setString(2,
username);
stmt7.executeUpdate();
}
catch (SQLException e)
{
System.err.println(e.getMessage());
}
return(mapping.findForward("updatesuccess"));
}
}
public ActionForward loadcurrent(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse
response)
throws
Exception
{
UpdateFormBean3 userBean
= (UpdateFormBean3)form;
HttpSession
session = request.getSession(true);
RegistrationDataBean
data;
data = (RegistrationDataBean)session.getAttribute("data");
String username = data.getUsername();
String url =
"jdbc:odbc:registrar";
String query = "SELECT DOB, MAJOR,
PHONE, ADDRESS, CITY, STATE, ZIP " +
"FROM STUDENTS
WHERE USERID = ?";
PreparedStatement
stmt;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try
{
Connection con = DriverManager.getConnection(url, "", "");
stmt = con.prepareStatement(query);
stmt.setString(1, username);
ResultSet
rs = stmt.executeQuery();
rs.next();
userBean.setDob(rs.getString(1));
userBean.setMajor(rs.getString(2));
userBean.setPhone(rs.getString(3));
userBean.setAddress(rs.getString(4));
userBean.setCity(rs.getString(5));
userBean.setState(rs.getString(6));
userBean.setZip(rs.getString(7));
userBean.setUsername(username);
}
catch (SQLException e)
{
System.err.println(e.getMessage());
}
return(mapping.findForward("missing-value"));
}
protected void makeWarning(HttpServletRequest
request,
String message)
{
UpdateFormBean3 updateFormBean3
=
(UpdateFormBean3)request.getAttribute("updateFormBean3");
updateFormBean3.setWarning(message);
}
}