build standards-based Java portlets using the Java Portlet Specification, JSR For more information and to download a sample application, see the blog . Jsr vs Jsr - Download as Word Doc .doc /.docx), PDF File .pdf), Text File .txt) or Introduction to the Struts Web Framework - NetBeans IDE Tutorial. Portlet Tutorial, Java Portlet, Portlet Example, Java Portlet Tutorial, What is JSR has introduced the using of annotations for controlling things .. Contribute us by commenting below and find downloaded source code.
|Language:||English, Spanish, Portuguese|
|Genre:||Business & Career|
|ePub File Size:||24.50 MB|
|PDF File Size:||10.77 MB|
|Distribution:||Free* [*Sign up for free]|
This portlet programming tutorial is an introduction to developing JSR applications that can be deployed to any standards-based portal server. Portlets are, quite simply, standardized, Java-based, content delivery vehicles that render themselves within the confines of a portal. This portlet programming tutorial shows how the PortletRequest and Portlet Response are used when developing JSR, request-response based apps. The following tutorial is the second in a series of JSR development learning resources appearing on TechTarget's TheServerSide. Java Portlet Specification , Java Specification Request (JSR) , was the latest portal server release from Sun, is free for download and deployment. The Java Portlet Specification (PDF), which describes the benefits of JSR . Forums · Java Magazine · Developer Training · Tutorials · portal7.info
Enjoy this article as well as all of our content, including E-Guides, news, tips and more. Step 2 of 2: You forgot to provide an Email Address. This email address is already registered. Please login. You have exceeded the maximum character limit.
For more information, see Portlet Modes. Select each of the portlet modes and specify the implementation method to use for rendering content, as described in step The content type identifies what type of content the portlet supports.
Portlets can support multiple content types. This is the default selection. If you selected Enable users to edit portlet content on the General Portlet Information page earlier in the wizard, then you can create portlet preferences to enable users of the portlet to specify values for the portlet at runtime. Go to step If you did not select this option, go to step On the Customization Preferences page, click Add to add a new portlet preference to the portlet.
By default, the wizard includes a portlet preference to enable users to customize the portlet title. In the Add New Preference dialog, in the Name field, enter a name for the new preference. The name must be unique in the portlet and cannot contain spaces. In the Default Values field, enter one or more default values for the new preference. Separate multiple values with commas. Select the Translate Preference check box if you want to be able to make the preference available in different languages.
For example, if the portlet is likely to be consumed by a multilingual portal you want to ensure that the preference displays in the appropriate language. If you enable this option, then entries for the preference are added to the resource bundle class that JDeveloper generates for the portlet.
Tip: Edit the resource bundle to provide translations for the preference name and default values. The name will almost always require translating, but the default values may not, for example, if the value is an integer.
Click OK. Repeat the preceding steps to add more preferences. When you are done click Next. On the Security Roles page, to add an existing security role to your portlet, select the security role and move it to the Selected list.
Security roles enable you to set tiered levels of access to the portlet. For example, a View user can view the portlet but cannot edit it; a Customize user can customize portlet settings; a Manage user can perform all available functions associated with the portlet.
The Available list displays the security roles defined for the application in which you are creating the portlet. Moving a security role to the Selected list creates a reference of the security role in the application's portlet deployment file portlet. You can create new security roles for the application by editing web. For more information, see the JDeveloper online help.
On the Caching Options page, select Cache Portlet to enable expiry-based caching for your portlet. Selecting this option indicates that portlet caching is managed by the portlet container.
The portlet itself may choose to cache content for any given response. Using Ajax In version 1. This servlet, however, was then addressed directly and not through the portal framework, so it did not have the portlet state provided, nor was it under the security protection of the portal, as you can see in figure 4.
The only way to share data between the portlet and the servlet is to use the application-scope session or the parameter on the URL that invokes the servlet. Figure 4. Ajax solution in JSR Serving the Ajax data using an additional servlet View image at full size How does the new portlet specification support Ajax use cases? As we have seen, JSR provides you with the means to serve resources directly using the portlet. Thus, you can issue XmlHttpRequests to ResourceURLs and, on the server side, get complete access to the portlet context such as render parameter, portlet mode, window state, portlet preferences, and portlet session.
We also learned that you can make some state changes in the serveResource call: Change the portlet preferences Change data in the portlet session scope You can now implement additional use cases such as asynchronous updates that make your user interface more responsive.
Figure 5. You can see that the Ajax calls go through the portal servlet and thus are under the control of the portal. Closing the gap to the servlet programming model The first version of the Java Portlet Specification restricted the portlet programming model in some areas compared to the servlet programming model.
This restriction was done because portlets are aggregated on a page and all concepts that assume that you are the only component on the page are not easily applied to the portlet programming model.
The second version tackles these issues and provides solutions so that the portlet programming model has nearly the same capabilities as the servlet one, plus the portlet specific extensions. Cookies, document head section elements, and HTTP headers In the first version of the Java Portlet Specification, the portlet could not contribute to sections of the portal page outside its portlet window.
In the second version, you can set cookies, document head section elements for example, HTML ones such as meta, link, or style and HTTP headers such as application-specific Pragma headers.
Two-part rendering life-cycle call To overcome the problem that version 1. These two parts are needed as some portal implementations, such as WebSphere Portal, stream the page and portlet output directly to the client to avoid buffering overhead. In this case, the portlet rendering cannot add anything to the head section of the document, as the head section is already written.
Thus, portlets that want to contribute to the head section or to set a portlet title should turn on the two-part rendering feature with the setting shown in listing 3 in the portlet deployment descriptor. Listing 3.
The GenericPortlet class does the rest for you. Using cookies You can set cookies at the response of each life-cycle method processAction, processEvent, render, and serveResource with this code: response.
Cookie cookie The cookie can then be accessed in all life-cycle methods using: request. Cookies may not be accessible at the client because they are stored at the portal server or they are put in a different namespace when the portlet runs as a remote portlet through WSRP. Cookies are not guaranteed to be shared across different portlets. Request dispatcher include and forward In the first version of the Java Portlet Specification, you had only the option of including servlets or JSPs from the render life-cycle call.
The second version lets you use forward and include and lets you use them in all life-cycle methods. This addition means that you can now dispatch to action or event logic written in a servlet, or you can do forwards when serving resources try to forward to the resource ID that you have set on a ResourceURL.
Leveraging the servlet life-cycle listeners In V1. For notifications about the servlet context and the corresponding portlet context. For notifications about attributes in the servlet context or the corresponding portlet context. For notifications about changes to the attributes of the HTTPServletRequest or the mirrored portlet request of the current Web application.
A servlet request listener can distinguish a plain servlet request targeted to a servlet from a wrapped servlet request targeted to a portlet by looking at the request attribute javax.
This attribute is set on a request targeted to a portlet, indicating the current portlet life-cycle phase of this request.
Access to all these life-cycle listeners gives you many hook points for managing objects related to these lifecycles, which is a great feature for many frameworks. These hook points, though, come with a cost, especially the request lifecycle listeners, which add significant processing overhead for each request.
Therefore, use them with care. Container runtime options for leveraging additional container behavior, such as the two-part rendering. Let's take a deeper look into the new features for extending the Java Portlet Specification in the next sections.
Portlet filters The new portlet filter functionality allows you to plug filters around any life-cycle call of the portlet. Following the common decorator pattern, filters can do pre- or post-processing, and they can modify or wrap the request and response objects that are passed to the portlet.
Typical applications of portlet filters could include these: Passing information from additional sources to the portlet as attributes or parameters Output filtering for security enforcement or markup compliance Collecting diagnostic information Bridging between Web application frameworks WebSphere Portal V6. The portlet filter programming model is modeled on the servlet filter model: Define the filters in the deployment descriptor.
Implement the corresponding Filter interface in your filter. You can also list multiple life-cycle entries and implement multiple Filter interfaces with your class. Provide a filter-mapping element where you describe to which portlets your filter should be applied you can also use an asterisk as a wildcard if it should be applied to all portlets in the application. The order of the filter-mapping elements in the deployment descriptor also defines the order of the filters that are applied to the portlet.
Listing 4 shows an example for a deployment descriptor entry. Listing 4. Each filter gets the current request and response, or a wrapped version created by a preceding filter, and the filter chain. After doing its preprocessing, the filter implementation can either terminate the request processing or call the next element in the filter chain, passing in either the received request and response or additional wrappers. The last element in the filter chain is the portlet itself.
Listing 5 is an example of a filter that does some pre- and post-processing and provides the portlet with a wrapped request. Listing 5. Do not add new methods on the wrapper; there may be other wrappers in the chain that you don't know of, and thus the portlet may not be able to access your new method. If you want to provide the portlet with additional capabilities, set an object providing access to these capabilities as a request attribute.
One example involves implementing a mapping of resources to shared IDs. You need to register such a listener with the listener element in the portlet deployment descriptor, and your class needs to implement the PortalURLGenerationListener interface that defines a callback method for each type of portlet URLs: action, render, and resource. Portlet-managed modes In JSR , the portlet could leverage only portlet modes that are supported by the portal framework running the portlet.
In some use cases, the portlet wants to offer portlet-specific functionality with the same user look-and-feel as portlet modes supported by the portal for example, with context menus on the portlet window. For example, a ShowShoppingCart portlet mode lists all entries that you currently have in your shopping cart.
To support these use cases, JSR introduces the portlet-managed modes that are not known to the portal, but are managed by the portlet itself.
The portlet can declare such a mode in the portlet deployment descriptor with the code shown in listing 6. Listing 6. This setting indicates to the portal that it should treat this mode just like the standard View mode concerning all aspects, including how it provides the portlet with preferences and from the perspective of access control.
The portal should provide UI controls that allow the portlet to switch to this mode. The portlet can define localized names for the decoration using the resource bundle entry: javax. Because the portal does not know specific semantics of the portlet-managed modes, it does not know when it makes sense to present a decoration to switch to a portlet-managed mode and when it does not.
Therefore, the JSR specification lets you indicate for which portlet modes the portal should display UI controls as part of the render response using the method setNextPossiblePortletModes. Note that you need to set this list of modes on each response and that you should enable the container runtime option javax.
Container runtime options Container runtime options allow the portlet to supply specific options to the portlet container that either change default behavior defined in the Java Portlet Specification or add additional behaviors. We've already seen an example of such a container runtime setting: the renderHeaders option. JSR defines a list of container runtime options that are all optional except for actionScopedRequestAttributes.
In addition to these options, specific portlet container implementations may provide their own options. You indicate that your portlet requires a specific container runtime option in the deployment descriptor using the code shown in listing 7.
Enables the two-part rendering mechanism that lets you set headers in streaming-based portal implementations. Allows you to change the default scope of the session object provided to servlets or JSPs that are called from the portlet through forward or include from application scope to portlet scope.
That way, these resources access the same session data using portlet and servlet APIs, which is particularly convenient if your JSPs are written for a servlet-based Web framework.
Allows Web frameworks to pass complex objects from the action or event phase to the render phase through the request. You are able to access these attributes until the next request with an action semantic which may be an action triggered through an Action URL or an event occurs. This feature likely is implemented by the portlet container by storing these attributes in the session.
Therefore, use this feature only if you cannot avoid it, as it will probably cause some performance degradation. This option is the only one that JSR requires to be supported by all containers. Note,that this title may be overrided by the portal or,programmatically by the portlet. Only one,short title per locale is allowed.
The display name need,not be unique. Typically, you should define your resources within the same package as your Portlet to make sure every Portlet has used its relevant messages. Another way to change the title programmatically, you can invoke setTitle against renderResponse object inside any delegated method i.
These actions URLs are useful for processing actions i. At the same time, some Portlets have provided so complicated business, the user may need some sort of help to get the Portlet working properly and for that purpose the doHelp is given. Other two Portlets have defined all of modes are available. Portal provides you the mode user interface for toggling between modes that are supported by the Portlet.