Personalizing the user experience through form data submitted in a portlet

A company wants to offer its customers various promotions based on purchases that the customers make through a portlet. The company might choose to change the business rules for these promotions over time. With IBM® WebSphere® Portal's Personalization, you can use rules and content spots in your portlets to display personalized data. You could enable the rules to change at run time based on input that users enter on forms in portlets. These changes can become effective immediately; you do not need to restart the server or re-deploy your portlets. This article shows you how to make data submitted through a form in a portlet accessible to WebSphere Portal's Personalization rules. You can apply this technique to the customer promotions scenario, and to other scenarios that require the ability to change personalization rules at run time.

Wanjun Wang (wangwan@us.ibm.com), Software Engineer, IBM

Wanjun Wang is the WebSphere Portal System Verification Test architect and has been working on Portal/Web Content Management/Personalization and other middleware for years. He oversees the system test strategy for Portal/WCM releases, which includes test configurations, reliability runs and automation. He is also actively engaged in high-profile customer consulting for large deployment. Wanjun is based in IBM Research Triangle Park Lab, USA.



20 June 2007

Introduction

Personalization is a core component in IBM® WebSphere® Portal. You can use Personalization rules and content spots in your portlets to display personalized data. You can make changes to rules at run time, and these changes can become effective immediately; you don't need to restart the server, redesign the application, or redeploy your portlets.

This article shows you how to make data submitted through a form in a portlet accessible to WebSphere Portal's Personalization rules. You learn about the flexibility provided by using Personalization rules to process portlet form data.

This article is intended for portlet developers who already know how to create a portlet project in IBM Rational® Application Developer Version 6 (in this article, called Application Developer) and how to install the portlet into WebSphere Portal.

You learn how to:

  1. Create a portlet project with a sample form.
  2. Display personalized results.
  3. Register custom session beans in Personalization.
  4. Use a content spot and Select rules in Personalization.

About the download

The download includes:

  1. SmartPromotion.warThe source and the compiled code of the portlet demonstrated in this article. You can install this portlet directly into WebSphere Portal if you don’t want to write your own portlet in Rational Application Developer.
  2. pzndemo.jarThe Personalization demo class needed to build the example portlet in Rational Application Developer.
  3. SmartPromotionSession.jarThe packaged session bean class needed to register with Personalization. You can also package it yourself from Rational Application Developer after you create a portlet project with a sample form.

The source code and deployable application are in the Resources directory of the Personalization sample.

A usage scenario

You could use the information in this article to implement the following scenario. A company plans to offer customers promotions based on a purchase made through a portlet. However, the business rule for promotions can change over time. For example, the company might want to offer employee pricing exclusively for prototype products for a month to collect market response. Other times, the company might offer employee pricing for any product which is less expensive than the original purchase. The company would also like to design the portlet such that the exact rule for promotions can be managed at run time without redeploying the application.


Before you begin

To play through the example scenario described in this article, you need to be familiar with, and have access to the following. See Resources for help in acquiring copies of this software.

  1. IBM Rational Application Developer Version 6 or higher
  2. IBM WebSphere Portal Version 6 or WebSphere Portal Express V6, or higher
  3. The WebSphere Portal Personalization demoWebSphere Portal includes the Personalization demo which you need to install if you want to complete the exercise described in this article. To install the sample, run the install-pzndemo Portal configuration task. See the WebSphere Portal Version 6.0 Information Center (listed in Resources) for more details. The task described in this article uses the Products resource collection from the sample. For simplicity, the example uses a form to submit the price of the purchase. In a real world application, you chould choose to make the price a hidden form input.

Create a portlet with Session Bean for Form Data

You first need to create a portlet project with a sample form in Rational Application Developer, which will also automatically generate a session bean class to handle form data:

  1. Create a JSR168 portlet, and name it SmartPromotion.
  2. For the target server, select WebSphere Portal v5.1 stub.
  3. Click Next.
  4. Select Add form sample, and then click Finish.

The wizard generates two classes, SmartPromotionPortlet.java and SmartPromotionPortletSessionBean.java, and a sample JSP called SmartPromotionPortletView.jsp.

SmartPromotionPortlet.java is the main portlet class; you don't need to modify it for the example in this article. SmartPromotionPortletSession.java is the generated session bean class to handle form data; it will be passed to the Personalization engine later. The JSP file is the display part of the portlet; you modify it later to render the personalized results.


Add a Personalization content spot

A Personalization content spot is a placeholder used to display Personalization rule results so that you don't have to hard-code the Personalization rule in the JSP. Using the content spot in portlets, you can assign different rules at run time without re-compiling, re-deploying, or updating the portlet applications in WebSphere Portal

To create a content spot:

  1. In Application Developer, right-click on the package name, and select New => Other.
  2. Choose Portal => Personalization => Content Spot.
  3. For both the Display Name and the Class Name, enter PromotionContentSpot.The display name is the name of the content spot that you need to create later in Personalization. If you plan to define the content spot under a folder in Personalization, you need to specify the display name as /<folder>/PromotionContentSpot. You can also manually modify the getDisplayName() method to return the correct name after the class is created.
  4. Click Yes on the Add Personalization Nature popup window. The wizard creates a class file called PromotionContentSpot.java.
  5. To use the Products resource collection in Personalization sample, make a JAR file out of the pzndemo directory under <websphere_portal>/pzn/v6.0/sample/lib and add the JAR file as an external library to the Application Developer project.
  6. To use the content spot, modify SmartPromotionPortletView.jsp to display the personalized results. The example code only lists the name and the employee price of the promoted products, as shown in Listing 1.

