File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Portals and Portlets and the fly likes develop a simple Hello World Portlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Portals and Portlets
Bookmark "develop a simple Hello World Portlet?" Watch "develop a simple Hello World Portlet?" New topic
Author

develop a simple Hello World Portlet?

Rohit Lal
Greenhorn

Joined: Sep 25, 2003
Posts: 20
Hi All,

I have been suffering for the past few days working with WSAD 5.1.2 with the Portal Toolkit option to enable portlet development. I am using Websphere Portal Server 5.1 as my test environment.

I have developed a JSR-168 portlet, but it refuses to work. I don't know what I have missed, since I'm a complete greenhorn. I have the following files in the project:

OnePortlet.java
===============

package com.first.portlet.one;

import java.io.IOException;
import java.io.PrintWriter;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.Portlet;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.GenericPortlet;

public class OnePortlet extends GenericPortlet
{
protected void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException
{
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}

web.xml
=======
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="first">
<servlet>
<servlet-name>OnePortlet</servlet-name>
<servlet-class>com.ibm.wps.pe.pc.std.core.PortletServlet</servlet-class>
<init-param>
<param-name>servlet-oid</param-name>
<param-value>OnePortlet</param-value>
</init-param>
<init-param>
<param-name>servlet-class</param-name>
<param-value>com.first.portlet.one.OnePortlet</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>OnePortlet</servlet-name>
<url-pattern>/OnePortlet/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<taglib id="PortletTLD">
<taglib-uri>http://java.sun.com/portlet</taglib-uri>
<taglib-location>/WEB-INF/tld/std-portlet.tld</taglib-location>
</taglib>
</web-app>


portlet.xml
==============
<?xml version="1.0" encoding="UTF-8"?>
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd" version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd">
<portlet id="Portlet_1">
<description>One Portlet description.</description>
<portlet-name>OnePortlet</portlet-name>
<display-name>One Portlet Display Name</display-name>
<portlet-class>com.first.portlet.one.OnePortlet</portlet-class>
<expiration-cache>-1</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>VIEW</portlet-mode>
</supports>
<supported-locale>en</supported-locale>
<resource-bundle>first.nl.FirstPortletResource</resource-bundle>
<portlet-info>
<title>OnePortlet</title>
<keywords>One, Portlet, One Portlet</keywords>
</portlet-info>
</portlet>
</portlet-app>

I get this huge exception trace starting with a message that says "prepare method was not called". Given below is the exception trace. Any ideas on what I'm missing?

[12/23/05 18:58:30:344 IST] 7b9af804 WebGroup I SRVE0180I: [First.war] [/first] [Servlet.LOG]: OnePortlet: init
[12/23/05 18:58:30:844 IST] 7b9af804 ServletInstan E SRVE0100E: Did not realize init() exception thrown by servlet OnePortlet: javax.servlet.ServletException: The prepare method was never called
at com.ibm.wps.pe.pc.std.core.PortletServlet.init(PortletServlet.java:98)
at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doInit(StrictServletInstance.java:82)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._init(StrictLifecycleServlet.java:147)
at com.ibm.ws.webcontainer.servlet.PreInitializedServletState.init(StrictLifecycleServlet.java:270)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.init(StrictLifecycleServlet.java:113)
at com.ibm.ws.webcontainer.servlet.ServletInstance.init(ServletInstance.java:189)
at javax.servlet.GenericServlet.init(GenericServlet.java:258)
at com.ibm.ws.webcontainer.webapp.WebAppServletManager.addServlet(WebAppServletManager.java:876)
at com.ibm.ws.webcontainer.webapp.WebAppServletManager.loadServlet(WebAppServletManager.java:224)
at com.ibm.ws.webcontainer.webapp.WebAppServletManager.getServletReference(WebAppServletManager.java:455)
at com.ibm.ws.webcontainer.webapp.WebApp.getServletReference(WebApp.java:652)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcherInfo.calculateInfo(WebAppRequestDispatcherInfo.java:187)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcherInfo.<init>(WebAppRequestDispatcherInfo.java:68)
at com.ibm.ws.webcontainer.webapp.WebApp.getRequestDispatcher(WebApp.java:1464)
at com.ibm.ws.webcontainer.webapp.WebApp.getRequestDispatcher(WebApp.java:1423)
at com.ibm.ws.webcontainer.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:276)
at com.ibm.ws.webcontainer.cache.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:71)
at com.ibm.ws.webcontainer.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:182)
at com.ibm.ws.webcontainer.oselistener.OSEListenerDispatcher.service(OSEListener.java:334)
at com.ibm.ws.webcontainer.http.HttpConnection.handleRequest(HttpConnection.java:56)
at com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java:615)
at com.ibm.ws.http.HttpConnection.run(HttpConnection.java:439)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:912)
---- Begin backtrace for Nested Throwables
java.lang.IllegalStateException: The prepare method was never called
at org.apache.pluto.PortletContainerServices.get(PortletContainerServices.java:38)
at org.apache.pluto.services.factory.FactoryManager.getService(FactoryManager.java:42)
at org.apache.pluto.services.factory.FactoryManager.getFactory(FactoryManager.java:37)
at org.apache.pluto.util.ObjectIDAccess.getObjectIDFactory(ObjectIDAccess.java:33)
at org.apache.pluto.util.ObjectIDAccess.createObjectID(ObjectIDAccess.java:28)
at com.ibm.wps.pe.pc.std.core.PortletServlet.init(PortletServlet.java:82)
at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doInit(StrictServletInstance.java:82)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._init(StrictLifecycleServlet.java:147)
at com.ibm.ws.webcontainer.servlet.PreInitializedServletState.init(StrictLifecycleServlet.java:270)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.init(StrictLifecycleServlet.java:113)
at com.ibm.ws.webcontainer.servlet.ServletInstance.init(ServletInstance.java:189)
at javax.servlet.GenericServlet.init(GenericServlet.java:258)
at com.ibm.ws.webcontainer.webapp.WebAppServletManager.addServlet(WebAppServletManager.java:876)
at com.ibm.ws.webcontainer.webapp.WebAppServletManager.loadServlet(WebAppServletManager.java:224)
at com.ibm.ws.webcontainer.webapp.WebAppServletManager.getServletReference(WebAppServletManager.java:455)
at com.ibm.ws.webcontainer.webapp.WebApp.getServletReference(WebApp.java:652)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcherInfo.calculateInfo(WebAppRequestDispatcherInfo.java:187)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcherInfo.<init>(WebAppRequestDispatcherInfo.java:68)
at com.ibm.ws.webcontainer.webapp.WebApp.getRequestDispatcher(WebApp.java:1464)
at com.ibm.ws.webcontainer.webapp.WebApp.getRequestDispatcher(WebApp.java:1423)
at com.ibm.ws.webcontainer.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:276)
at com.ibm.ws.webcontainer.cache.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:71)
at com.ibm.ws.webcontainer.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:182)
at com.ibm.ws.webcontainer.oselistener.OSEListenerDispatcher.service(OSEListener.java:334)
at com.ibm.ws.webcontainer.http.HttpConnection.handleRequest(HttpConnection.java:56)
at com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java:615)
at com.ibm.ws.http.HttpConnection.run(HttpConnection.java:439)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:912)

