|
Creating Standards-Based J2EE Portlets
Creating Standards-Based J2EE Portlets
This tutorial shows you how to build and deploy a JSR 168
portlet by using the Java Portlet Wizard in JDeveloper.
Approximately 1 hour
This tutorial covers the following topics:
Place the cursor over this icon to load and view all
the screenshots for this tutorial. (Caution: This action loads all screenshots
simultaneously, so response time may be slow depending on your Internet connection.)
Note: Alternatively, you can place the cursor over
each individual icon in the following steps to load and view only the screenshot
associated with that step.
The screenshots are of Oracle Portal running on Windows 2000
Professional. If you are running a different operating system, your windows
will look somewhat different.
Organizations that engage in enterprise portal projects have
found application integration to be a major issue. Until now, users have developed
portlets using proprietary APIs for a particular portal platform and have often
faced a shortage of portlets available from a specific portal vendor. All this
is changing with the introduction of the following standards:
 |
Web Services for Remote Portlets (WSRP) |
 |
Java Portlet Specification (JPS) based
on Java Specification Request (JSR 168) |
These two standards enable the development of portlets that
interoperate with different portal products and therefore widen the availability
of portlets within an organization. This wider availability can, in turn, dramatically
increase an organization's productivity when building enterprise portals.
WSRP is a Web services standard that enables
plug-and-play of visual, user-facing Web services with portals or other intermediary
Web applications. It defines the communication protocol between a portal application
and the remote portlet. WSRP, being a standard, enables interoperability between
WSRP producers and any WSRP portal.
JPS is a specification that defines a set
of APIs to enable interoperability between portlets and portals, addressing
the areas of aggregation, personalization, presentation, and security. JSR 168
defines how Java developers should write portlets using standard APIs.
Oracle Portal fully supports the JSR 168 portlet specification.
You can develop JSR 168 portlets using the Java Portlet Wizard, a JDeveloper
plug-in. The OracleAS Java Portlet Container enables you to build and run interoperable
Java portlets quickly and easily. The container provides a runtime environment
for Java portlets coded to the standard JPS (based on JSR 168) APIs that enables
the portlets to be utilized by any portal supporting the OASIS WSRP standards.
Portlets deployed to Oracle's container are exposed to portals through WSRP
and can be registered against any portal vendor's WSRP-compliant portal.
The diagram below provides an overview of the development,
deployment, and page request process of a JSR 168 portlet:
| 1. |
You build your producer and portlets using
Oracle JDeveloper and then deploy the producer to the Java Portlet Container. |
| |
You register the producer with Oracle
Portal (not shown in the diagram). |
| 2. |
Using a Web browser, you request the portal
page containing your portlet. |
| 3. |
Oracle Portal sends a request to the producer,
asking for the portlet content. |
| 4. |
The portlet generates its content and the
producer returns it to Oracle Portal. |
| 5. |
Oracle Portal assembles the portal page
and returns it to the browser. |
Oracle Application Server Containers for J2EE (OC4J) is the
J2EE server component of the Oracle Application Server. A producer (also called
a provider) is the communication link between Oracle Portal and a portlet; each
portlet communicates with Oracle Portal through its producer.
In this tutorial, you have the opportunity to:
 |
Use Oracle JDeveloper to create
and deploy a JSR 168 Java portlet. |
 |
Create an application server connection
to an OC4J container. |
 |
Register a portlet producer with
Oracle Portal. |
 |
Add your Java portlet to a portal
page. |
 |
Enhance and redeploy the JSR
168 Java portlet. |
Back to Topic
List
In this tutorial, you use Oracle JDeveloper with the OracleAS
Portal Add-In to create and deploy a JSR 168 Java portlet. You begin by creating
a JDeveloper workspace and project. Then you use the Java Portlet Wizard to
generate a simple, JPS-compatible Java portlet. You create a deployment profile
for the Web application that contains your simple Java portlet. Using the deployment
profile, you deploy the Web application and portlet to an OC4J container. You
then register the portlet provider with a WSRP-enabled Oracle Portal instance
and add the portlet to a portal page. In JDeveloper, you enhance the portlet
behavior and make some changes to the portlet source code. Finally, you redeploy
the Web application containing the enhanced portlet.
Back to
Topic List
Before starting this tutorial, you should:
 |
