Step by Step creating a custom Portlet service


Portlet Services

Portlet services are used to provide common functionality/services to portlets. WebSphere Portal provides many out of the box portlet services for different functionality like PUMA service, Authentication service and lot more.
Portlets use JNDI look up to obtain an instance of the required portlet service. A portlet service can only be invoked from inside a portlet. New services can be added to the portal but it requires restart of the portal.

Following are the steps to create a custom Portlet Service:

1. Create an interface which extends interface and defines public methods which you want to expose through the service.

2. Implement the above interface along with interface in your custom class.

3. Export the Service as Java archive (JAR) file.

4. Register the portlet service with Portal.

5. Restart the server for changes to take effect.

Step1: Defining the interface

package com.sample.portlet.service;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;

public interface MyService extends PortletService {
    public void myFirstPortletService(PortletRequest request, PortletResponse response)  throws IOException;
Step2: Writing the Service Implementation
package com.sample.portlet.service;
import java.util.prefs.Preferences;
import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;

public class MyServiceImpl implements MyService,PortletServiceProvider  {
    private String message;

    public void init(Preferences servicePreferences) {
        // read the message from the portlet preferences , default is "My First Service"
        message = servicePreferences.get("message", "My First Service");

    public void myFirstPortletService(PortletRequest request, PortletResponse response) throws IOException {
              System.out.println("Calling myFirstPortletService Method : " + message);

Step3: Registering the service

  • Put all service interface and implementation classes into a JAR file.
  • Place the JAR file in the wp_profile/PortalServer_root/config directory.
  • Register the new portlet service with the WP PortletServiceRegistryService resource environment provider in the WebSphere Application Server administration console.
  • Create an entry to register the implementation in the JNDI directory. The name for this entry is jndi.service_interface and the value is service implementation. The fully qualified service interface name can then be used to look-up the service.
  • Restart WebSphere Portal to activate the new settings.

WAS Console

            value: com.sample.portlet.service.MyServiceImpl

Step4: Invoking the Service From the Portlet

import javax.portlet.*;
import com.sample.portlet.service.MyService;

public class CustomPortletServicePortlet extends GenericPortlet {
      private PortletServiceHome accountInfoServiceHome;
       private MyService myservice;
      public void init() throws PortletException {
         /* Lookup the portlet service */
          try {
            javax.naming.Context ctx = new javax.naming.InitialContext();
            Object home = ctx.lookup("portletservice/com.sample.portlet.service.MyService");
              if (home != null)
                  accountInfoServiceHome = (PortletServiceHome) home;
          } catch(NameNotFoundException ex) {
                 // Exception handling code goes here
          } catch (NamingException ex) {
                 // Exception handling code goes here
      public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
            // Set the MIME type for the render response
            if(accountInfoServiceHome != null){
                  try {
                        myservice = (MyService)accountInfoServiceHome.getPortletService(MyService.class);
                        myservice.myFirstPortletService(request, response);
                  } catch (PortletServiceUnavailableException e) {
                       // Exception handling code goes here

If the service is invoked properly then the default "My First Service" can be viewed in the SystemOut.log.


Jeff Power said...

is it supported for JSR 286 portlets as well?

Arun Khurana said...

Yes Jeff. It is supported.

Kumar said...

Can I cache porlet services calls?

Arun Khurana said...

No we can't cache the service calls instead caching of portlet and data returned by the service call can be cached for sure...

Post a Comment

Recent Posts

Popular Posts

© 2011-2019 Web Portal Club