Listing 1. Sample JSP to display personalized results

<%@ page session="false" contentType="text/html" 
 import="java.util.*,javax.portlet.*,smartpromotion.*,
 pzndemo.resources.Products" %>
<%@taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<portlet:defineObjects/>

<%
	SmartPromotionPortletSessionBean sessionBean = 
	(SmartPromotionPortletSessionBean)renderRequest.getPortletSession()
	.getAttribute(SmartPromotionPortlet.SESSION_BEAN);
%>

<DIV style="margin: 6px">

<H3 style="margin-bottom: 3px">Welcome to Personalization!</H3>
This is a sample form to test Personalization on session bean.

<DIV style="margin: 12px; margin-bottom: 36px">

<FORM method="POST" action="<portlet:actionURL/>">
  <LABEL  for="<%=SmartPromotionPortlet.FORM_TEXT%>">
    Enter purchase price:</LABEL><BR>
  <INPUT name="<%=SmartPromotionPortlet.FORM_TEXT%>" type="text"/>
  <INPUT name="<%=SmartPromotionPortlet.FORM_SUBMIT%>" 
    type="submit" value="Submit"/>
</FORM>

<%
 String formText = sessionBean.getFormText();
   if( formText.length()>0 ) {
     try {
	PromotionContentSpot contentSpot=new PromotionContentSpot();
	contentSpot.setRequest(request);
	Object[] results=contentSpot.getRuleContent();
	if ((results!=null) && (results.length>0)) {
		for (int i=0; i<results.length; i++) {
			pzndemo.resources.Products prod=
			(pzndemo.resources.Products)(results[i]);
%>
		<%=prod.getNameOfProduct()%>, Discount Price: 
		  <%=prod.getEmployeePrice()%><BR>
<%
		}
		} else {
		%>
		No promotion for purchase of "<%=formText%>".
		<%
	}
	} catch (Exception e) {
	%>
	Failed to personalize on session bean:<BR>
	<%=e %><BR>
	<%=e.getMessage() %><BR>
	<%
     }
   }

%>

</DIV>

</DIV>

Add the portlet session bean to the Personalization engine

After modifying the JSP to display personalized results, you need to register the session bean with the Personalization engine.To enable the Personalization engine to leverage data in session bean:

  1. Export the session bean class as a jar file with full class path.
  2. Copy it to <wp>/pzn/v6.0/collections directory on the WebSphere Portal machine.
  3. Restart WebSphere Portal server to load the jar file.

To register the session bean with Personalization:

  1. Create an application object in Personalization.
  2. Session key and class name are required fields for the creation of the application object. In the example, the session key is automatically generated as SmartPromotionPortletSessionBean in SmartPromotionPortlet.java. You can keep that generated name.
  3. Specify the class name as smartpromotion.SmartPromotionPortletSessionBean.

Create a Personalization rule using the session bean

After you create the application object, Personalization rules can use its fields as operands for personalizing content. The PromoByPrice rule (below) returns products whose cost is lower than the purchase price currently stored in session data.


Create the content spot used by the portlet and map the rule

Finally, create a Personalization content spot named PromotionContentSpot which uses Products as the output type to match the content spot class generated in Application Developer. Assign the PromoByPrice rule to the content spot as the default mapping.


Install the portlet and personalize

Install the portlet. To view the discounted products being offered, enter a purchase price, and then click Submit.

In this eample, the purchase price entered is 100; therefore, only items whose cost is lower than 100 will be offered with the discount.


Create and map an advanced Personalization rule

To create an advanced rule, you can add more conditions. For example, in addition to the price condition, suppose you want to add another condition to the PromoByPriceState rule. You want it to select only those products whichare currently in production and are not yet available.

  1. Add an additional whose Stage of Production is Future clause in the rule, as shown below.
  2. Assign the new rule to the content spot, and all users will get the results based on the new conditions.

Personalization also provides binding rules which you can use to apply different select rules for different groups of users, and campaigns where selection rules are performed for a specified period of time. The binding rules and the campaigns can all be applied to content spots without any redeployment of Web applications. Rease refer to WebSphere Portal 6 InfoCenter for the details of using these Personalization capabilities.


Conclusion

Now you know how to leverage the Personalization engine provided by WebSphere Portal to process form data. The advantage of using Personalization is flexibility; you can apply user-entered form data to personalize the user’s experience. Also, as often required by business, you can dynamically manage the user’s experience using Personalization rules without redesign or redeploying the application.


Download

DescriptionNameSize
Sample codesamples-smartpromotion.zip14KB

Resources

Learn

Get products and technologies

Discuss

Comments

developerWorks: Sign in

Required fields are indicated with an asterisk (*).


Need an IBM ID?
Forgot your IBM ID?


Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name



The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.

 


All information submitted is secure.

Dig deeper into WebSphere on developerWorks


static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=WebSphere
ArticleID=230962
ArticleTitle=Personalizing the user experience through form data submitted in a portlet
publish-date=06202007