Have installed Oracle JDeveloper 10.1.2.0. |
 |
Have installed the Java Portlet Container (JSR 168) to build portlets based on portlet standards.
Note: Follow the instructions in the 10.1.4 Portal Developer's Guide (Section 6.3), available on the Oracle Technology Network, to install the portlet container. |
 |
Have installed the Portal
Extension for Oracle JDeveloper to build portlets based on portlet
standards or PDK APIs
Note: Follow the instructions in the Portal Extension for Oracle JDeveloper Installation Guide. |
 |
Have access to or have installed Oracle Portal 10.1.4. |
 |
Have access to an Oracle Database Server
(for example, the OracleAS Infrastructure Metadata Repository database) |
 |
Have access to an
Internet browser such as Mozilla Firefox or Microsoft Internet Explorer (IE).
|
Back to Topic
List
Oracle JDeveloper uses a well-defined structure to manage
Java applications. When you work in JDeveloper, you organize your work in projects
within application workspaces. JDeveloper provides several templates that you
can use to create a workspace and projects.
A workspace is the highest level in the control structure.
A workspace keeps track of the projects and environment settings while you develop
a Java program.
A JDeveloper project organizes the source files, packages,
classes, images, and other elements needed by your program.
In this section, you create a JDeveloper workspace and project.
To create a new workspace with an empty project in JDeveloper,
perform the following steps:
| 1. |
Double-click the JDeveloper 10g
icon on your Desktop to open the JDeveloper environment.

|
| 2. |
When JDeveloper is displayed, ensure that
the Applications tab of the Application Navigator is the active
tab. 
|
| 3. |
In the Application Navigator, right-click
the Applications node and select New from the context menu
to launch the New Gallery containing JDeveloper wizards. 
|
| 4. |
From the General
Categories list, select Workspaces. 
|
| 5. |
In the Items list, select the Workspace
item.
Note: Your screen might differ from
the image shown below.

|
| 6. |
Click OK.

|
| 7. |
In the Create Workspace
dialog, enter the following details:
| Field |
Values |
| Workspace Name |
<Your_Name>_OWHO
|
| Directory Name |
D:\OWHO\<Your_Name_OWHO> |
Note: If the directory does
not exist, JDeveloper will create it.

|
| 8. |
Ensure the Add a
New Empty Project checkbox is selected and click OK.

|
| 9. |
In the Create Project dialog,
enter the following details:
| Field |
Values |
| Project Name |
<Your_Name> |
| Directory Name |
D:\owho\<Your_Name_OWHO>\<Your_Name> |

|
| 10. |
Click OK.

|
Back to Topic
List
With the Oracle Portal Extension for Oracle JDeveloper, you
can create, test, and deploy Java Portlets from Oracle JDeveloper 10g.
The Portal Extension for Oracle JDeveloper provides two wizards: Java Portlet
and Oracle PDK Java Portlet. Both wizards guide you through the steps of creating
the portlet skeleton. After running the appropriate wizard, all you need to do is implement your business logic.
The Java Portlet Wizard allows you to build portlets based on the standards
(that is, WSRP, JSR 168). The Oracle PDK Java Portlet Wizard allows you to build
portlets based on the PDK APIs.
The Oracle JDeveloper 10g Portal Add-ins reduce the amount
of work required to wrap your business logic so you can concentrate on implementing
the necessary functionality. Using the Java Portlet Wizard dramatically increases
your flexibility and productivity when you are developing Java portlets.
In this section, you use JDeveloper's Java Portlet Wizard
to create a simple JPS-compliant Java portlet.
To create a simple Java portlet in JDeveloper, perform the
following steps:
| 1. |
Right-click <Your_Name> project and select New
from the context menu.

