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

Apress bắt đầu ứng dụng với java google - p 8 doc

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 (940.22 KB, 10 trang )

CHAPTER 4 ■ SERVLET CONTAINER AND FRAMEWORKS
48
<input type="hidden" name="action" value="accountLookup"/>
<span class="heading">Search by Account Name:</span>
<p/>
<input type="text" name="accountName" value="ACME"
style="width: 300px"/>
&nbsp
<input type="submit" value="Search"/>
&nbsp
</form>
<p/>

</body>
</html>

If the account does not exist, users can create a new account using a standard
HTML form, as shown in Figure 4-2. You are simply collecting some summary
information in order to identify the account. Listing 4-2 contains the code for your
input form.

Figure 4-2. The Create a New Account web page
CHAPTER 4 ■ SERVLET CONTAINER AND FRAMEWORKS
49
Listing 4-2. The code for accountCreate.jsp
<html>
<head>
<title>Telesales Demo (Google App Engine for Java)</title>
<link rel="stylesheet" type="text/css"
href="/stylesheets/styles.css"/>
</head>


<body>
<span class="nav"><a href="index.html">Back</a></span><p/>
<span class="title">Create a New Account</span>
<p/>

<form method="get" action="telesales">
<input type="hidden" name="action" value="accountCreateDo"/>
<table border="0" cellspacing="1" cellpadding="5" bgcolor="#CCCCCC">
<tr bgcolor="#407BA8">
<td style="color: #ffffff; font-weight: bold;">Name</td>
<td bgcolor="#ffffff"><input type="input"
name="name"></td>
</tr>
<tr bgcolor="#407BA8">
<td style="color: #ffffff; font-weight: bold;">City</td>
<td bgcolor="#ffffff"><input type="input"
name="billingCity"></td>
</tr>
<tr bgcolor="#407BA8">
<td style="color: #ffffff; font-weight:
bold;">State</td>
<td bgcolor="#ffffff"><input type="input"
name="billingState"></td>
</tr>
<tr bgcolor="#407BA8">
<td style="color: #ffffff; font-weight:
bold;">Phone</td>
<td bgcolor="#ffffff"><input type="input"
name="phone"></td>
</tr>

<tr bgcolor="#407BA8">
<td style="color: #ffffff; font-weight:
bold;">Website</td>
CHAPTER 4 ■ SERVLET CONTAINER AND FRAMEWORKS
50
<td bgcolor="#ffffff"><input type="input"
name="website"></td>
</tr>
<tr>
<td colspan="2" bgcolor="#ffffff" align="center"><input
type="submit" value="Submit"></td>
</tr>
</table>
</form>

</body>
</html>

The search results page (Figure 4-3) displays the accounts returned from the servlet.
Listing 4-3 contains the code to display the search box and any results returned from
Bigtable by the keyword search. Users can click the account name to obtain more
details about the account.

Figure 4-3. The account lookup web page
CHAPTER 4 ■ SERVLET CONTAINER AND FRAMEWORKS
51
Listing 4-3. The code for accountLookup.jsp
<%@ page import="java.util.List"%>
<%@ page import="com.appirio.entity.*"%>


<%
List<Account> accounts =
(List<Account>)request.getAttribute("accounts");
%>

<html>
<head>
<title>Google App Engine Servlet Example with Bigtable</title>
<link rel="stylesheet" type="text/css"
href="/stylesheets/styles.css"/>
</head>
<body>
<span class="title">Google App Engine Servlet Example with
Bigtable</span>
<p/>
<p>Before creating a new Opportunity, ensure that the Account does
not already exist. You can also <a
href="telesales?action=accountCreate"/>create a new account</a>.</p>
<p/>
<form method="post" action="telesales">
<input type="hidden" name="action" value="accountLookup"/>
<span class="heading">Search by Account Name:</span>
<p/>
<input type="text" name="accountName" value="<% if
(request.getParameter("accountName") != null) {
out.println(request.getParameter("accountName")); } %>" style="width:
300px"/>
&nbsp
<input type="submit" value="Search"/>
&nbsp