[12/23/05 18:58:31:047 IST] 7b9af804 WebGroup E SRVE0020E: [Servlet Error]-[OnePortlet]: Failed to load servlet: java.lang.IllegalStateException: The prepare method was never called
at org.apache.pluto.PortletContainerServices.get(PortletContainerServices.java:38)
at org.apache.pluto.services.factory.FactoryManager.getService(FactoryManager.java:42)
at org.apache.pluto.services.factory.FactoryManager.getFactory(FactoryManager.java:37)
at org.apache.pluto.util.ObjectIDAccess.getObjectIDFactory(ObjectIDAccess.java:33)
at org.apache.pluto.util.ObjectIDAccess.createObjectID(ObjectIDAccess.java:28)
at com.ibm.wps.pe.pc.std.core.PortletServlet.init(PortletServlet.java:82)
at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doInit(StrictServletInstance.java:82)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._init(StrictLifecycleServlet.java:147)
at com.ibm.ws.webcontainer.servlet.PreInitializedServletState.init(StrictLifecycleServlet.java:270)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.init(StrictLifecycleServlet.java:113)
at com.ibm.ws.webcontainer.servlet.ServletInstance.init(ServletInstance.java:189)
at javax.servlet.GenericServlet.init(GenericServlet.java:258)
at com.ibm.ws.webcontainer.webapp.WebAppServletManager.addServlet(WebAppServletManager.java:876)
at com.ibm.ws.webcontainer.webapp.WebAppServletManager.loadServlet(WebAppServletManager.java:224)
at com.ibm.ws.webcontainer.webapp.WebAppServletManager.getServletReference(WebAppServletManager.java:455)
at com.ibm.ws.webcontainer.webapp.WebApp.getServletReference(WebApp.java:652)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcherInfo.calculateInfo(WebAppRequestDispatcherInfo.java:187)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcherInfo.<init>(WebAppRequestDispatcherInfo.java:68)
at com.ibm.ws.webcontainer.webapp.WebApp.getRequestDispatcher(WebApp.java:1464)
at com.ibm.ws.webcontainer.webapp.WebApp.getRequestDispatcher(WebApp.java:1423)
at com.ibm.ws.webcontainer.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:276)
at com.ibm.ws.webcontainer.cache.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:71)
at com.ibm.ws.webcontainer.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:182)
at com.ibm.ws.webcontainer.oselistener.OSEListenerDispatcher.service(OSEListener.java:334)
at com.ibm.ws.webcontainer.http.HttpConnection.handleRequest(HttpConnection.java:56)
at com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java:615)
at com.ibm.ws.http.HttpConnection.run(HttpConnection.java:439)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:912)

