Friday, 16 March 2012

Working with portlet container in websphere application server 7.0-Part4

PortletPreferences are set by portlets to store customized information. By default, the PortletServingServlet servlet stores the portlet preferences for each portlet window in a cookie. 

The attributes of the cookie are defined as follows:

The name of the cookie has the fixed value of PortletPreferenceCookie.
The value of the cookie contains a list of preferences by mapping to the following structure:
*['/' pref-name *['=' pref-value]]
All preferences start with '/' followed by the name of the preference. If the preference has one or more values, the values follow the name separated by the '=' character. A null value is represented by the string '#*!0_NULL_0!*#'. 

In this example, the cookie value look like, 

Name: PortletPreferenceCookie

Path: /myportletpref/MyPortletPref/default

Value: /namepair=wpsadmin=wpsadmin

URL of the portlet is :

Sample Code:


import java.util.*;
import javax.portlet.*;

 * A sample portlet based on GenericPortlet
public class MyPortletPrefPortlet extends GenericPortlet {

    public static final String JSP_FOLDER    = "/_MyPortletPref/jsp/";    // JSP folder name

    public static final String VIEW_JSP      = "MyPortletPrefPortletView";         // JSP file name to be rendered on the view mode
    public void init() throws PortletException{
    public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
        // Set the MIME type for the render response
        // Invoke the JSP to render
        PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(getJspFilePath(request, VIEW_JSP));
    public void processAction(ActionRequest request, ActionResponse response) throws PortletException, {
        PortletPreferences prefs=request.getPreferences();
        String[] namepair={request.getParameter("myname"),request.getParameter("password")};
        prefs.setValues("namepair", namepair);;
    private static String getJspFilePath(RenderRequest request, String jspFile) {
        String markup = request.getProperty("wps.markup");
        if( markup == null )
            markup = getMarkup(request.getResponseContentType());
        return JSP_FOLDER + markup + "/" + jspFile + "." + getJspExtension(markup);

    private static String getMarkup(String contentType) {
        if( "text/vnd.wap.wml".equals(contentType) )
            return "wml";
            return "html";
    private static String getJspExtension(String markupName) {
        return "jsp";


MyPortletPrefPortletView .jsp

<%@page session="false" contentType="text/html" pageEncoding="ISO-8859-1" import="java.util.*,javax.portlet.*,*" %>
<%@ taglib uri="" prefix="portlet"%>               
Enter your Name<BR>
<form action='<portlet:actionURL/>'>
<input type="text" name="myname"/>
<input type="password" name="password"/>
<input type="submit" name="submit" value="submit"/>


Click here to download the source code

No comments:

Post a Comment