</form>
<p/>
<% if (accounts.size() > 0) { %>
<span class="heading"><%= accounts.size() %> accounts matching
your search criteria:</span>

<p/>
CHAPTER 4 ■ SERVLET CONTAINER AND FRAMEWORKS
52
<table border="0" cellspacing="1" cellpadding="5"
bgcolor="#CCCCCC" width="50%">
<tr bgcolor="#407BA8">
<td style="color: #ffffff; font-weight: bold;">Name</td>
<td style="color: #ffffff; font-weight: bold;">City</td>
<td style="color: #ffffff; font-weight:
bold;">State</td>
<td style="color: #ffffff; font-weight:
bold;">Phone</td>
</tr>
<% for (int i = 0;i<accounts.size();i++) { %>
<% Account a = (Account)accounts.get(i); %>
<tr style="background:#ffffff"
onMouseOver="this.style.background='#eeeeee';"
onMouseOut="this.style.background='#ffffff';">
<td><a
href="telesales?action=accountDisplay&accountId=<%= a.getId() %>"><%=
a.getName() %></a></td>
<td><%= a.getCity() %></td>
<td><%= a.getState() %></td>
<td><%= a.getPhone() %></td>

</tr>
<% } %>
</table>

<% } else { %>
<span class="heading">No matching accounts found.</span>
<% } %>
<p/>

</body>
</html>

The Account Display view (Figure 4-4) shows the details of the account, a link to
create a new sales opportunity, and a list of all opportunities for the account in
Bigtable. Listing 4-4 contains the code for this page.
CHAPTER 4 ■ SERVLET CONTAINER AND FRAMEWORKS
53

Figure 4-4. The Account Display web page
Listing 4-4. The code for accountDisplay.jsp
<%@ page import="java.util.List"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="com.appirio.entity.*"%>

<%
Account account = (Account)request.getAttribute("account");
List<Opportunity> opportunities =
(List<Opportunity>)request.getAttribute("opportunities");
SimpleDateFormat sdf = new SimpleDateFormat("M/d/yyyy");
%>


<html>
<head>
<title>Telesales Demo (Google App Engine for Java)</title>
<link rel="stylesheet" type="text/css"
href="/stylesheets/styles.css"/>
</head>
CHAPTER 4 ■ SERVLET CONTAINER AND FRAMEWORKS
54
<body>
<span class="nav"><a href="/index.html">Search</a></span><p/>
<span class="title">Account Display</span>
<p/>

<table border="0" cellspacing="1" cellpadding="5" bgcolor="#CCCCCC">
<tr bgcolor="#407BA8">
<td style="color: #ffffff; font-weight: bold;">Name</td>
<td bgcolor="#ffffff"><%= account.getName() %></td>
</tr>
<tr bgcolor="#407BA8">
<td style="color: #ffffff; font-weight: bold;">City</td>
<td bgcolor="#ffffff"><%= account.getCity() %></td>
</tr>
<tr bgcolor="#407BA8">
<td style="color: #ffffff; font-weight:
bold;">State</td>
<td bgcolor="#ffffff"><%= account.getState() %></td>
</tr>
<tr bgcolor="#407BA8">
<td style="color: #ffffff; font-weight:

bold;">Phone</td>
<td bgcolor="#ffffff"><%= account.getPhone() %></td>
</tr>
<tr bgcolor="#407BA8">
<td style="color: #ffffff; font-weight:
bold;">Website</td>
<td bgcolor="#ffffff"><%= account.getWebsite() %></td>
</tr>
</table>

<br><a href="telesales?action=opportunityCreate&accountId=<%=
account.getId() %>">Create a new Opportunity</a><p/>

