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

WebSphere Studio Application Developer Version 5 Programming Guide part 27 pdf

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

234 WebSphere Studio Application Developer Version 5 Programming Guide
Figure 7-49 Creating a new life-cycle listener (page 2)
All the default values are suitable, just click
Finish
to complete the wizard. The
listener is created and added to the Web deployment descriptor. A Java editor is
also opened on the listener class Java file.
Editing the listener
Switch to the Java editor that was opened on the HttpSessionInspector.java
file. The source code of the sample listener is available in:
\sg246957\sampcode\dev-web\listener\HttpSessionInspector.java
The first method we will enter calculates an object’s size when serialized. This
method is useful for determining how much space a session or one of its
attributes would take if they would be serialized to a database. Add the size
method as shown in Figure 7-50.
Chapter 7. Developing Web applications 235
Figure 7-50 Listener helper method: size
The next helper method that has to be created is called report. It handles the
printing of messages to the standard output (Figure 7-51).
Figure 7-51 Listener helper method: report
Now enter the life-cycle methods for the HttpSessionListener interface
(Figure 7-52).
Figure 7-52 Listener implementation for the HttpSessionListener interface
Finally, enter the methods for the HttpSessionAttributeListener interface
(Figure 7-53).
private int size (Object anObject) {
ByteArrayOutputStream array = new ByteArrayOutputStream ();
try {
(new ObjectOutputStream (array)).writeObject(anObject);
return array.size();
} catch (IOException e) {


return -1;
}
}
private void report (String reason, HttpSession session, Object value,) {
if (value == null)
System.out.println (reason + ": session #" + session.hashCode() + "
size: " + size(session));
else
System.out.println (reason + ": session #" + session.hashCode() + "
size: " + size(session) + " Attribute size: " + size(value));
}
public void sessionCreated(HttpSessionEvent arg0) {
report ("Session created", arg0.getSession(), null);
}
public void sessionDestroyed(HttpSessionEvent arg0) {
report ("Session destroyed", arg0.getSession(), null);
}
236 WebSphere Studio Application Developer Version 5 Programming Guide
Figure 7-53 Listener implementation of the HttpSessionAttributeListener interface
To get rid of all the errors from undefined classes, select
Source -> Organize
Imports
and the required import statements are added to the code.
Save your changes and close the editor. Run the application to see the results of
your work. The listener’s messages should show up on the Console view.
Figure 7-54 contains an extract from the Console showing the output of both the
filter and the listener.
Figure 7-54 Sample output of filter and listener
public void attributeRemoved(HttpSessionBindingEvent arg0) {
report ("Attribute " + arg0.getName() + " removed",

arg0.getSession(), arg0.getValue());
}
public void attributeAdded(HttpSessionBindingEvent arg0) {
report ("Attribute " + arg0.getName() + " added",
arg0.getSession(), arg0.getValue());
}
public void attributeReplaced(HttpSessionBindingEvent arg0) {
report ("Attribute " + arg0.getName() + " replaced",
arg0.getSession(), arg0.getValue());
}
Session created: session #1152644455 size: 226
Attribute customerNumber added: session #1152644455 size: 249
Attribute size: 10
Attribute accountNumber added: session #1152644455 size: 276
Attribute size: 15
- Deposit being performed from 127.0.0.1 (127.0.0.1) using HTTP/1.1 at
Sun Apr 06 15:23:02 PDT 2003 Parameters: amount=100, transaction=Deposit
- Transfer being performed from 127.0.0.1 (127.0.0.1) using HTTP/1.1 at
Sun Apr 06 15:23:14 PDT 2003 Parameters: destinationAccount=106-6002,
amount=200, transaction=Transfer
Attribute accountNumber replaced: session #1152644455 size: 276
Attribute size: 15
- ListTransactions being performed from 127.0.0.1 (127.0.0.1) using
HTTP/1.1 at Sun Apr 06 15:23:24 PDT 2003 Parameters:
transaction=ListTransactions
Invalidating session
Session destroyed: session #1152644455 size: 276
Chapter 7. Developing Web applications 237
Creating Web pages from a JavaBean
Application Developer provides support for generating a working set of pages

and Java classes from a simple JavaBean or a set of JavaBeans. A JavaBean, in
this context, is any Java class that has a public constructor and public getters and
setters for its properties.
As we have seen before, JavaBeans usually either implement or represent the
business model tier. The JavaBean wizard will generate the view and control
layers to work with the JavaBeans you select, in accordance to the MVC
architectural pattern.
The JavaBean Web Pages Wizard supports the following activity models:
 Set bean properties—Create an input form that collects input from users and
stores the resulting data within the properties of a Java bean.
 Retrieve bean properties—Create a result form that displays a bean's current
property values.
 Execute a bean's method(s)—Run any number of methods from a bean by
submitting an input form.
 Execute a bean's method(s) with parameters—Create an input form that
collects data from users, and use this data as input to a bean's methods.
 Display a method's result—Create a result page that displays the return value
of a method.
 Combination—Create a set of pages that include any combination of the
above models.
For the sole purpose of demonstrating the wizard, we are going to redo the same
two pages and one servlet we did before (index.html, listAccounts.jsp and
ListAccounts). You will then be able to compare the two approaches.
We create the application in the ItsoProGuideBasicWeb project.
To better organize your code, create a new folder under Web Content named jbwp
(as in JavaBean Web pages).
Important: The Java classes you select must have zero-argument
constructors, as per the JavaBeans specification, or the resulting generated
code will fail to work correctly.
238 WebSphere Studio Application Developer Version 5 Programming Guide

Select the newly created jbwp folder and select
File -> New -> Other
from the
menu bar. Select the
Web -> Java Bean Web Pages
option and click
Next
to
continue. This opens the wizard’s first page, as shown in Figure 7-55.
Figure 7-55 JavaBean Web pages wizard: start
 The
Destination folder
field should have come up already filled in, since you
had the folder selected. In the
Java package
field, type itso.jbwp. We do not
want the code mixed with the old application.
 The model selection drop-down currently only has one entry:
View Bean
. This
code generation model uses view helpers, which are Java wrapper classes
that manage all the model interaction
.
 The rest of the dialog is simply informative. Selecting the
Files
field values
displays a description.
Chapter 7. Developing Web applications 239
Click
Next

to proceed to the next page (Figure 7-56).
Figure 7-56 JavaBean Web pages wizard: select bean and methods
 The second step consists of selecting the JavaBean that will act as the model
for the generated pages, and selecting which of its properties and methods
the pages should be created for.
 Click
Browse
to bring up the
Choose Java Bean
dialog. Type Banking in the
Choose Java Bean field. The dialog presents you with the matching types,
but the list is probably going to be limited to just one occurrence. Select the
appropriate type (Banking) and package (itso.bank.facade) and click
OK
.
 The wizard automatically introspects the bean and presents you with the list
of available properties and methods. You do not have to click
Introspect

unless you type in the class name directly.
 Select the getCustomer and getAccounts methods. We only want to retrieve
the customer and its accounts.
240 WebSphere Studio Application Developer Version 5 Programming Guide
Click
Next
to continue to the next page (Figure 7-57).
Figure 7-57 JavaBean Web pages wizard: controller, view beans
 The third wizard’s page lets you select the style sheets you want associated
with the generated pages. By default, Web Content\theme\Master.css comes
selected, and that is enough for us. You may add, remove, or change the

priority of style sheets by clicking
Add
,
Remove
, and the arrow buttons,
respectively.
 Here you may also select whether or not the wizard should link the given error
page to the generated pages. Note that the wizard will not generate the error
page. It will just declare it in the generated pages’ page directive for you. We
do not need that behavior.
 The default setting for storing the results is
Request
, the option that you
should select for this example. Selecting
Request
means that the method
results will only be available for the duration of the HTTP request. If you want
the results to be preserved for the lifetime of the user session, choose the
Session
option. If the results occupy too much memory, this may not be the
best approach.
 You can then choose how you would like the wizard to generate control layer
code for you. You may want to generate a new controller servlet, to reuse an
existing one, or not to use a controller servlet at all (in which case the wizard
would generate view and control code in the JSPs). Select the
Create a new
Front Controller
option.
Chapter 7. Developing Web applications 241
 While you are still on this page, you can choose to have the wizard create a

view bean wrapper for you. This wrapper class allows you to format the result
data for display.
If you deselect this option, the wizard accesses data using
the previously chosen bean directly. For our simple example, we do not want
a wrapper.
Click
Next
to proceed to the fourth page (Figure 7-58).
Figure 7-58 JavaBean Web pages wizard: input page
This page lets you see and configure how the generated input HTML file will look
like:
 Select the getCustomer method and clear its
Label
property.
 Select the customerID item under the getCustomer method and configure its
label to Enter a customer number:
 Deselect the getAccounts method and customerID under it, we only want one
input field for the customer number.
 Select the
Page
tab and set the Page Title field to RedBank.
242 WebSphere Studio Application Developer Version 5 Programming Guide
Click
Next
to proceed to the fifth page (Figure 7-59).
Figure 7-59 JavaBean Web pages wizard: results page
Using this page you can determine and configure how the response JSP will
look:
 Use the arrows to move getAccounts under getCustomer (if the sequence is
reversed).

 Select the getCustomer method and clear its
Label
property.
 Select the getCustomer result (right under the getCustomer method) and set
its
Label
property to Customer:.
 Expand the getCustomer method and select both firstName and lastName
properties. Select each field and change the labels to First name: and Last
name:.
 Expand the getAccounts method, select it, and clear its label.
 Select the getAccounts under it and set its label to Accounts: and its layout
style to
table
(use the drop-down menu).
Chapter 7. Developing Web applications 243
 The getAccounts result is an array of Account objects. Expand the
getAccounts result.
 The properties of the Account class are now displayed under getAccounts.
Check the id and balance fields.
 Select the id and balance fields and change the labels to Account number and
Balance.
 Configure the page title to RedBank: Customer's Accounts.
Click
Next
to skip to the wizard’s last page (Figure 7-60).
Figure 7-60 JavaBean Web pages wizard (page 6)
Finally, you have to configure a prefix to be appended before every generated
resource. Simply type ListAccounts and click
Finish

to complete the wizard.
The following resources were generated:
 A ListAccountsInputForm.html file in the Web Content\jbwp folder.
 A ListAccountsResultsForm.jsp file in the Web Content\jbwp folder.
 A ListAccountsController servlet in the itso.jbwp package. This servlet has
also been registered in the Web module’s deployment descriptor.
Completing the code
Because we only generated one input field for the customer ID, the call to the
getAccounts method has no parameter. We have to add the parameter manually.
Open the ListAccountsResultsForm.jsp file and find the method calls. Change
the getAccounts call:
itso.bank.model.Customer methodResult0 = listAccountsBean.getCustomer
(new java.lang.String(request.getParameter

×