This article will show you how to create and deploy a simple Portlet in WebSphere Portal version 5.
Introduction
Someone often asked me this question: "What is the simplest Portlet that people can create through WebSphere Portal version 5 from the beginning ?". The fixed answer is "Hello World ". This article will take a long time to answer this question in detail. I am going to show you how to create the simplest protlet for WebSphere Portal V5. You will start with some java code, compile it, And then package it. Next, you will create a deployment descriptor that tells the application server and the portal website about the Portlet information. Finally, we package it together and deploy the new Portlet to the portal website.
Create directory structure
First, create a directory structure in which you will create a Portlet. Below is the directory structure that I will use for this simple Portlet:
helloWorld/com/ibm/portlets/sample
-- Location where the source code is stored
helloWorld/WEB-INF
-- Location of the deployment descriptor
helloWorld/WEB-INF/lib
-- Location where jar files are stored
Create a Java File
The sample directory is where you will store the Java source code. Create a file namedHelloWorld.java
And then open the file in your favorite text editor. You need to input (or copy or paste)HelloWorld.java
File class:
package com.ibm.portlets.sample; //portlet APIs import org.apache.jetspeed.portlet.*; //Java stuff import java.io.*; public class HelloWorld extends PortletAdapter { public void service(PortletRequest request, PortletResponse response) throws PortletException, IOException { PrintWriter writer = response.getWriter(); writer.println("hello, world"); } }
|
Compile code
Once you have created the source file, you can compile the Java code. In a batch file, you can use the following script to compile the Portlet. You must first define some environment variables (for example, the location of the Java main directory ). It is usually a good idea to compile with the JDK that comes with WebSphere Application Server, because this is the environment in which you will run the application. You also need to set the path to access the Java compiler. In addition, you need to set the variable libpath to the directory where the WebSphere JAR file is located.
Next, you construct a variable named CP for our compilation class path. The CP variable can be built on one line, but to illustrate the different jar files required, we will split the branch and display it. Then, you call the Java compiler to compile the code. This assumes that you are in the helloworld directory. Change the directory path to a suitable installation path.
set JAVA_HOME=C:/WebSphere/AppServer/java set PATH=%JAVA_HOME%/bin set LIBPATH=C:/WebSphere/AppServer/lib set CP=. set CP=%CP%;%LIBPATH%/j2ee.jar set CP=%CP%;%LIBPATH%/dynacache.jar set CP=%CP%;C:/WebSphere/PortalServer/shared/app/portlet-api.jar javac -classpath %CP% com/ibm/portlets/sample/HelloWorld.java
|
If this cannot be compiled for some reason, you must correct the error before proceeding. Some common errors are:
- Input error -- incorrect class name, path, and variable name
- File name -- the file name must match the class name. In our example, the file name is
HelloWorld.java
The class name isHelloWorld
. If you have changed one name, the other name also needs to be changed.
- Editor error -- make sure the editor stores the file as text. An editor like a WordPad does not store files as text by default.
Create a JAR File
After compiling the Java source fileWEB-INF/lib
Directory to create a jar file. Assume that you arehelloWorld
Directory. If you create a batch file in the compilation code section, you can include these statements as part of the file. It depends on the path you have set to pointjar
Program.
set JAVA_HOME=C:/WebSphere/AppServer/java set PATH=%JAVA_HOME%/bin jar -cv0f ./WEB-INF/lib/HelloWorld.jar com/ibm/portlets/sample/*.class
|
Create deployment descriptor
Now, you need to create two XML files:
helloWorld/WEB-INF/web.xml
-Web deployment descriptor.
helloWorld/WEB-INF/portlet.xml
-Portlet deployment descriptor.
Web. XML-web deployment descriptor
The Web deployment descriptor is required by WebSphere Portal. Now the Portlet extends the servlet, so you need to use the Web deployment descriptor to declare the servlet (Portlet) class.
<?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="HelloWorldWebApp"> <display-name>HelloWorldPortlet< /display-name> <servlet id="Servlet_1"> <servlet-name>HelloWorld< /servlet-name> <servlet-class>com.ibm.portlets.sample.HelloWorld< /servlet-class> </servlet> <servlet-mapping id= "ServletMapping_1"> <servlet-name>HelloWorld< /servlet-name> <url-pattern>/HelloWorld/*< /url-pattern> </servlet-mapping> </web-app>
|
Portlet. XML-Portlet deployment descriptor
The Portlet deployment descriptor defines the Portlet to the portal. Each Portlet usesportlet
Elementhref
Property is mapped to a servlet defined in the Web deployment descriptor. ToBoldThese references. The Portlet must define a unique ID, which can be referenced by each specific Portlet. Use of each specific Portletconcrete-portlet
Elementhref
The specific ID of the property to reference the Portlet. These references are displayed in blue.
<?xml version= "1.0" encoding= "UTF-8"?> <!DOCTYPE portlet-app-def PUBLIC "-//IBM//DTD Portlet Application 1.1//EN" "portlet_1.1.dtd"> <portlet-app-def> <portlet-app uid= "com.ibm.portlets.sample.HelloWorld.1" major-version="1" minor-version="0"> <portlet-app-name>HelloWorld0Portlet< /portlet-app-name> <portlet href="WEB-INF/web.xml#Servlet_1"id="Portlet_1" major-verion="1" minor-verion="0"> <portlet-name>HelloWorld< /portlet-name> <cache> <expires>0< /expires> <shared>no< /shared> </cache> <allows> <maximized/> <minimized/> </allows> <supports> <markup name= "html"> <view/> </markup> </supports> </portlet> </portlet-app> <concrete-portlet-app uid= "com.ibm.portlets.sample.HelloWorld.1.2"> <portlet-app-name>Concrete HelloWorld< /portlet-app-name> <context-param> <param-name>Author< /param-name> <param-value>tcat@us.ibm.com< /param-value> </context-param> <concrete-portlet href="#Portlet_1"> <portlet-name>HelloWorld< /portlet-name> <default-locale>en< /default-locale> <language locale= "en"> <title>Hello World</title> <title-short></title-short> <description></description> <keywords></keywords> </language> </concrete-portlet> </concrete-portlet-app> </portlet-app-def>
|
Errors may occur in many places when these XML files are created. These errors are discovered only when you try to deploy the Portlet to the portal. Here are some notes:
- Check every XML element to make sure that it has a closed element.
- Check whether the class name is entered incorrectly. In this example, check whether the name you give to the class is
HelloWorld
.
- Make sure that the ID and href match.
- Make sure that the editor does save the file as text.
Create a war File
Finally, we can create war files for distribution. We will use the standardjar
Command to build the war file. InhelloWorld
Run the following command in the directory:
set JAVA_HOME=C:/WebSphere/AppServer/java set PATH=%JAVA_HOME%/bin jar -cf HelloWorld.war WEB-INF
|
Deploy war files
- As the portal Administrator (
wpsadmin
) Log on to the portal.
- In the upper-right corner of the default topic, selectAdministrationLink.
Figure 1. Administrator Link
- Select on the leftPortletsPage, and selectInstallPortlet.
Figure 2. Install the Portlet Management page
- Click.
- Locate the location of the war file, select, and click.
Figure 3. file selection dialog box
- Click. This may take some time. Please wait for it to complete.
- Verify that the Portlet is helloworld. Click. Wait again until the installation is complete.
Figure 4. Portlet installation verification Screen
- Finally, you will see this confirmation message :.
If an error occurs during deployment, it usually means that you have encountered an error in an XML file. Carefully analyze the error information displayed on the installation page. You can usually find the cause of the error. This information is also recorded in the latest log file:
%WPS_HOME%/log/wps_YYYY.MM.DD-HH.MM.SS.log
The create deployment descriptor lists Common Errors in XML files. You should also check the following:
- The XML Declaration must be in the first line of the file (No space ).
- The XML file has a correct name in case of a correct case.
Add a Portlet to a page
- Select on the leftPortal user interfacePage.
- ClickManage pagesPortlet.
Figure 5. Management page Portlet
- Then selectMy portalPage.
- Click.
- Enter a title for the new page.
- Click.
- Click.
- Click the Edit page icon next to the new page.
- Click.
- Search
Hello
, And click.
- Select the hello World Portlet and click.
Figure 6. Search for portlets
- Click to complete the page layout design.
Figure 7. Edit Layout Design
- In the upper-right corner, click the my portal link and select the page you created.
Figure 8. portal of the created Portlet
Conclusion
Now you have created a new Portlet! Now you know how to create a simple Portlet from scratch. First, write, compile, and package Java code. Create a deployment descriptor and package the Portlet for distribution and deployment. Finally, deploy the Portlet to your portal.
From now on, writing any Portlet is possible for you. Good luck and good fun!
Back to Top
References
- WebSphere Portal product documentation
- WebSphere Portal Zone