[12/23/05 18:58:31:094 IST] 7b9af804 WebGroup E SRVE0026E: [Servlet Error]-[OnePortlet]: java.lang.IllegalStateException: The prepare method was never called
at org.apache.pluto.PortletContainerServices.get(PortletContainerServices.java:38)
at org.apache.pluto.services.factory.FactoryManager.getService(FactoryManager.java:42)
at org.apache.pluto.services.factory.FactoryManager.getFactory(FactoryManager.java:37)
at org.apache.pluto.util.ObjectIDAccess.getObjectIDFactory(ObjectIDAccess.java:33)
at org.apache.pluto.util.ObjectIDAccess.createObjectID(ObjectIDAccess.java:28)
at com.ibm.wps.pe.pc.std.core.PortletServlet.init(PortletServlet.java:82)
at com.ibm.ws.webcontainer.servlet.StrictServletInstance.doInit(StrictServletInstance.java:82)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet._init(StrictLifecycleServlet.java:147)
at com.ibm.ws.webcontainer.servlet.PreInitializedServletState.init(StrictLifecycleServlet.java:270)
at com.ibm.ws.webcontainer.servlet.StrictLifecycleServlet.init(StrictLifecycleServlet.java:113)
at com.ibm.ws.webcontainer.servlet.ServletInstance.init(ServletInstance.java:189)
at javax.servlet.GenericServlet.init(GenericServlet.java:258)
at com.ibm.ws.webcontainer.webapp.WebAppServletManager.addServlet(WebAppServletManager.java:876)
at com.ibm.ws.webcontainer.webapp.WebAppServletManager.loadServlet(WebAppServletManager.java:224)
at com.ibm.ws.webcontainer.webapp.WebAppServletManager.getServletReference(WebAppServletManager.java:455)
at com.ibm.ws.webcontainer.webapp.WebApp.getServletReference(WebApp.java:652)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcherInfo.calculateInfo(WebAppRequestDispatcherInfo.java:187)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcherInfo.<init>(WebAppRequestDispatcherInfo.java:68)
at com.ibm.ws.webcontainer.webapp.WebApp.getRequestDispatcher(WebApp.java:1464)
at com.ibm.ws.webcontainer.webapp.WebApp.getRequestDispatcher(WebApp.java:1423)
at com.ibm.ws.webcontainer.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.java:276)
at com.ibm.ws.webcontainer.cache.invocation.CachedInvocation.handleInvocation(CachedInvocation.java:71)
at com.ibm.ws.webcontainer.srp.ServletRequestProcessor.dispatchByURI(ServletRequestProcessor.java:182)
at com.ibm.ws.webcontainer.oselistener.OSEListenerDispatcher.service(OSEListener.java:334)
at com.ibm.ws.webcontainer.http.HttpConnection.handleRequest(HttpConnection.java:56)
at com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java:615)
at com.ibm.ws.http.HttpConnection.run(HttpConnection.java:439)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:912)


