Developing User Registration Form
This section takes you through the steps necessary to develop the User Registration Form for our Struts, Hibernate and Spring based User Registration and Authentication Application. This section shows you how to develop the User Registration Form and related JSP files.
This User Registration section of application is composed of two Views.
- Registration Form: Registration Form displays the fields
to take the necessary input from the user.
- Result Page: The result page displays the message that indicates success. If the there is some error while registering the user, Registration Form is displayed again indicating the error occurred during the registration process.
The necessary code are described further down in this section.
Registration JSP (userRegister.jsp)
The Registration JSP (userRegister.jsp) generates the registration form and displayed to the user to take the input.
Here is the code of the userRegister.jsp file:
<%@ taglib uri="/tags/struts-bean" prefix="bean" %> <%@ taglib uri="/tags/struts-html" prefix="html" %> <html:html locale="true"> <head> <LINK rel="stylesheet" type="text/css" name="anyname" href="<html:rewrite page='/css/style.css'/>"> <title></title> <html:base/> <SCRIPT LANGUAGE=javascript> function checkEmail(email) { if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)){ return (true) } alert("Invalid E-mail Address! Please re-enter.") return (false); } function validateForm(formObj){ if(formObj.userid.value.length==0){ alert("Please enter User ID!"); formObj.userid.focus(); return false; } if(formObj.password.value.length==0){ alert("Please enter password!"); formObj.password.focus(); return false; } if(formObj.email.value.length==0){ alert("Please enter Email!"); formObj.email.focus(); return false; } if(!checkEmail(formObj.email.value)){ formObj.email.focus(); return false; } if(formObj.address.value.length==0){ alert("Please enter address!"); formObj.address.focus(); return false; } if(formObj.phno.value.length==0){ alert("Please enter Phone No.!"); formObj.phno.focus(); return false; } if(isNaN(formObj.phno.value)){ alert("Please enter correct Phone No!"); formObj.phno.focus(); return false; } formObj.actionUpdateData.value="update"; return true; } //--> </SCRIPT> </head> <body> <%@ include file="../top.jsp"%> <center> <!-- <table width="60%"> <tr> <td width="100%"> --> <html:form action="/userregister" method="post" onsubmit="return validateForm(this);"> <html:hidden property="id" /> <html:hidden property="action"/> <html:hidden property="actionUpdateData"/> <table width="50%" border="1" class="signup" align="center"> <tr> <td colspan="2" align="center"> <font size="4" color="#660099"> Please Enter the Following Details</font><br> </td> </tr> <!-- <tr> <td align="right" width="50%"><b>Id</b></td> <td width="50%" align="left"> <html:text property="id" size="30" maxlength="120"/> </td> </tr> --> <tr><td colspan="2" align="center"><font color="red"><html:errors/></font> </td></tr> <tr> <td align="right" width="50%"><b>User Id<font color="#FF0000">*</font></b></td> <td align="left" width="50%"> <html:text property="userid" size="30" maxlength="120"/> </td> </tr> <tr> <td align="right"><b> Password<font color="#FF0000">*</font></b></td> <td align="left"> <html:password property="password" size="30" maxlength="120"/> </td> </tr> <tr> <td align="right"><b>Email</b></td> <td align="left"> <html:text property="email" size="30" maxlength="120"/> </td> </tr> <tr> <td align="right"><b>Address</b></td> <td align="left"> <html:text property="address" size="30" maxlength="120"/> </td> </tr> <tr> <td align="right"><b>Phone No.<b></td> <td align="left"> <html:text property="phno" size="30" maxlength="120"/> </td> </tr> <tr><td colspan="2"> </td></tr> <tr> <td align="center" colspan="2"> <html:submit>Save</html:submit> </td> </tr> </table> </html:form> <!-- </td> </tr> </table> </center> --> </body> </html:html>
Above file should be saved in the project\pages\user directory of web application.
Struts Registration Form
Registration form of the application which is associated with the ActionMapping "/userregister", which is action mapping used for registering the user. Here is code of Registration Form:
package roseindia.web.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
public class UserRegisterForm extends ActionForm{
private String action="add";
private String actionUpdateData;
private Integer id;
private String userid;
private String password;
private String email;
private String address;
private String phno;
public void reset(ActionMapping mapping,HttpServletRequest request){
this.id = null;
this.userid=null;
this.password=null;
this.email=null;
this.address=null;
this.phno=null;
this.action="add";
this.actionUpdateData="";
}
public ActionErrors validate(
ActionMapping mapping, HttpServletRequest request ) {
ActionErrors errors = new ActionErrors();
return errors;
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhno() {
return phno;
}
public void setPhno(String phno) {
this.phno = phno;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getActionUpdateData() {
return actionUpdateData;
}
public void setActionUpdateData(String actionUpdateData) {
this.actionUpdateData = actionUpdateData;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
Above code UserRegistrationForm.java should be present in the project\WEB-INF\src\java\roseindia\web\struts\form directory of the project.
Success JSP Page
Success page confirms the successful completion of user registration. Here is the code of the success jsp page (registersuccess.jsp):
<%@ taglib uri="/tags/struts-bean" prefix="bean" %> <%@ taglib uri="/tags/struts-html" prefix="html" %> <html:html locale="true"> <head> <title></title> <html:base/> </head> <body> <%@ include file="../top.jsp"%> <center> <p><b>You are register successfuly !</b></p> </center> </body> </html:html> |
Above jsp page (registersuccess.jsp) should be saved into project\pages\user directory.
In this section we have developed JSP file and Struts action form for our application.