|
| 2. |
In the Categories list, expand
the Web Tier node and click Portlets.

|
| 3. |
In the Items list, ensure
that Java Portlet is selected and click OK.

The Java Portlet Wizard is displayed. This wizard helps
you to create JPS-compliant Java portlets.
|
| 4. |
If you are on the Welcome
page of the wizard, click Next.

|
| 5. |
On the General Portlet Properties
page, leave the default values, as indicated below:
| Field |
Value |
| Class |
Portlet1> |
| Package |
mypackage1 |
| Default Language |
English |
| Customizable |
(selected) |

|
| 6. |
Click Next.

|
| 7. |
On the Name and Attributes
page, enter the values as specified below:
| Field |
Value |
| Display Name |
WelcomePortlet |
| Portlet Title |
Welcome to OOW |
| Short Title |
Welcome |
| Description |
This portlet displays a welcome message to OOW attendees. |
| Keywords |
welcome, Oracle Open
World, OOW |
Keywords provide additional information about a page
or item so that users can easily locate the page or item during a search.

|
| 8. |
Click Next.

|
| 9. |
On the
Content Types and Portlet Modes page, notice that text/html is
the default content type. View and edit are listed as default portlet
modes for text/html.
Note: Edit mode is listed because Customizable
was selected on the General Portlet Properties page.

|
| 10. |
Click view
and verify that the Generate JSP option is selected as the implementation
method.

|
| 11. |
Click edit
and verify that the Generate JSP option is selected as the implementation
method.

|
| 12. |
Click Finish.

Although you are on step 3 of 7, you have defined everything
you need for this simple portlet.
|
| 13. |
Look in
the Application Navigator for the files that the Java
Portlet Wizard generated.

|
Back to Topic
List
In this section, you use JDeveloper to create an application
server connection. The connection is necessary to deploy your JPS-compliant
Java portlet to an OC4J container.
To create an application server connection, perform the following
steps:
| 1. |
In the
Application Navigator, click the Connections tab.

|
| 2. |
Right-click
Application Server and choose New Application
Server Connection from the context menu.

|
| 3. |
If the
Create Application Server Connection - Welcome page appears, click Next.

|
| 4. |
In Step 1 of the wizard, specify
the following connection details:
| Field |
Value |
| Connection Name |
PDKStandardsOC4J |
| Connection Type |
Oracle
Application Server 10G |

|
| 5. |
Click Next.

|
| 6. |
In Step
2 of the wizard, provide the authentication details that will be used
for deployment:
| Field |
Value |
| Username |
ias_admin |
| Password |
oracle1 |
Note: The password for ias_admin on your application server will be (or at least, should be) different from the one in this example.

|
| 7. |
Click Next.

|
| 8. |
In Step 3 of the wizard, provide
the appropriate values for the connection details for your application server environment. For example:
| Field |
Value |
| Enterprise Manager
OC4J Host Name |
localhost |
| Enterprise Manager
OC4J HTTP Port |
1811 |
| Remote Server's
Oracle Home Directory |
C:\MIDTIER_10G |
| OC4J Instance Name
|
home |

|
| 9. |
Click Next.
In step 4 of the Wizard, click Next again.

|
| 10. |
In step
5 of the wizard, click Test Connection to test the OC4J connection.

It may take a few moments to establish the connection.
|
| 11. |
Look for
the Success! message in the Status field.

|
| 12. |
Click Finish.

|
Back to Topic
List
In this section, you create a deployment profile for the
Web application that contains your simple Java portlet. Then you use the deployment
profile to deploy your Java portlet to a Java Portlet Container.
To deploy your Java portlet to an OC4J container, perform
the following steps:
| 1. |
Return
to the Applications tab in the Navigator.

|
| 2. |
Right-click
the <Your_Name> project and
select New from the context menu.

|
| 3. |
In the
Categories section, expand General and click Deployment Profiles.

|
| 4. |
In the
Items section, select WAR File.

|
| 5. |
Click OK.