Rohit Lal<br />SCPJ2 (2000)
Stefan Zoerner
Author
Ranch Hand

Joined: Nov 29, 2001
Posts: 121
Rohit,

it looks like you are mixing the two ways of creating portlets in WebSphere Portal. In the new JSR 168 style, a portlet is not a servlet (in the vendor specific portlet API, it was).

It is therefore not necessary (probably not even valid) to declare the portlet as a servlet in web.xml. A short file like this should do:



Even the taglib is not necesary in your case, because you do not use JSP (yet).

I hope this solves your starting issue.
Greetings from Hamburg,
Stefan


Stefan Zoerner
Author of German LDAP-Book
Committer at Apache Directory Project
Stefan Zoerner
Author
Ranch Hand

Joined: Nov 29, 2001
Posts: 121
Here is another thing -- more a suggestion. This has nothing to do with your error at startup.

It is highly recommended to set the content type in the render phase of the portlet life cycle.
In you case something like



It will probably work in webSphere without, but there are portlet containers which require the content type.

Greetings Stefan
Rohit Lal
Greenhorn

Joined: Sep 25, 2003
Posts: 20
Thanks Stefan,

I tried it out, but am now not getting any reponse at all.

When I try the URL giving the portlet name after the web-app name, nothing happens, no error - nothing.
http://192.100.100.80:9086/first/OnePortlet

The "OnePortlet" comes from the <portlet-name> tag in portlet.xml.

I even put in a init(..) method in the portlet with a System.out msg inside, but even that was not invoked. Perhaps there is something wrong with portlet.xml also?

Plus, how will the portlet be registered as a web component in this application? I see there is no mention of it in web.xml?

I appreciate your reponse very much. I know I've asked very beginner level questions, so please do excuse me.

Best regards, Rohit
Stefan Zoerner
Author
Ranch Hand

Joined: Nov 29, 2001
Posts: 121
Rohit,

you can't access a portlet directly via an URL. Portlets are used by a portal server (WebSphere Portal in your case) to compose pages. Even for a page, it is not required that it has a reusable (static) URL (although you can define one for a page in WebSphere Portal).

A portlet application is a special web application, and the portlet.xml file describes which classes are your portlet(s). This is what you have already done in you portlet.xml. If you deploy it to a JSR 168 compliant portal, it is vendor specific how to add it to a page, assign access rights (if necessary) and so on. You use a WAR-file for the deployment, same as in WebApps (but with the additional XML file). One option for WebSphere Portal is to upload the file via the Portal Administrative Console. With a test environment, it is easier to run it.

If you have a running WebSphere Portal Test Environment with your IDE, you can access it with a browser like that:
http://localhost:9081/wps/portal/
The port my differ, see the logs for the HttpTransport line.

To run a portlet within you portal, add the Portlet Application to an EAR, and add this project to your server (Servers perspective), that is you associate the portlet project with your test environment.
Details: see online help (Rational Application Developer -- Developing portal and portlet applications)

Greetings, Stefan
Rohit Lal
Greenhorn

Joined: Sep 25, 2003
Posts: 20
Thanks Stefan,

Obviously I have still a lot to learn!! I am currently out of town, but will try this as soon as I get back. Thank you very much for your kind assistance, again.

Best regards,

Rohit
Rohit Lal
Greenhorn

Joined: Sep 25, 2003
Posts: 20
Stephan:

I finally got the portlet working. I used the code as you had suggested and ran the portlet project itself. I did this on Rational Application Developer since it seems a more integrated tool to develop portlets than WSAD.

Also, in WSAD i couldn't get to publish the portlet project in the in-built test environment and was having to publish it remotely onto the portal server. I suppose the portal toolkit doesn't take care of this? I did not have any problems in using RAD's in-built test environment.

Thanks very much for your help. After this simple hello world portlet, I will try and progress onto to portlets with their full functionalities.

Regards,

Rohit
Stefan Zoerner
Author
Ranch Hand

Joined: Nov 29, 2001
Posts: 121
Rohit,

nice to here from your success! I like RAD as well -- especially with combined JavaServer Pages development for the portlet views, if you use JSP for rendering your portlet fragments. If not, this would be a natural next step for your learning progress (a PortletRequestDispatcher to include JSPs in the render phases for the different portlet modes).

Good luck,
Stefan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: develop a simple Hello World Portlet?