Tải bản đầy đủ (.pdf) (10 trang)

Dynamic Web Pages using JSP - Lab Deliverable 12 potx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (451.85 KB, 10 trang )



Using Actions Ver 1.0 © 2005 Aptech Limited 1
Lab Deliverable 12 Using Actions

Part II

1. Write a program to display a forward action demo page. Provide a link on the demo page
to forward the output to another JSP file. Display a message to the user when the user clicks
on the link.

Solution:

The files used to run the application are:

1. index.jsp
2. Forward_Demo.jsp
3. Welcome.jsp


index.jsp

<html>
<head>
<title>Using Include Actions</title>
</head>

<body>
<h1>Using Forward Actions</h1>
<a href="forwardJsp.do">Click here for Forward Action</a>
</body>


</html>

Enter the code in Notepad and save the file as index.jsp in %TOMCAT_HOME%/webapps/
Session19.1.

//Forward_Demo.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> Sample First Document</title>
</head>
<body>
<h1>
<br>You are being forwarded to this page
</h1>
</body>
</html>

Save the file as Forward_Demo.jsp in %TOMCAT_HOME%/webapps/Session19.1.


2 Ver 1.0 © 2005 Aptech Limited JSP and Struts


//Welcome.jsp

<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>


<html:html locale="true">
<head>
<title><bean:message key="welcome.title"/></title>
<html:base/>
</head>
<body bgcolor="white">

<logic:notPresent name="org.apache.struts.action.MESSAGE"
scope="application">
<font color="red">
ERROR: Application resources not loaded check servlet
container logs for error messages.
</font>
</logic:notPresent>

<h3><bean:message key="welcome.heading"/></h3>
<p><bean:message key="welcome.message"/></p>

</body>
</html:html>

Save the file as Welcome.jsp in %TOMCAT_HOME%/webapps/Session21 directory.
Enter the path http://localhost:8080/Session19.1/index.jsp in the address bar. A welcome page
appears as shown in Figure 19.1.



Figure 19.1: Forward Action Page



Using Actions Ver 1.0 © 2005 Aptech Limited 3

Click on the link on the forward action page to see the implementation of forwardaction.
The output appears as shown in Figure 19.2.



Figure 19.2: Result Page

2. Write a program to provide three colors to the user in a drop-down list. When the user selects a
color and clicks on Submit button, dispatch the action performed by the user to another page that
will display a success message to the user. In addition display some text depending on the color
selected.

Hint: Use lookup dispatch action to dispatch the user request and display the output

Solution:

The files used to run the application are:

1. lookup.jsp
2. Dispatch_Demo.java
3. DispatchDemoActionForm.java


//lookup.jsp

<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>


<html>
<head>
<title>Using LookupDispatchAction</title>
</head>

<body>


4 Ver 1.0 © 2005 Aptech Limited JSP and Struts

