As per the JSR 286 Specification the following Listeners, which work with Servlet Requests, can be applied to requests targeted towards Portlets.
For this to work the Container needs to set a variable javax.portlet.lifecycle_phase.
Lets see an example.
In this example we will create a new custom HttpSessionAttributeListener. This listener will be triggered every time an attribute is added or removed from the PortletSession object.
Step 1: Create a JSR compliant Portlet, using our earlier blog post. If you have one already then make the following changes.
Create a new Class file CustomSessionAttributeListener, or just copy the code below. This file needs to implement HttpSessionAttributeListener interface.
public class CustomSessionAttributeListener implements
HttpSessionAttributeListener{
public void attributeAdded(HttpSessionBindingEvent event) {
System.out.println("Following attribute was added"+event.getName());
}
public void attributeRemoved(HttpSessionBindingEvent event) {
System.out.println("Following attribute was removed"+event.getName());
}
public void attributeReplaced(HttpSessionBindingEvent event) {
}
}
- ServletContextListener
- ServletContextAttributeListener
- HttpSessionActivationListener
- HttpSessionAttributeListener
- HttpSessionBindingListener
- ServletRequestListener.
For this to work the Container needs to set a variable javax.portlet.lifecycle_phase.
Lets see an example.
In this example we will create a new custom HttpSessionAttributeListener. This listener will be triggered every time an attribute is added or removed from the PortletSession object.
Step 1: Create a JSR compliant Portlet, using our earlier blog post. If you have one already then make the following changes.
Create a new Class file CustomSessionAttributeListener, or just copy the code below. This file needs to implement HttpSessionAttributeListener interface.
public class CustomSessionAttributeListener implements
HttpSessionAttributeListener{
public void attributeAdded(HttpSessionBindingEvent event) {
System.out.println("Following attribute was added"+event.getName());
}
public void attributeRemoved(HttpSessionBindingEvent event) {
System.out.println("Following attribute was removed"+event.getName());
}
public void attributeReplaced(HttpSessionBindingEvent event) {
}
}
Step 2: Add the following xml fragment to your web.xml file
<listener>
<listener-class>
com.listeners.CustomSessionAttributeListener
</listener-class>
</listener>
Step 3: In the Portlet class just add an attribute to the session object. Refer to the code below.
public class RegisterUserPortlet extends GenericPortlet{
@RenderMode(name="VIEW")
public void display(RenderRequest req, RenderResponse res) throws PortletException, IOException{
/*Session Demo*/
PortletSession session = req.getPortletSession();
session.setAttribute("profilename", "ankur");
session.removeAttribute("profilename");
}
}
And that is it. Just deploy your code. When you will access this Portlet you will see following messages in the console / log file.
Following attribute was added profilename
Following attribute was removed profilename
No comments:
Post a Comment