hi
here is a simple example which involves two JSP, two form classes and an action class.Index jsp is loaded first. here you key in the username and click the ok button. it takes you to the other jsp named Accept.jsp which displays the user name. In the backend, when you click the button on index.jsp, the value of username is taken to the form bean and in the action class the data from the form bean of Index.jsp is retrieved and stored in the form bean of the Accept.jsp and the jsp is loaded. It automatically gets the data from the form bean and displays it on the Accept.jsp.
The following are the codes.
code of index.jsp..
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
<head>
<title>FIrst Example</title>
<html:base/>
</head>
<body bgcolor="white">
<html:errors/>
<html:form action="Index.do" focus="username" name="indexForm" type="testing.IndexForm">
<table border="0" width="100%">
<tr>
<th align="right">
Give Your Name
</th>
<td align="left">
<html:text property="username" size="16" maxlength="16"/>
</td>
</tr>
<tr>
<td align="right">
<html:submit property="submit" value="OK"/>
</td>
</tr>
</table>
</html:form>
</body>
</html>
code for accept.jsp
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<HTML>
<HEAD>
</HEAD>
<BODY>
<html:form action="" focus="output" name="AcceptForm" type="testing.AcceptForm">
<table border="0" width="100%">
<tr>
<th align="right">
Give Your Name
</th>
<td align="left">
<html:text property="output" size="16" maxlength="16"/>
</td>
</tr>
<tr>
<td align="right">
<html:submit property="submit" value="OK"/>
</td>
</tr>
</table>
</html:form>
</BODY>
</HTML>
code for the action class IndexAction
package testing;
import javax.servlet.http.*;
import javax.servlet.*;
import org.apache.struts.action.*;
import java.io.IOException;
public final class IndexAction extends Action {
public ActionForward perform (ActionMapping mapping,ActionForm form ,HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
String username = ((IndexForm)form).getUsername();
AcceptForm acptform = new AcceptForm();
acptform.setOutput(username);
HttpSession session = request.getSession();
session.setAttribute("AcceptForm",acptform);
return mapping.findForward("success");
}
}
code for index form
package testing;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
public final class IndexForm extends ActionForm{
private String username="subhendu";
public IndexForm()
{
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username=username;
}
}
code for acceptform
package testing;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
public final class AcceptForm extends ActionForm{
private String output;
public AcceptForm()
{
}
public String getOutput()
{
return output;
}
public void setOutput(String output)
{
this.output="from accept bean"+output;
}
}
entry in action.xml
<action-mappings>
<action path="/Index"
actionClass="testing.IndexAction"
formAttribute="indexForm"
formClass="testing.IndexForm"
scope="session"
inputForm="/index.jsp"
validate="true">
<forward name="success" path="/Accept.jsp"/>
</action>
</action-mappings>