<h1>Using LookupDispatchAction</h1>
<%
String msg = (String)request.getAttribute("msg");
if (msg != null && !(msg.equals("")))
{
out.println("<H1>Success</H1>The action has been
dispatched.<br>");
out.println(msg);
}
%>
<html:form action="lookupDemo.do">
Select an action to dispatch:
<html:select property="method">
<html:option value="red">Red</html:option>
<html:option value="yellow">Yellow</html:option>
<html:option value="green">Green</html:option>
</html:select>
<br><br><br>
<html:submit/>

</html:form>
</body>
</html>

Enter the code in Notepad and save the file as lookup.jsp in %TOMCAT_HOME%/webapps/
Session19.3.


//Dispatch_Demo.java

package MARKO;
import java.io.*;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.action.*;
import org.apache.struts.actions.LookupDispatchAction;

public class DispatchDemo extends LookupDispatchAction
{

public ActionForward red(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws IOException,
ServletException
{
request.setAttribute("msg","<h3>Red : Stop.<h3>");
return mapping.findForward("success");
}



Using Actions Ver 1.0 © 2005 Aptech Limited 5

public ActionForward yellow(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws IOException,
ServletException
{
request.setAttribute("msg","<h3>Yellow : Caution.<h3>");
return mapping.findForward("success");
}

public ActionForward green(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws IOException,
ServletException
{
request.setAttribute("msg","<h3>Green : Go.<h3>");
return mapping.findForward("success");
}
protected Map getKeyMethodMap()
{
Map map = new HashMap();
map.put("MARKO.red", "red");
map.put("MARKO.yellow", "yellow");
map.put("MARKO.green", "green");
return map;
}
}


Save the file as Dispatch_Demo.jsp in the TOMCAT_HOME%/webapps/Session19.3.

//DispatchDemoActionForm.java

package MARKO;
import org.apache.struts.action.ActionForm;

public class DispatchDemoActionForm extends ActionForm
{
private String method = "";
public String getmethod()
{
return method;
}
public void setMethod(String method)
{
this.method = method;
}
}



6 Ver 1.0 © 2005 Aptech Limited JSP and Struts

Save the file as DispatchDemoActionForm.java in
%TOMCAT_HOME%/webapps/Session21 directory. Compile the code using command
prompt. Enter the path http://localhost:8080/Session19.1/lookup.jsp in the address bar. The output
appears as shown in Figure 19.3.




Figure 19.3: Lookup Dispatch Action Page

Select a color from the drop down list and click on Submit button. The output appears as shown
in Figure 21.4.



Using Actions Ver 1.0 © 2005 Aptech Limited 7


Figure 19.4: Result Page

Do It Yourself

1. Create a Web page to provide various products such as banking, Debit cards, and credit Cards
to the user. The page should allow the user to select a product. Display the details about the
selected product to the user.

Solution:

The filenames used to run this application are:

1. index.jsp
2. displayOptions.jsp



8 Ver 1.0 © 2005 Aptech Limited JSP and Struts



//index.jsp

<html>
<head>
<title>MARKO</title>
</head>

<body bgcolor="white">
<h3>MARKO Bank</h3>
<BR><BR><BR><BR>
<form method="post" action="displayOptions.do">
Please select a product from the list:<BR><br><BR>
<select name=select>
<option value="0">Select a Product</option>
<option value="Banking">Banking</option>
<option value="Credit Cards">Credit Cards</option>
<option value="Debit Cards">Debit Cards</option>
</select>
<br><br><br>
<INPUT TYPE="Submit">
</form>
</html>


//displayOptions.jsp
<html>
<head>
<title>MARKO Options Page</title>

</head>

<%
String options = request.getParameter("select");
if ((options == null)||(options.equals("")))
{
%>

<jsp:forward page="/index.jsp"/>

<%
}
if (options.equals("0"))
{
%>

<jsp:forward page="/index.jsp"/>

<%
}


Using Actions Ver 1.0 © 2005 Aptech Limited 9
if (options.equals("Banking"))
{
%>

<h2>Banking</h2>
<P>
MARKO is Worlds largest bank having a network of about 10,000

branches and extension counters are over 100,000 ATMs. MARKO
offers a wide range of banking products and financial services
to corporate and retail customers through a variety of
delivery channels.
</P>

<%
}
if (options.equals("Credit Cards"))
{
%>

<h2>Credit Cards</h2>
<P>
MARKO Bank allows you to compare various credit card offers,
select the offer that matches your requirements and apply
online via the web site. This certainly beats the alternative
of sifting through direct mail.
</P>

<%
}
if (options.equals("Debit Cards"))
{
%>

<h2>Debit Cards</h2>
<P>
MARKO Bank allows you to compare various Debit card offers,
select the offer that matches your requirements and apply

online via the web site. This certainly beats the alternative
of sifting through direct mail.
</P>

<%
}
%>
</body>
</html>

Enter the path http://localhost:8080/Session19_4/index.jsp in the address bar. The output
appears as shown in Figure 19.5.



10 Ver 1.0 © 2005 Aptech Limited JSP and Struts



Figure 19.5: Submit Query Page

Select a product, such as Debit Card from the drop down list, and click the Submit Query button.
The output appears as shown in Figure 19.6.



Figure 19.6: Product Detail Page

×