0% found this document useful (0 votes)
69 views

Java Portlets (JSR-168)

Java technology based web component Managed by a portal container Generates a piece of markup called "fragment" servlet architecture does not define the possible states and transitions of an included servlet or JSP. Portlet architecture does define a personalization interface nor the idea of persisting the personalization information.

Uploaded by

Sai Sandeep
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Java Portlets (JSR-168)

Java technology based web component Managed by a portal container Generates a piece of markup called "fragment" servlet architecture does not define the possible states and transitions of an included servlet or JSP. Portlet architecture does define a personalization interface nor the idea of persisting the personalization information.

Uploaded by

Sai Sandeep
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 82

12/09/2003

Java Portlets
(JSR-168)

1
12/09/2003

Sang Shin
[email protected]
www.javapassion.com
Java™ Technology Evangelist
Sun Microsystems, Inc.
2
12/09/2003

Agenda

? Java Portlet (JSR 168)


? WSRP (only a few slides since there was a
session about this yesterday)
? Products (only a few slides)
– Sun Java Enterprise Systeem Portal Server, Portlet
Builder
12/09/2003

What is a Portlet?

4
12/09/2003

What is a Portlet?
? Java technology based web component
? Managed by a portal container
? Generates a piece of markup called
“fragment”
– Adheres to certain rules such as no <html> tags, for
instance
– Fragment generated by a Portlet aggregates with
that from other Portlets to form a portal page
– Fragment generated by a Portlet may vary from one
user to another depending on the user
configuration
12/09/2003

What does JSR-168 define?


? Portlet API
– Portlet Requests
– Preferences
– User information
– Security
– Deployment packaging
? Portlet Container
– Extension of servlet container
? Contract between component and
container
12/09/2003

What are out-scope of JSR 168?


? Aggregation
? Layout management
? Page personalization and configuration
engines
? Portal administration and configuration
12/09/2003

Default Desktop

Second- Top-level tab


level table container
container

Bottom-
level
Portlet
12/09/2003

Portal/Portlet Architecture

Window
Security Config

Remote Portlet
Execution
module Portlet
Portlet Enterprise
Dispatcher
Aggregation Information
Engine Portlet Systems
User
Configuration/ Persistent
Personalization Data Store
Engine
12/09/2003

Portal and Portlet Interaction


Portlet Portlets
User Portal container A B C
A Action on B
Do action on B processAction
B C

Render A
render

These requests Render B


may be done in
parallel render
Render C
render

A’

B’ C

Outside of the scope of the Portlet specification Scope of the Portlet specification
12/09/2003

