Source code of the following files are given below:
1. index.jsp
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
<head
<title>Struts Sample</title>
</head>
<body>
<html:form action="Lookup" name="lookupForm" type="test.LookupForm">
<table width="80%" border=1>
<tr>
<td>
Symbol:</td>
<td><html:text property="symbol"/></td>
</tr>
<tr>
<td colspan="2" align="center".>
<html:submit/>
</td>
</tr>
</table>
</html:form>
</body>
</html>
2. LookupForm.java
package
test;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class LookupForm extends ActionForm
{
private
String symbol=null;
public String getSymbol()
{
return (symbol);
}
public void setSymbol(String symbol)
{
this.symbol=symbol;
}
public void reset(ActionMapping mapping,HttpServletRequest request)
{
this.symbol=null;
}
}
3. quote.jsp
<html>
<head>
<title>
Response View</title></head>
<body>
<h1>Current Price:</h1>
<%= request.getAttribute("PRICE") %>
</body>
</html>
4. LookupAction.java
package test;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class LookupAction extends Action{
protected Double getQuote(String symbol)
{
if(symbol.equalsIgnoreCase("SUNW"))
{
return new Double(25.00);
}
return null;
}
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException
{
Double price=null;
//Default target to success
String target=new String("success");
if(form!=null)
{
//Use the LookupFrom to get the request parameters
LookupForm lookupForm=(LookupForm)form;
String symbol=lookupForm.getSymbol();
price=getQuote(symbol);
}
if(price==null)
{
target=new String("failure");
}
else
{
request.setAttribute("PRICE",price);
}
//Forward to the appropriate View
return (mapping.findForward(target));
}
}
5. struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<! DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="lookupForm" type="test.LookupForm"/>
</form-beans>
<action-mappings>
<action path="/Lookup" type="test.LookupAction" name="lookupForm">
<forward name="success" path="/quote.jsp"/>
<forward name="failure" path="/index.jsp"/>
</action>
</action-mappings>
</struts-config>
6. web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<!-- Standard Action
Servlet Configuration -->
<servlet>
<servlet-name>action</servlet-name>
<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>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Standard Actionservlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- Standard Welcome File List -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Struts Tag Library Descriptors -->
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
</web-app>
Will be waiting for your reply..
Thanks
Amitav Anand