|
| 6. |
In the Create Deployment Profile
- WAR File dialog box, enter the following details:
| Field |
Value |
| Deployment Profile
Name |
<YourName>Portlets |
| Directory |
D:\owho\<Your_Name>_OWHO\<Your_Name> |

|
| 7. |
Click OK.

The WAR Deployment Profile Properties window is displayed.
|
| 8. |
Click Specify
J2EE Web Context Root and enter <YourName>Portlets.

|
| 9. |
Click OK.

|
| 10. |
Look for
the <YourName>Portlets.deploy
file displayed below the Resources node in the Application Navigator.

|
| 11. |
Right-click
the <YourName>Portlets.deploy deployment profile and select
Deploy to > PDKStandardsOC4J.

|
| 12. |
Wait a
couple of minutes for the Deployment finished message
in the Deployment Log at the bottom of the Oracle JDeveloper window to
verify that the deployment succeeded.

|
| 13. |
Open another
browser window or tab. Enter a URL similar to the one below:
http://localhost/<YourName>Portlets/portlets?WSDL where
YourName is your name.
For example:

The browser displays the WSDL through which the portal
can identify the producer and communicate with it:

|
After you create and deploy your producer and its portlets,
you need to register the producer with Oracle Portal so that Oracle Portal can
locate and execute the portlet. Registering a producer gives Oracle Portal the
information it needs to access and communicate with your producer. In this section,
you register your portlet provider with a WSRP-enabled Oracle Portal.
Note: Oracle Portal uses the term provider,
instead of producer.
If you are already logged in, start with step 5.
To register your Java portlet provider with Oracle Portal,
perform the following steps:
| 1. |
Open a browser window or a new tab in your browser.
|
| 2. |
Enter the appropriate URL for accessing Portal Builder; for example, for a default installation, the URL is: http://<hostname>:7778/pls/portal.

|
| 3. |
Click the Login link
in the Login portlet.

|
| 4. |
Enter your <username> and <password>
to access the portal.
Click Login.

|
| 5. |
From the Oracle Portal home page, click
the Navigator link in the top right.

|
| 6. |
Click the Providers
tab.

|
| 7. |
Click the Registered
Providers link.

|
| 8. |
Click the Register New...
Provider link (top left).

|
| 9. |
In the Register Provider
page, enter the following details:
| Field |
Values |
| Name |
<YourName>Provider |
| Display Name |
<Your Name> Provider |
| Timeout |
100 |
| Timeout Message |
<Your Name> Provider has timed out! |
| Implementation
Style> |
WSRP |

|
| 10. |
Click Next.

|
| 11. |
On the Define
Connection page, enter the WSDL URL for your provider. This is
the URL that you used to view the WSDL in the previous section.

|
| 12. |
Click Next.

|
| 13. |
When the
Provide Registration Property Values page appears, click Finish.

When the Registration Confirmation page appears, verify
that the message says, "You have successfully registered the provider
<Your Name> Provider" and click OK.

|
Back to Topic
List
Before you can add WelcomePortlet to a page, you will create
a new page group. In the previous topic, you have registered your portal producer
with a WSRP-enabled Oracle Portal. Now you can add WelcomePortlet to a portal
page and view it.
In this section, you create a page group.
Then you add your Java portlet to a portlet region and look at it in both view
mode and edit mode.
To create a page group, perform the following steps:
| 1. |
Click the Page
Groups tab in the Navigator.

|
| 2. |
Click Create
New… Page Group on the top left.

|
| 3. |
In the Display Name
field, enter My
Standard Portlets.

|
| 4. |
Click Create.

When you create a page group, a root page is automatically
created as a member of the page group. This root page is assigned the
same name as the page group. Every page that you create within
the page group becomes a sub-page of the root page. After you create the page group, your root
page automatically displays in Edit mode.
Your root page should look similar to this:

|
| 5. |
Click the Add Portlet
icon on the region header.

|
| 6. |
In the Portlet Repository
on the left, click Portlet Staging Area.

|
| 7. |
Click <Your
Name> Provider.

