Monday, December 1, 2014

Portlet: Create portlet (JSR 286) using JDEV

In this blog we are going to create a basic standard based portlet (JSR 286) using JDEV.

Why do we need portlet: Portlet is a way to share your UI content in multiple application. Let say you have an application which shows employee hierarchy. You may be interested to show that particular part in other applications as well. In such cases these common UI which need to appear in multiple application can be created using portlet and then all applications can consume them.

What is JSR 286: . JSR 286 is java specification for portlets. I would say there are mainly two parties involved here. 1. Developers 2. Portlet producer. Portlet producer is a server which handles request and provide response like a web container. For example weblogic, jboss etc. Now all these java servers need to know if a request comes, how will they know its for which portlet, which class needs to be instantiated and which method of class needs to be called to get response. Developers also need to implement those classes. So there is a need of standard so that portlet developed on one java server can freely run on any other java server supporting JSR 286. Standards are something like this
1. Have a portlet.xml file in WEB-INF folder
2. Define your portlet in this xml file. Every portlet will point to a class.
3. Class must implement javax.portlet.Portlet interface. (As default implementation is already provided by java in javax.portlet.GenericPortlet class. We can directly extend it)
4. If you are implementing Portlet interface then you need to implement init, processAction, render, destroy method
5. If you are extending GenericPortlet then you can directly write doDispatch, doView, doEdit etc method. These methods will be called based on portlet mode.

There are many others but its enough to start with.

Similarity with Servlet Specifications: JSR 286 is a bit similar to Servlet specifications.
1. We define our servlet in web.xml, which is inside WEB-INF folder
2. Every servlet entry points to a class.
3. Class needs to implement javax.servlet.Servlet interface (or extent javax.servlet.http.HttpServlet)
4. If you are implementing Servlet interface you need to implement init, service etc methods
3. If you are extending javax.servlet.http.HttpServlet then you can directly write doGet, doPost etc.

Creating JSR 286 portlet:
JDEV provides mainly three ways of creating portlets
1. Using Oracle-PDK Java Portlet
2. Using Standard based java portlet (JSR286)
3. Exposing ADF tasflows as portlet.

We are going to create standard based portlets (JSR 286).
Note: JSR 286 is new standard for portlet development. It is also called Portlet 2.0 standards. Before this we had JSR 168. In old JDEV version you will see JSR 168 only.

        Click next - next - next - finish in other stops

We are kind of done with our initial portlet. Lets check entries created by JDEV
1. Project structure:

2. portlet.xml:

3. doDispatch method of GenericPortlet transfers all request to doDispatch method first. If we override this it means all request to the portlet will come to this method. Lets see default generated code of this method.

4. view.jsp:

5. edit.jsp:

6. processAction method of This method is called when end user clicks OK/Apply button when portlet is in edit mode.

With that our whole code is ready.

But to test it we need to deploy our portlet on a portlet producer. We will do it in next blog.

In a nutshell request response cycles will be something like