Saturday, June 20, 2009

Create a Login form using struts

Create a Login form using struts. Create two text fields in the Web page, and name the fields as User and Password. The page includes a Submit button. After the user clicks the Submit button, a page should appear indicating that the user is a valid user.

The files used in this exercise are:

1. cust.jsp
2. valid.jsp
3. CustForm.java
4. CustAction.java

<%@ taglib uri = "/WEB-INF/struts-html.tld" prefix="html" %>
<html>
<head> <title>Testing struts</title> </head>
<body>
<html:form action = "cust.do" > <center>
<b>User:</b> <html:text property = "first" />
<b>Password:</b><html:password property = "pwd" />
<html:submit /> </center>
</html:form>
</body>
</html>

Enter the code in Notepad and save the file as cust.jsp in %TOMCAT_HOME%/webapps/ struts-test.

<html>
<head><title>Valid User</title>
</head>
<body>
<center><h2><b>VALID USER</b></h2></center>
</body>
</html>

Valid Use
Enter the code in Notepad and save the file as valid.jsp in %TOMCAT_HOME%/webapps/ struts-test.

package common.test;
import javax.servlet.http.*;
import org.apache.struts.action.*;

public class CustForm extends ActionForm
{
private String mFirst = null;
private String mPwd = null;

public String getFirst() { return mFirst; }
public void setFirst(String aFirst) { mFirst = aFirst; }

public String getPwd() { return mPwd; }
public void setPwd(String aPwd) { mPwd = aPwd; }
public void reset(ActionMapping aMapping, HttpServletRequest aRequest)
{
mFirst = null;
mPwd = null;
}
}

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/struts-test/WEB-INF/classes/common/test.

package common.test;
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.getPwd();
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/struts-test/WEB-INF/classes/common/test.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
<struts-config>
<!-- Form Bean Definitions -->
<form-beans>
<form-bean name = "custForm" type = "common.test.CustForm"/>
</form-beans>
<!-- Global Forward Definitions -->
<global-forwards>
<forward name = "start" path = "/index.jsp"/>
</global-forwards>
<!-- Action Mapping Definitions -->
<action-mappings>
<action path = "/cust" type = "common.test.CustAction" name = "custForm">
<forward name = "saved" path = "/valid.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 16.3.


Figure 16.3: Login page

After the user clicks the Submit button, the output of the program is as shown in Figure 16.2.

Figure 16.4: Valid user page

0 nhận xét: