Showing posts with label tutorial. Show all posts
Showing posts with label tutorial. Show all posts

Thursday, January 24, 2008

Portlet Tutorial - Deploying your first portlet to OpenPortal Portlet Container 2.0

This post is a part of the Portlet 2.0 (JSR 286) Tutorial series


At the time of this writing, only Sun seems to have a functional JSR 286 portlet container. Apache Pluto 2.0 is work in progress and I have been unsuccessful in running the Exo Portlet Container.


The OpenPortal portlet container is Sun Microsystem's implementation of the JSR 286 spec. Here is how you would deploy the Hello World portlet to this container.

Step 1 :

Get JDK 5 or greater: http://java.sun.com/javase/downloads/index.jsp

Step 2 :

Get the Glassfish Application Server or Tomcat
If you downloaded Glassfish, set it up first.

Step 3 :

Download the latest stable release of the portlet container.

Step 4 :

Follow the install instructions: Installing the Portlet Container

Step 5:

Start your server and access the portlet container at http://localhost:8080/portletdriver

Step 6:

Once you have the portlet container running, you will notice two tabs. A Portlets tab and an Admin tab.The Portlets tab helps view all the portlets currently deployed. The Admin tab is used to to deploy/undeploy portlets. You are most likely to see a blank page. This is because no portlets are deployed by default.

Click on the Admin tab and proceed to deploy the portlet.


















Once you click on deploy, the portlet is automatically deployed and configured. Click on the Portlets tab to now view the portlet.












You have successfully run your first portlet !

Tuesday, January 15, 2008

Portlet Tutorial - Anatomy of a portlet

This post is a part of the Portlet 2.0 (JSR 286) Tutorial series

Now that we have written and successfully deployed our Hello World portlet, let's understand it's various components.Here is how our portlet would look when deployed in a container.


Notice how the portal has created a distinct boundary or window for our portlet. This is called a Portlet Window. Each portlet is visually contained in a portlet window. In addition to the portlet content (i.e the "Hello Portlet 2.0 World" message that we printed in the render method), the portlet window also contains the portlet title (we mentioned this in the portlet.xml) and some click-able controls called decorations. These decorations help control two different aspects of a portlet - modes and window states.

Portlet Modes and Portlet Window States

At any given point in time, all portlets can defined by in terms of two unique characteristics - what the portlet is currently doing and how much space the portlet is taking up on the page. The Portlet Mode defines what the portlet is currently doing. The Portlet Window State defines how much space a portlet takes up in a particular page.

For example, we could have written a help message for the Hello World Portlet and displayed it when a user clicks on the Help button (the decoration with a "?" symbol). In this case the portlet would be in the "Help" mode. Similarly we could have written different "Hello World" messages based on how much space the portlet occupies on the page. This is possible because in each case, the portlet would be associated with different window states.

Monday, January 7, 2008

Portlet Tutorial - Hello Portlet 2.0 World

This post is a part of the Portlet 2.0 (JSR 286) Tutorial series

As explained in the previous post, a portlet is a pluggable component that can be run inside any compliant portal server. Here is an example of a portlet running inside a portal.


Note how the portlet occupies only a part of the portal page. This is the primary difference between a portlet and a servlet. A portlet is meant to occupy only a part of the web page. And a portal web page consists of multiple, different portlets.

Now, let's learn to write a simple Hello World Portlet. The Portlet API defines an interface called javax.portlet.Portlet. Any portlet you write must implement this interface. One easy way to do so is to extend the javax.portlet.GenericPortlet class. The GenericPortlet already implements the Portlet interface and it is strongly recommended that you always inherit GenericPortlet while writing your own portlets.

Here is our code.


public class HelloWorldPortlet extends GenericPortlet{

public void render(RenderRequest request, RenderResponse response)
throws PortletException, IOException{

response.setContentType("text/html");
response.getWriter().write("Hello Portlet 2.0 World");
}
}