Why Portlet?
(Why can't we use Servlet?)

11
12/09/2003

Why Portlets?
? Servlet architecture does not define the
Desktop metaphor where markup
aggregation can occur
? Servlet architecture does not define the
possible states and transitions of an
included Servlet or JSP
? Servlet architecture does not define how
the state of one Servlet or JSP affects the
display of the other included Servlets or
JSPs
12/09/2003

Why Portlets?
? Servlet architecture does not define a
personalization interface nor the idea of
persisting the personalization information
? Servlet architecture does not define URL-
rewriting functions to allow the creation of
links and actions targeted to a specific form
within the fragment of a page (Portlet
markup fragment)
? Servlet architecture does not support
caching scheme of fragments
12/09/2003

Deploy to Many

Write Once

Portlet Interface
JSR 168
Compliant
Portals
12/09/2003

Portlet Architecture

15
12/09/2003

Portal Architecture
With JSR 168 Support
Portlet
Container

User Agent
Browser, Phone, PDA Network
12/09/2003

Portal
? A “specialized” web application that
provides value-added services such as
– Personalization
– Single Sign-On
– Content aggregation from various sources
– Secure search facilities
– Localization of content
? A Portal “page” represents a complete
markup document consisting of several
Portlet components
12/09/2003

Portlet Container
? Portlets are deployed in a Portlet container
such that container can
– Provides runtime environment for Portlets
– Manage life cycle of Portlets
– Provide persistent storage for storing Portlet
preferences
– Cache the portlets
– Receive requests from the portal to execute
requests on the portlet
? Not responsible for contents aggregation
12/09/2003

Portlet vs. Servlet


(Portlet is built over
Servlet Architecture)

19
12/09/2003

Similarity with Servlet


? Both are Java technology based web
components
? Life-cycle is managed by a specialized
container
– Servlet by servlet container
– Portlet by portlet container
? Both generate dynamic content
? Both interact with web client via a
request/response paradigm
12/09/2003

? Deployment model
? Classloading
? Packaging and deployment
? Lifecycle management
? Session management
? Request dispatching
12/09/2003

Differences from Servlet


? Portlets only generate markup fragments, not
complete documents
? Portlets are not directly bound to a URL
? Web clients interact with portlets through a portal
system
? Portlets have a more refined request handling
– action requests and render requests
? Portlets have predefined states
– portlet modes and window states
? Portlets can exist many times in a portal page
12/09/2003

Extra Features of Portlets


? Portlets have means for accessing and storing
persistent configuration and customization data
? Portlets have access to user profile information
? Portlets have URL rewriting functions for creating
hyperlinks within their content
– allow portal server agnostic creation of links and
actions in page fragments
? Portlets can store transient data in the portlet
session in two different scopes
– application-wide scope
– the portlet private scope
12/09/2003

Servlet Features Forbidden to


Portlets
? Setting the character set encoding of the
response
? Setting HTTP headers on the response
? The URL of the client request to the portal
12/09/2003

Servlet Features that are


leveraged by Portlets
? Portlets can leverage servlets, JSPs and JSP
tag-libraries for generating content
– Content should be markup fragment
? A portlet can call servlets and JSPs using a
request dispatcher
12/09/2003

Integration between the two

? Attributes set in the portlet request are


available in the included servlet request
? Attributes set in the portlet session are
accessible from the servlet session and vice
versa
? Portlet and the included servlet or JSP
share the same output stream
12/09/2003

Portlet and
Web App. Frameworks

27
12/09/2003

? Struts/JSF are built over Servlets


? Struts/JSF can be built over Portlets
– APIs exposed to Struts/JSF developers should
remain the same
– Struts/JSF developers must be aware of the
markup fragment he has to produce
? The markup fragment must follow the rules
? Generate Portlet URL
12/09/2003
Sun™
Push your
Exo development
Portal Buildfurther
on JSF, Struts Tech
Days
12/09/2003

Portlet Modes &


Windows State

30
12/09/2003

Portlet Modes
? Indicates function that a portlet performs
– Execute different tasks and generate different
content based on the function they perform
? Portals must support three modes
– VIEW
? Portlet renders markup fragment in this mode
– EDIT
? Used to change per-user settings to customize
rendering
– HELP
? Used to display help information
12/09/2003

Portlet Modes (Contd.)


? Portals might support other custom modes
– ABOUT
– CONFIG
– EDIT_DEFAULTS
– PREVIEW
– PRINT
? Portals might support other vendor specific
modes
12/09/2003

Windows State
? Is an indicator of the amount of portal page
space that will be assigned to the content
generated by a portlet
? Provided by portlet container
? Three states
– NORMAL
– MAXIMIZED
– MINIMIZED
? Custom states are possible
12/09/2003

Portlet Features

34
12/09/2003

Portlet Persistence

? Portlets can store persistent data for a


given user in a PortletPreferences object
? Preferences are read-write in the action
phase (processAction()) and read-only in
render phase
? Usually user preferences are modified in
EDIT mode
12/09/2003

User Profile Attributes

? Can store information about user that can


be used to personalize content for that user
? User profile information is represented as a
USER_INFO structure which is then mapped
to the real information in datastore
? Portlet defines the attributes it wants to
access in the deployment descriptor
12/09/2003

Portal context

? Can provide information on


– Portal vendor
– Version
? Used by portlets to find out more
information about additional extensions
supported by Portal
12/09/2003

Security
? Authentication is left to the underlying
servlet container
? Authorization
– Follows J2EE ‘roles’ model
– Supports programmatic role checking
12/09/2003

Session Management
? Facade on top of the HttpSession
? Two scopes: APPLICATION & PORTLET
– PORTLET is a convenience namespacing
– It’s common for portlets to appear more than once in
a page (i.e.: EU-News, US-News)
? Servlets, JSPs and Portlets within a Portlet
Application share the same session
? Session creation event notification is
supported
12/09/2003

Portlet Request Dispatcher


? Portlets can include Servlets, JSPs and
static content during a render request
? Similar to Servlet’s RequestDispatcher
12/09/2003

Localization

? Portlets can be localized by using resource


bundles
? Resource bundles are specified in
deployment descriptor
? Portlet can access resource bundle via
PortletContext.getResourceBundle() API
12/09/2003

Caching

? Expiration based caching


? Declarative caching
– Specify <expiration-cache> element in deployment
descriptor for the portlet
? Programmatic caching
– Modify EXPIRATION_CACHE property of render
response
12/09/2003

Portlet URL
?
Portlets are always accessed through a
Portal
?
Portlets do not have a direct URL mapping
? Portlet URLs allow Portlets to create URLs
that target to themselves (through the
Portal end-point)
12/09/2003

Portlet API's

44
12/09/2003

1 public interface Portlet {


2
3 public void init(PortletConfig config) throws PortletException;
4
5 // Action Request: Invoked only when the URL is an action targeted
6 // to the portlet, it does not produce output
7 public void processAction( ActionRequest request,
8 ActionResponse response)
9 throws PortletException, IOException;
10
11 // Render Request: Invoked on every request to the portal, it
12 // produces output to create the portal page
13 public void render(RenderRequest request,
14 RenderResponse response)
15 throws PortletException, IOException;
16
17 public void destroy();
18 }
12/09/2003

Portlet Life Cycle

Initialized by container init()


Notifies initialization of portlet
by container.

render() Handle requests processAction()


render() notifies the Portlet to processAction() notifies the
generate the markup Portlet about user's request
fragment for action.

Destroyed by container destroy()


destroy() notifies the
destruction of portlet by
container. Should free up
resources in this method.
12/09/2003

GenericPortlet class
? Implements Portlet interface
? Render()--like service() in servlets, calls
specified render methods based on Portlet
mode
– doView() for View mode
– doEdit() for Edit mode
– doHelp() for Help mode
? Is extended by portlet developers
– Override render methods as necessary
12/09/2003

DoView()
Render() doView()
doEdit()
doHelp() HTML
Portlet
12/09/2003

DoEdit()
doView() HTML
Render() doEdit()
doHelp()
Edit Form
Portlet
12/09/2003

ProcessAction()

Portlet

Render() DoView()
ProcessAction() HTML
store()

S1DS
12/09/2003

Other Interfaces/Classes
? PortletConfig, PortletContext,
PortalContext,PortletSession
? PortletPreferences interface
? WindowState (MINIMIZED,
NORMAL & MAXIMIZED)
? PortletMode (VIEW, EDIT & HELP)
? PortletURL
? PortletRequestDispatcher
? Portlet Tag Library
12/09/2003

PortletPreferences
? Persistent read/write Portlet configuration
– getValues(), setValues() to access PortletPreferences
– May modify attributes and store() in persistence
during processAction()
? Normally, portlet preferences are per
portlet/per user
? Persistency is managed by the portlet-
container
? Default values are defined in the portlet.xml
deployment descriptor
12/09/2003

Portlet Example – portlet class


1:
2: public class HelloWorldPortlet extends GenericPortlet {
3:
4: public void doView( RenderRequest request,
5: RenderResponse response)
6: throws PortletException {
7:
8: response.setContentType(“text/html”);
9: PrintWriter writer = response.getWriter();
10: Writer.write("Hello World");
11: }
12: }
12/09/2003

Packaging &
Deployment

54
12/09/2003

Portlet Web Application


? All resources, portlets, deployment
descriptors are packaged in one web
application archive (WAR file)
? PortletApp\
? jsp
? htmls
? WEB-INF\
? web.xml
? portlet.xml
? sun-portlet.xml
? classes\
? Lib\
12/09/2003

Portlet Example – portlet.xml


1: <portlet-app>
2: <portlet>
3: <portlet-name>HelloWorldPortlet</portlet-name>
4: <portlet-class>
5: Custom.HelloWorldPortlet
6: </portlet-class>
7: <expiration-cache>0</expiration-cache>
8: <supports>
9: <mime-type>text/html</mime-type>
10: <portlet-mode>VIEW</portlet-mode>
11: </supports>
12: <portlet-preference>
13: <preference>
14: <name>locale </name>
15: <value>USA</value>
16: </preference>
17: </portlet-preference>
18: </portlet>
19: </portlet-app>
12/09/2003

WeatherPortlet
Sample Code

57
12/09/2003

WeatherPortlet

58
12/09/2003

WeatherPortlet (1 of 7)

1. public class WeatherPortlet extends GenericPortlet {


2. private WeatherForecasterService _weatherService;
3.
4. public void init(PortletConfig config) throws PortletException,
5. UnavailableException {
6. super.init(config);
7. String weatherWSURL = config.getInitParameter("weather.url");
8. if (weatherWSURL==null)
9. throw new UnavailableException(“Web Service URL not found",-1);
10.
11. // Look up a WeatherForeCaster Web service through JNDI
12. try {
13. Context ctx = new InitialContext();
14. _weatherService = (WeatherForecasterService)
15. ctx.lookup("java:/comp/env/WeatherForecaster");
16. }catch (Exception ex) {
17. throw new UnavailableException(
18. "Weather Service can not be initialized"); 59
19. }
12/09/2003

WeatherPortlet (2 of 7)
1. public void doView(RenderRequest rReq, RenderResponse rRes)
2. throws PortletException, IOException {
3. rRes.setContentType("text/html");
4. PortletPreferences prefs = rReq.getPreferences();
5. String zip = rReq.getParameter("zip");
6. if (zip==null)
7. zip = prefs.getValue("zip","10000");
8. String unit = prefs.getValue("unit","F");
9. try {
10. W eatherForecaster weatherForecaster =
11. _weatherService.getWeatherForecasterPort();
12. W eather weather =
13. weatherForecaster.getWeather(zip,unit.charAt(0));
14. weatherForecaster = null;
15. rReq.setAttribute("weather",weather);
16. PortletRequestDispatcher rd = getPortletContext().
17. getRequestDispatcher("/weather/weatherView.jsp");
18. rd.include(rReq,rRes);
19. }catch (Exception ex) {
20. rRes.setProperty("expiration-cache","0"); 60
21. PortletRequestDispatcher rd =
12/09/2003

WeatherPortlet (3 of 7)

weatherView.jsp
1. <%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
2. <h3>Weather Information</h3> <p>
3.
4. City: <jsp:getProperty name="weather" property="city"/> <p>
5. Zip Code: <jsp:getProperty name="weather" property="zip"/> <p>
6. Time: <jsp:getProperty name="weather" property="time"/> <p>
7. Temperature unit: <jsp:getProperty name="weather" property="unit"/>
<p>
8. Current Temperature: <jsp:getProperty name= "weather"
property="currentTemp"/> <p>
9. Low: <jsp:getProperty name="weather" property="minTemp"/><p>
10. High: <jsp:getProperty name="weather" property="maxTemp"/><p>
11. <form method="post" action="<portlet:actionURL/>">
12. Enter Zip Code:
13. <input type="text" name="zip" value="">
14. <input type="submit" name="submit" value="Search">
15. </form>
61
12/09/2003

WeatherPortlet (4 of 7)

1. public void doEdit(RenderRequest rReq, RenderResponse rRes)


2. throws PortletException, IOException {
3. rRes.setContentType("text/html");
4. PortletPreferences prefs = rReq.getPreferences();
5. String zip = prefs.getValue("zip","1000");
6. String unit = prefs.getValue("unit","F");
7. rReq.setAttribute("zip",zip);
8. rReq.setAttribute("unit",unit);
9. String error = rReq.getParameter("error");
10. if (error!=null) {
11. error = "<font color=\"red\"><b>ERROR: </b>"
12. +error+"</error>";
13. } else {error = "";}
14. rReq.setAttribute("error",error);
15. PortletRequestDispatcher rd =
16. getPortletContext().getRequestDispatcher("/weather/weatherEdit.jsp");
17. rd.include(rReq,rRes);
18. }
62
12/09/2003

WeatherPortlet (5 of 7)
1. public void processAction(ActionRequest aReq, ActionResponse aRes) throws
PortletException, IOException {
2. PortletPreferences prefs = aReq.getPreferences();
3. String zip = aReq.getParameter("zip");
4. if (aReq.getPortletMode().equals(PortletMode.VIEW))
5. aRes.setRenderParameter("zip",zip);
6. else
7. if (aReq.getPortletMode().equals(PortletMode.EDIT)) {
8. ... // some variables initialization
9. String unit = aReq.getParameter("unit");
10. prefs.setValue("zip",zip);
11. prefs.setValue("unit",unit);
12. try {
13. prefs.store();
14. editOK = true;
15. }catch (ValidatorException ex) {
16. editOK = false;
17. errorMsg = ex.getMessage();
18. }
19. if (editOK)
20. aRes.setPortletMode(PortletMode.VIEW);
21. else {
22. aRes.setRenderParameter("error", URLEncoder.encode(errorMsg));
23. } }
63
12/09/2003

WeatherPortlet (6 of 7)

1. public class WeatherValidator implements PreferencesValidator {


2. public void validate(PortletPreferences preferences) throws ValidatorException {
3. String zip = preferences.getValue("zip",null);
4. String unit = preferences.getValue("unit",null);
5. if (zip==null || unit==null) {
6. Set set = new HashSet();
7. set.add("zip");
8. set.add("unit");
9. throw new ValidatorException( "must contain zip and unit keys",set);
10. }
11. try {
12. int i = Integer.parseInt(zip);
13. if (i<0 || i>99999)
14. throw new Exception();
15. }catch (Exception ex) {
16. Set set = new HashSet();
17. set.add("zip");
18. throw new ValidatorException("zip must be a valid Zip code",set);
19. }
20. if (!unit.equals("F") && !unit.equals("C")) {
21. ...
22. } } }

64
12/09/2003

portlet.xml
1. <portlet-app>
2. <portlet>
3. <portlet-name>WeatherPortlet</portlet-name>
4. <portlet-class>sample.portlet.WeatherPortlet</portlet-class>
5. <init-param>
6. <name>weather.url</name>
7. <value>java:/comp/env/WeatherProvider</value>
8. </init-param>
9. <expiration-cache>3600</expiration-cache>
10. <supports>
11. <mime-type>text/html</mime-type>
12. <portlet-mode>EDIT</portlet-mode>
13. <portlet-mode>HELP</portlet-mode>
14. </supports>
15. <portlet-info><title>WeatherPortlet</title></portlet-info>
16. <portlet-preferences>
17. <preference>
18. <name>zip</name> <value>95054</value>
19. </preference>
20. <preference>
21. <name>unit</name> <value>F</value>
22. </preference>
23. <preferences-validator>sample.portlet.WeatherValidator
24. </preferences-validator>
25. </portlet-preferences>...
65
12/09/2003

WSRP
(Web Services for
Remote Portlet)

66
12/09/2003

WSRP Value Proposition

? Provides protocols and metadata for


– Presentation-oriented Web services
– Meant for aggregation and syndication with no
programming on the consumer side
? Roles
– WSRP Producers (Web service provider)
– WSRP Consumers (Web service consumer)
12/09/2003

Portal Architecture
With WSRP Support

User Agent Network


Browser, Phone, PDA Network
12/09/2003

JSR168 and WSRP


? They were designed in collaboration
mode
? Their functionality is fully aligned

Portlet:Portlet.processAction()

WSRP: WSRP.performBlockingInteraction()

Portlet: Portlet.render()

WSRP: WSRP.getMarkup()
12/09/2003

Sun Java System


Portal Server

70
12/09/2003

Integrated Communications
Desktop and Wireless Access
Access Mail, Calendar and
Instant Messaging Channels
Instant Messaging Server
from your Desktop or...
Forward From Any
Alerts to Mobile Device
a Phone

Messaging Server

Calendar Server

Make Calendar
Appointments
Respond to Email
12/09/2003

Personalization
?
Modify any aspect of the desktop (as
allowed by the Administrator)
– Add or delete portlets
– Customize the desktop layout
– Use or create personalized themes
– Modify or create multiple tabs
– Update user profile information
– Modify portlets settings and properties
12/09/2003

Sun Java System Portal Server and


Portal standards
? Sun Java System Portal Server supports
JSR 168
? WSRP will be supported in the first Sun Java
System update
12/09/2003

Sun Java System


Portal Server Architecture
How do JSR168 and WSRP fit in ?

Producer
WSRP

SOAP

WSRP

Portlet Container
Consumer Portlet
(remote Portal)

Portlet
HTTP/S

Network
HTML/WML

Portlet
VoiceXML

User Agent Portal


(Browser)

HTTP/S
WSRP

Consumer

SOAP
WSRP Producer
Network
(remote Portal)
12/09/2003

Sun ONE Portal Server with


JSR 168 and integration to EIS

SAP Vendor
Portlet Specific EIS
PSFT
Portlet

Portlet Container
JDBC
User Agent JDBC
(Browser) Portlet
Network
(PDA) Portal JCA Network
(Phone) Portlet
XML
Portlet XML
WS Web
Portlet SOAP Server
URL
Portlet Html

Sun Proprietary and Confidential. Please do not distribute.


12/09/2003

Sun Java Studio


Portlet Builder

76
12/09/2003

Portlet Builder

? Portlet development plug-in for Sun JavaTM


Studio
? Easy development
– It has wizards
– It creates and updates the portlet.xml
? Supports portlets execution, debugging
and deployment within the Studio IDE
? Runs in Sun JavaTM Studio 4.x and 5.x
12/09/2003
12/09/2003
Sun™
Portlet Builder further
Push your development 2.0 (Studio 5) Tech
Days
12/09/2003

Resources

80
12/09/2003

Resources
? JSR 168
http://www.jcp.org/jsr/detail/168.jsp
TM
? Sun ONE Portal Server
http://wwws.sun.com/software/products/portal_srvr/home_portal.html

? Sun ONE Portlet Builder


http://wwws.sun.com/software/download/products/3f187b02.html

? WSRP @ OASIS
http://www.oasis-open.org/committees/wsrp/

? WSRP @ XML Cover Pages


http://xml.coverpages.org/wsrp.html
12/09/2003

Thank You!

82

You might also like