JSF Portlet Session Maintenance

To Maintain the Sessions between the two portlets which are deployed on the same portlet page we have to follow the below steps.

Each Portlet Instance does not preserve its independent session, all the instance of the portlet share the same managed beans. This happens because the PortletSession object by default maintains the attributes in APPLICATION SCOPE (this can be used whenever you need all your portlet instances to share the same attribute) But if you need your portlets to have their independent attribute values then we need to set the scope as PORTLET SCOPE. To say this point clear the below sample is explained.

The scenario which I faced for this is: I had a created a Jsf Portlet in eclipse 3.4 Ganymede, The name of the project is FinalJSFTest3 in that faces jsp name which is FinalJSFTestView.jsp, success’s.jsp and the bean which i used for this is FinalJSFBean.java. The FinalJSFBean.java contains both setters and getters and the action which u want to perform when u click on the jsp button is also in this bean.

When u deployed this application in WPS v6.1, The flow of this application is as follows:

1) The portal server load the web.xml first and from the web.xml it will redirect to portlet.xml

2) From the portlet.xml file depending upon the class mentioned it loads the Faces context class and loads the FinalJSFTestVeiw.jsp page.

3) The FinalJSFTestVeiw.jsp page contains two input textboxes and with a submit button.

4) When u fill the data in the text boxes as soon as, when u click on the submit button a request has been sent to the faces-config.xml file. And depending upon the action which u write for the bean it goes to the bean and executes the appropriate action and returns the value form the action.

5) Depending upon the action it redirects to the appropriate page.

Here the problem which i faced is If i add the same portlet twice in the same page and If I submit values in the first portlet and I click on the submit button

It navigates to the next page (success.jsp) with the values which I have given in the first page, there I have a back button in the success.jsp page if I click on the back button it will goes to the previous page. Here the values which I send in the first page can be viewed in the second page upto now the scenario is fine. The problem here is the values which I send in the second portlet page is navigates to success page with values by the same time it is updating the values in the first portlet page which is a wrong session maintenance. For this I followed the below scenario:

In the bean when we are setting the attributes in general we set in a request.setAttribute. but in the JSF Bean we don’t have Render request, Render response so i set these values in a sessionAttribute which is a portletSession and I maintained these values in the PortletScope.

See the example code here.

In the above code on buttonAction () it is executing some code. In this code first i get the currentInstance of the FacesContext object and i stored that currentInstance in a facesContext and i set the portletSession to null. And i setting the portletSession to true using the request.getPortletSession(true), thereafter iam setting the attribute values using portletSession.setAttribute (String arg0, Object arg1, int arg2) here the above code is reflected as follows String “username”, Object username, int portletSession.PORTLET_SCOPE. It is a JSF Bean so iam returning the successfrombean. It is mapped with faces-config.xml file. i.e. we have an add rule concept in JSF that depending upon the output returned from the action we can navigate to another page which we mapped to that output action. In the same way depending upon this successfrombean it is going to navigate to success.jsp page.

In the success.jsp page we want to show the values which we passed in the FinalJSFTestView.jsp page. For that i done with the below code.

Using the above code I retrieved the values from the FinalJSFTestView.jsp page and I stored those values in a portletSession.PORTLET_SCOPE.

With the above methodology we can maintain a individual session for each portlet in jsf portlets. With this the session will won’t conflict each other the data can’t clash any where through out that portlet scope. We can also call it is Inter-portlet communication with in the application.

For PortletSession Maintenance you can see the following references.


Note: The above PortletSessionMaintenance problem is explained for JSFPortlet project. Here we don’t have a doView(), doProcessaction ()


Manoj said...

I have an issue with JSF portlets in Web 2.0 theme. On a particular I have two jsf portlets installed. When I click on the submit button of the first portelt, the second portlet also gets refreshed. Both the portlets are deployed in the same EAR. Could you please help me out?


Blufmaster said...

which WPS are u using?? 6.0 or 6.1
IBM has a problem with 6.0 if u want to do partial refresh u shud use ajax.
According to IBM in WPS 6.1 the pages get partially refresh , entire page dont get refresh if u use Web2.0

U might be using 6.1 i guess as u are using JSF portlets in Web 2.0 theme...

Keep ur bean in session