<% if (opportunities.size() > 0) { %>

<p/><span class="heading">Opportunities for <%=
account.getName() %></span><br><p/>

<table border="0" cellspacing="1" cellpadding="5"
bgcolor="#CCCCCC">
<tr bgcolor="#407BA8">
CHAPTER 4 ■ SERVLET CONTAINER AND FRAMEWORKS
55
<td style="color: #ffffff; font-weight: bold;">Name</td>
<td style="color: #ffffff; font-weight:
bold;">Amount</td>
<td style="color: #ffffff; font-weight:
bold;">Stage</td>
<td style="color: #ffffff; font-weight:
bold;">Probability</td>

<td style="color: #ffffff; font-weight: bold;">Close
Date</td>
<td style="color: #ffffff; font-weight:
bold;">Order</td>
</tr>
<% for (int i = 0;i<opportunities.size();i++) { %>
<% Opportunity o = (Opportunity)opportunities.get(i); %>
<tr style="background:#ffffff"
onMouseOver="this.style.background='#eeeeee';"
onMouseOut="this.style.background='#ffffff';">
<td nowrap><%= o.getName() %></td>
<td>$<%= o.getAmount() %></td>
<td><%= o.getStageName() %></td>
<td><%= o.getProbability() %>%</td>
<td><%= sdf.format(o.getCloseDate()) %></td>
<td><%= o.getOrderNumber() %></td>
</tr>
<% } %>
</table>

<% } else { %>
<p/><span class="heading">No Opportunities found for <%=
account.getName() %></span>
<% } %>

</body>
</html>

Listing 4-5 contains the code for the Create New Opportunity page, which is shown
in Figure 4-5. This HTML form collects the name of the opportunity, the anticipated

amount of the opportunity, and some additional attributes. Submitting the form
creates the new opportunity in Bigtable and takes users back to the Account Display
page where they can view the newly created opportunity (Figure 4-6).
CHAPTER 4 ■ SERVLET CONTAINER AND FRAMEWORKS
56

Figure 4-5. The Create a New Opportunity web page
Listing 4-5. The code for opportunityCreate.jsp
<%
String accountName = (String)request.getAttribute("accountName");
%>

<html>
<head>
<title>Telesales Demo (Google App Engine for Java)</title>
<link rel="stylesheet" type="text/css"
href="/stylesheets/styles.css"/>
</head>
<body>
<span class="nav"><a
href="telesales?action=accountDisplay&accountId=<%=
request.getParameter("accountId") %>">Back</a></span><p/>
<span class="title">Create a New Opportunity</span>
<p/>

<form method="post"
action="telesales?action=opportunityCreateDo&accountId=<%=
request.getParameter("accountId") %>">
CHAPTER 4 ■ SERVLET CONTAINER AND FRAMEWORKS
57

<input type="hidden" name="accountId" value="{{accountId}}">
<table border="0" cellspacing="1" cellpadding="5" bgcolor="#CCCCCC">
<tr bgcolor="#407BA8">
<td style="color: #ffffff; font-weight:
bold;">Account</td>
<td bgcolor="#ffffff"><%= accountName %></td>
</tr>
<tr bgcolor="#407BA8">
<td style="color: #ffffff; font-weight: bold;">Name</td>
<td bgcolor="#ffffff"><input type="input" name="name"
style="width:250px"></td>
</tr>
<tr bgcolor="#407BA8">
<td style="color: #ffffff; font-weight:
bold;">Amount</td>
<td bgcolor="#ffffff"><input type="input" name="amount"
value="125.25"></td>
</tr>
<tr bgcolor="#407BA8">
<td style="color: #ffffff; font-weight:
bold;">Stage</td>
<td bgcolor="#ffffff">
<select name="stageName">
<option>Prospecting</option>
<option>Qualifications</option>
<option>Value Proposition</option>
</select>
</td>
</tr>
<tr bgcolor="#407BA8">

<td style="color: #ffffff; font-weight:
bold;">Probability</td>
<td bgcolor="#ffffff">
<select name="probability">
<option value="10">10%</option>
<option value="25">25%</option>
<option value="50">50%</option>
<option value="75">75%</option>
</select>
</td>
</tr>
<tr bgcolor="#407BA8">

×