Working with User Input Ver 1.0 © 2005 Aptech Limited 1
Lab Deliverable 10 Working with User Input
Part II
1. Create a user details page. The page should have First Name, Last Name, and Email address
fields. On clicking the submit button, a new Web page should display the details entered by
the user.
Hint: Use getAttribute to display the user details.
Solution:
The files used in this exercise are:
1. index.jsp
2. displayname.jsp
3. NameForm.java
4. NameAction.java
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
<head>
<title>Sample Struts Application</title>
</head>
<body>
<h3> User Details</h3>
<html:form action="Name" name="nameForm"
type="example.NameForm" >
<table width="80%" border="0">
<tr>
<td><b>First Name:</b>
<html:text property="name" />
</td>
</tr>
<tr>
<td><b>Last Name:</b>
<html:text property="last" />
</td>
</tr>
<tr>
<td>
<b>Email:
</b>
2 Ver 1.0 © 2005 Aptech Limited JSP and Struts
<html:text property="email" />
</td>
</tr>
<tr>
<td><html:submit />
</td>
</tr>
</table>
</html:form>
</body>
</html>
Enter the code in Notepad and save the file as index.jsp in %TOMCAT_HOME%/webapps/
details.
<html>
<head>
<title>Sample Struts Display Name</title>
</head>
<body>
<h2> Confirm the Details You Entered</h2>
<table width="80%" border="0">
<tr>
<td>
<b>First Name:</b><%= request.getAttribute("NAME") %>
</td>
</tr>
<tr>
<td>
<b>Last Name:</b><%= request.getAttribute("LAST") %>
</td>
</tr>
<tr>
<td>
<b>Email:</b><%= request.getAttribute("EMAIL") %></td>
</tr>
</table>
</body>
</html>
Enter the code in Notepad and save the file as displayname.jsp in %TOMCAT_HOME%/
webapps/details.
Working with User Input Ver 1.0 © 2005 Aptech Limited 3
package example;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class NameForm extends ActionForm
{
private String name = null;
private String last = null;
private String email = null;
public String getName()
{
return (name);
}
public String getLast()
{
return (last);
}
public String getEmail()
{
return (email);
}
public void setName(String name)
{
this.name = name;
}
public void setLast(String last)
{
this.last = last;
}
public void setEmail(String email)
{
this.email = email;
}
public void reset(ActionMapping mapping, HttpServletRequest
request)
{
this.name = null;
this.last= null;
this.email=null;
}
}
Enter the Java code in Notepad and save the file as NameForm.java. Compile the file from the
command prompt and copy the class file in %TOMCAT_HOME%/webapps/details/WEB-
INF/classes/example.
4 Ver 1.0 © 2005 Aptech Limited JSP and Struts
package example;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class NameAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm
form,HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
String target = new String("success");
String name=null;
String last=null;
String email=null;
if ( form != null )
{
NameForm nameForm = (NameForm)form;
NameForm lastForm = (NameForm)form;
NameForm emailForm = (NameForm)form;
name = nameForm.getName();
last = lastForm.getLast();
email = emailForm.getEmail();
}
if ( name == null )
{
target = new String("failure");
}
else
{
request.setAttribute("NAME", name);
request.setAttribute("LAST", last);
request.setAttribute("EMAIL", email);
}
return (mapping.findForward(target));
}
}
Working with User Input Ver 1.0 © 2005 Aptech Limited 5
Enter the Java code in Notepad and save the file as NameAction.java. Compile the file from
the command prompt and copy the class file in
%TOMCAT_HOME%/webapps/details/WEB-INF/classes/example.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts
Configuration 1.1//EN"
" />config_1_1.dtd">
<struts-config>
<! Form Bean Definitions >
<form-beans>
<form-bean name="nameForm" type="example.NameForm"/>
</form-beans>
<! Action Mapping Definitions >
<action-mappings>
<action path="/Name" type="example.NameAction" name="nameForm"
input="/index.jsp">
<forward name="success" path="/displayname.jsp"/>
<forward name="failure" path="/index.jsp"/>
</action>
</action-mappings>
</struts-config>
Update the struts-config.xml file used in the Web application.
The output of the program is as shown in Figure 17.1.
6 Ver 1.0 © 2005 Aptech Limited JSP and Struts
Figure 17.1: Accepting User Details
The output of the program that displays user details is as shown in Figure 17.2.
Figure 17.2: Details Page
Do It Yourself
1. Consider the Part II question and add validation to the First Name, Last name, and Email
address fields.
Hint: Use validate method to validate the user details
Working with User Input Ver 1.0 © 2005 Aptech Limited 7
Solution:
The files used in this exercise are:
1. valid.jsp
2. savedOk.jsp
3. CustForm.java
4. CustAction.java
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
<head> <title>Validations</title> </head>
<body>
<html:errors/>
<html:form action="cust.do" >
<b>First Name:</b> <html:text property="first" /> <br>
<b>Last Name:</b> <html:text property="last" /> <br>
<b>Email:</b>
<html:text property="email" /> <br>
<html:submit />
</html:form>
</body>
</html>
Enter the code in Notepad and save the file as valid.jsp in %TOMCAT_HOME%/webapps/
details.
<html>
<head><title>Saved</title> </head>
<body>
<h3>Information is Saved</h3>
</body>
</html>
Enter the code in Notepad and save the file as savedOk.jsp in
%TOMCAT_HOME%/webapps/ details.
package common;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class CustForm extends ActionForm
{
private String mFirst= null;
8 Ver 1.0 © 2005 Aptech Limited JSP and Struts
private String mLast= null;
private String mEmail= null;
public String getFirst()
{
return mFirst;
}
public void setFirst(String aFirst)
{
mFirst= aFirst;
}
public String getLast()
{
return mLast;
}
public void setLast(String aLast)
{
mLast= aLast;
}
public String getEmail()
{
return mEmail;
}
public void setEmail(String aEmail)
{
mEmail= aEmail;
}
public void reset(ActionMapping aMapping, HttpServletRequest
aRequest)
{
mFirst= null;
mLast= null;
mEmail= null;
}
public ActionErrors validate(ActionMapping aMapping,
HttpServletRequest aRequest)
{
ActionErrors err = new ActionErrors();
if ((mFirst==null) || (mFirst.length() <1) )
err.add("first", new ActionError("error.cust.first") );
if ((mLast==null) || (mLast.length()<1) )
err.add("last", new ActionError("error.cust.last")
);
if ((mEmail==null) || (mEmail.length()<1) )
err.add("email", new
ActionError("error.cust.email") );
Working with User Input Ver 1.0 © 2005 Aptech Limited 9
return err;
}
}
Enter the Java code in Notepad and save the file as CustForm.java. Compile the file from the
command prompt and copy the class file in %TOMCAT_HOME%/webapps/details/WEB-
INF/classes/common.
package common;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class CustAction extends Action
{
public ActionForward perform(ActionMapping aMapping,
ActionForm aForm, HttpServletRequest aRequest,
HttpServletResponse aResponse) throws ServletException
{
CustForm f = (CustForm) aForm;
String first = f.getFirst();
String last = f.getLast();
String email = f.getEmail();
return aMapping.findForward("saved");
}
}
Enter the Java code in Notepad and save the file as CustAction.java. Compile the file from
the command prompt and copy the class file in
%TOMCAT_HOME%/webapps/details/WEB-INF/classes/common.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts
Configuration 1.1//EN"
" />config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="custForm" type="common.CustForm"/>
</form-beans>
<! Action Mapping Definitions >
10 Ver 1.0 © 2005 Aptech Limited JSP and Struts
<action-mappings>
<action path="/cust" type="common.CustAction"
name="custForm" input="/valid.jsp">
<forward name="saved" path="/savedOk.jsp" />
</action>
</action-mappings>
<! end comment if struts1.0.x >
<plug-in
className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-
rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
Update the struts-config.xml file used in the Web application.
# standard errors
errors.header=<UL>
errors.prefix=<LI>
errors.suffix=</LI>
errors.footer=</UL>
# validator
errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} is required.
errors.byte={0} must be an byte.
errors.date={0} is not a date.
errors.double={0} must be an double.
errors.float={0} must be an float.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.short={0} must be an short.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.
# other
errors.cancel=Operation cancelled.
errors.detail={0}
errors.general=The process did not complete. Details should
follow.
errors.token=Request could not be completed. Operation is not in
sequence.
# welcome