Java Tutorial - Java Script : Handling the Registration Form

Java Tutorial - Java Script :

Handling the Registration Form


Now that we have seen how the error processing works, let’s examine how we handle the registration screen in Struts. As you may recall from our initial login screen, we provided a Register button to allow a user to register for the services of the site. Following is the relevant section of code from index.jsp:

<html:form action=”startregister “>
<html:submit> Register </html:submit>
</html:form>
Our tag specifies startregister as the action for this form. Even though we are not dealing with a form in the conventional sense, we use the

<html:form> taglib to define the action for the Register button. The pertinent configuration associated with the startregister action is shown below for convenience.
<form-beans>
<form-bean name =”registerForm” type=”RegisterForm” />
</form-beans>
<action-mappings>
<action path=”/startregister”
type=”EmptyAction”
name=”emptyForm”
validate=”false”
input=”/index.jsp”>
<forward name =”Success” path=”/register.jsp” />
</action>
</action-mappings>

The above configuration tells the controller that when a request comes in from the browser for the URL startregister.do, the EmptyForm should be used to receive the form data. The EmptyForm class does nothing, and only exists to short-circuit ActionServlet in situations like this when we do not require form processing. The code for the EmptyForm class is as follows:

 import org.apache.struts.action.*;
public final class EmptyForm extends ActionForm
{
}
We do not perform validation, so the servlet will send the request on to our action class, EmptyAction. In this case, the only action we need is to forward the request to our target JSP. The code for the action class is as follows:
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class EmptyAction extends Action
{
public ActionForward execute(
ActionMapping aMapping,
ActionForm aForm,
HttpServletRequest aRequest,
HttpServletResponse aResponse)
throws ServletException
{
return aMapping.findForward(“Success”);
}
}

Again, our implementation of the Action class does nothing but forward to the Success view, which we see is configured for register.jsp. Now that we have everything set up, go ahead and click the Register button and you should see the registration form, as shown in Figure 7.4.
We now have an interface that we can use to collect registration information from our prospective members. Let’s look at the code for register.jsp in order to look at the functionality.

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<HTML>
<HEAD>
<%@ taglib uri=”/WEB-INF/struts-html.tld” prefix=”html” %>
<%@ taglib uri=”/WEB-INF/struts-bean.tld” prefix=”bean” %>
<TITLE><bean:message key=”oldfriends.highschool”/></TITLE>
</HEAD>
<BODY bgcolor=”#999999”>
<TABLE width=”100%”>
<TBODY>
<TR valign=”top”><!-- row 2 -->
<TD colspan=”4”>
<TABLE border=”0” width=”100%”>
<COL span=”1”>
<COL span=”1” align=”center”>
<TBODY>
<TR valign=”middle” align=”center”>
<TD valign=”bottom” align=”left”>
<html:form action=”/mainscreen”>