We have overridden only one method of the GenericPortlet interface called render.We will look into the render method in detail in a subsequent blog post. For the time being you can assume that whenever your portlet is "shown" on the portal page, the render method is called.

Since portlets are extended components of a regular web application, they can also be packaged along with your servlets and JSPs in a war file. However you will need a separate descriptor along your regular web.xml to describe portlets. This descriptor is called the portlet descriptor and the file is called portlet.xml.

Here is the portlet.xml for our Hello World Portlet.

<portlet-app xmlns=\"http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd\"
xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
schemalocation=\"http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd
http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd\">
<portlet>
<portlet-name>HelloWorldPortlet</portlet-name>
<portlet-class>com.portalzone.example1.HelloWorldPortlet</portlet-class>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
<portlet-mode>edit</portlet-mode>
<portlet-mode>help</portlet-mode>
</supports>
<portlet-info>
<title>Hello World Portlet</title>
</portlet-info>
</portlet>
</portlet-app>


That’s it ! You have written your first portlet application. You can grab the ready to deploy war file from here: example1.war and deploy the application to your container of choice. If you are interested in the source, here it is : example1-src.zip.

Friday, December 28, 2007

Portlet Tutorial - Web Portals and Portlets

This post is a part of the Portlet 2.0 (JSR 286) Tutorial series

Introduction to Web Portals

A Web portal is a website that acts as a single point of access for a wide variety of information. Think of a site like Yahoo.On Yahoo, you can check news, shop, read your emails, chat, play games etc. You also have access to a wide varietyof other content and services provided by Yahoo. Yahoo is a good example of a Web portal.

Web portals need not always be accessible to everyone. They can exist within the confines of an organization, accessible to only people who are a part of the organization like employees, contractors, partners, suppliers etc. These portals are called Intranet Portals.Portals like Yahoo, that are open to all, are called Internet Portals.

Web Portal software

While it is possible to build a web portal like any other website using technologies like Servlet/JSP, ASP, PHP etc, today there exists a wide range of software that help build a web portal pretty quickly. For instance, If you are a .Net developer you could consider using the Microsoft Office Sharepoint Server

The advantage of using portal server software is that the software would already have most of the common portal functionality in place. All portal software will offer some means of managing your users (user management), login/logout (authentication), skins/themes (customization), creating multiple user profiles (personalization), search etc. Most portal software will offer significant out-of-the-box functionality like RSS feedreaders and email/calendar applications. Thus portal software, in general, will reduce the amount of code that you need to write to have a portal up and running. It reduces your design/code/test/debug cycles and also makes your portal easier to maintain/upgrade.

The disadvantage of using portal server software is that sometimes it might not be as flexible as you want it to be. All portals offer only a certain degree of customization. If you require something beyond that, you might be better off writing your own solution. Most portals are bulky, loaded with features and functionality. If you are looking for something that's simple, the size and complexity of the portal might come as a pain.

Java Portlets

While all portal software offer many useful out-of-the-box functionality, sometimes it is necessary to write your own custom code and "plug it" into the portal. Java portals allow you to do do this by writing a standardized plugin component called a Portlet. The Java Portlet , defined by the JSR 286, allows you to write your own code and put it into any compliant portal server. Just like you can write a servlet and deploy it to any web server/servlet container , you can write a portlet and deploy it to any portal server/portlet container.

There are a wide array of vendors that support the Portlet spec. IBM, BEA, Sun, Jboss, Liferay, Apache, Exo are a few examples.

Portlet 2.0 (JSR 286) Tutorial

In this ongoing series of blog entries I intend to provide a comprehensive guide to creating web portals using the Java Portlet technology. This tutorial assumes prior knowledge of Java Servlets and Java Server Pages (JSP).

Posts in this series

  1. Web Portals and Portlets

  2. Hello Portlet 2.0 World


  3. Anatomy of a Portlet