index.jsp
<%@ page language="java" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<html>
<body>
<html:form action="Lookup">
<table width ="45% " border="0">
<tr>
<td>Enter The Symbol:</td>
<td><html:text property="symbol" /></td>
</tr>
<tr>
<td colspan="2" align="center"><html:submit /></td>
</tr>
</table>
</html:form>
</body>
</html>
quote.jsp
<html>
<body>
<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
current Price:<%=request.getAttribute("PRICE")%>
</td>
</tr>
</table>
</body>
</html>
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 Bean Definitions -->
<form-beans>
<form-bean name="myfrm" type="har.LookupForm" />
</form-beans>
<!-- =================================== Action Mapping Definitions -->
<action-mappings>
<action path="/Lookup" type="har.LookupAction" name="myfrm">
<forward name="success" path="/quote.jsp"/>
<forward name="failure" path="/index.jsp"/>
</action>
</action-mappings>
</struts-config>
<?xml version="1.0" encoding="ISO-8859-1"?>
web.xml
<!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>
<display-name>Struts Blank Application</display-name>
<!-- Standard Action Servlet Configuration (with debugging) -->
<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>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- The Usual Welcome File List -->
<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
</web-app>
LookupAction.java
package har;
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;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
public class LookupAction extends Action {
protected Double getQuote(String symbol){
if(symbol.equalsIgnoreCase("SUNW")){
return new Double(25.00);
}
else
{
return new Double(50.00);
}
}
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException {
Double price=null;
String target =new String("success");
if(form!= null){
LookupForm lookupForm=(LookupForm)form;
String symbol=lookupForm.getSymbol();
price=getQuote(symbol);
}
if(price == null){
target= new String("failure");
}
else
{
request.setAttribute("PRICE",price);
}
return (mapping.findForward(target));
}
}
LookupForm.java
package har;
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;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
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;
}
}
Download full source code here