|
| 8. |
Click WelcomePortlet
to copy it to the Selected Portlets list and click OK.

WelcomePortlet is displayed in your portal page.

|
| 9. |
When your portal page
redisplays with the Welcome portlet on it, click the View Page
link at the top of the page.

You can see your portal page:

|
| 10. |
Click the Personalize
link on the portlet header.

|
| 11. |
In the Portlet Title
field, verify that the portlet title displays the name you defined. The
portlet is not very exciting, but it works!
Click Close.

|
| 12. |
Click the Edit
link to return to Edit mode.

|
Back to Topic
List
In this section, you enhance WelcomePortlet with some predefined
code that allows a user to paste HTML into a field for the portlet to render.
You also see how easily you can redeploy a portlet.
| 1.
|
In JDeveloper,
double-click the view.jsp file
in the Application Navigator.

|
| 2.
|
Toggle
between the Design view and the Source view using tabs in
the code editor.

|
| 3. |
Select
in the Design
view, and then click the Source tab to see the relevant Java code.

|
| 4. |
You can
see the source code:

|
| 5. |
To add
a predefined code snippet to view.jsp,
place the cursor at the end of this line of code: <portlet:defineObjects/>
and press [Enter].

|
| 6. |
Copy the
code provided below and paste it in the view.jsp
file.
<%
PortletPreferences prefs = renderRequest.getPreferences();
%>
<%= prefs.getValue("portletContent",
"Portlet Content") %>
|

Note: Alternatively, you can copy the code from the
hwview.txt
file.
|
| 7. |
Double-click
the edit.jsp file in the Application Navigator.

|
| 8. |
In the
Design view, notice the table in edit.jsp,
which consists of a form field, a form input field, and two form button
fields.

|
| 9. |
Compare
the JDeveloper design view on your screen with the image of the running
portlet in Oracle Portal, which is shown below:

|
| 10. |
To add
a code snippet to edit.jsp, click the Source tab.

|
| 11. |
Place the
cursor at the end of this line of code: </TD></TR>
(that is, at the end of the first table row) and press [Enter].

|
| 12. |
Copy the code provided below to implement
a form field called Content, and paste it in the edit.jsp
file.
<tr>
<td width="20%">
<p class="portlet-form-field" align="right">Content
</p>
</td>
<td width="80%">
<textarea rows="10" cols="60" class="portlet-form-input-field"
name="portletContent">
<%= prefs.getValue("portletContent", "Portlet Content")%>
</textarea>
</td>
</tr> |

Note: Alternatively, you can copy the code from the
hwedit.txt
file.
|
| 13. |
Click the
Design tab to see the new form field that was added.

|
| 14. |
To add
a code snippet to Portlet1.java,
double-click the Portlet1.java file in the Application Navigator.

|
| 15. |
Press [Ctrl]
+ [F] to implement a search. Enter prefs.store
in the Find Text dialog box and click OK.

|
| 16. |
Place the
cursor at the beginning of the prefs.store();
line of code.

|
| 17. |
Copy the
code provided below and paste it in the Portlet1.java
file.
String
contentParam = request.getParameter("portletContent");
prefs.setValues("portletContent", buildValueArray(contentParam));
|

Note: Alternatively, you can copy the code from the
hwjava.txt
file.
|
| 18. |
In the
Application Navigator, right-click <YourName>Portlets.deploy and select Deploy
To PDKStandardsOC4J.

JDeveloper automatically saves and compiles the code
before deploying the portlet.
|
| 19. |
Return
to your root page in Oracle Portal.
The portlet displays the text Portlet Content
— this is one of the changes that you made in JDeveloper.
Note: If you receive the following
message, then the portlet is in a transient state. Simply refresh the
page.
Error: Could not get markup. The cookie or session
is invalid or there is a runtime exception.

Note: If you receive a "Portlet could not be contacted"
message, click the Reload button again in your browser or press F5
to refresh the page to match the image below.
|
| 20. |
Click the
View Page link at the top of the page.

|
| 21. |
On your port | |