Listing 1
The LogonForm Bean (logon.LogonForm)
package logon;
import org.apache.struts.action.ActionForm;
public class LogonForm extends ActionForm {
/**
* A String variable representing our username field
*/ protected String username;
/**
* A String variable representing our password field
*/ protected String password;
/**
* A method to get the username form field. Will be
* used in LogonAction class to retrieve the username
*/
public String getUsername() {
return username;
}
/**
* A method to set the username form field. This is
* called on using introspection from the ActionServlet
* controller
*/
public void setUsername(String username) {
this.username = username;
}
/**
* A method to set the password form field. This is
* called on using introspection from the ActionServlet
* controller
*/
public void setPassword(String password) {
this.password = password;
}
/**
* A method to get the password form field. Will be
* used in LogonAction to retrieve the username
*/
public String getPassword() {
return password;
}
}
Listing 2
The Action Class (LogonAction.java)
package logon;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;
public class LogonAction extends Action
{
/**
* This is the method called on by ActionServlet
* when a request is made for our logon action.
* "mapping" is a class representation of our
* logon action as defined in action.xml.
* "form" is our form bean that we created for
* this action, it should be an instance of
* "LogonForm".
*/
public ActionForward perform( ActionMapping mapping,
ActionForm form, HttpServletRequest request, HttpServletResponse response)
{
//turn our form instance into our custom form bean
LogonForm theForm = (LogonForm) form;
//get the username and password from our
//form, they were set using the struts form
//tags in logon.jsp
String username = theForm.getUsername();
String password = theForm.getPassword();
if
(username.equals(password)) {
// then get an ActionForward that
// maps to the logical name "success"
// and return it return mapping.findForward("success");
}
else
{
//return an ActionForward respresenting
//the logical failure page
return mapping.findForward("failure");
}
}
}
Listing 3
The Mapping (struts-config.xml)
<?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-beans>
<form-bean name="logonForm" type="logon.LogonForm"/>
</form-beans>
<action-mappings>
<action path="/logon" type="logon.LogonAction" name="logonForm" scope="request" input="/logon.jsp">
<forward name="success" path="/success.jsp" />
<forward name="failure" path="/failure.jsp" />
</action>
</action-mappings>
</struts-config>>
Listing 4
?xml version="1.0" encoding="ISO-8859-1" ?
<!DOCTYPE web-app PUBLIC
'-//Sun Microsystems, Inc.//DTD Web Application2.3//EN'
'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app>
<servlet>
<!--Inside of Here we define all servlet information. -->
<servlet-name>action</servlet-name>
<!-- This is the controller for our application. -->
<servlet class>org.apache.struts.action.
ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>validate
</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<!--See the Servlet specification for exact information. -->
<!--This code sends requests with a ".do" extension to the ActionServlet controller.-->
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>