Create a user details page

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" %>
<%@ 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>
<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.

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.
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));
}
}
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"
"http://jakarta.apache.org/struts/dtds/struts-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.


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