Hello World -- WebSphere Portal V5 simplest Portlet: Part 1 creation and deployment

Source: Internet
Author: User
Tags websphere application server

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.javaAnd then open the file in your favorite text editor. You need to input (or copy or paste)HelloWorld.javaFile 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 isHelloWorld.javaThe 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/libDirectory to create a jar file. Assume that you arehelloWorldDirectory. 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 pointjarProgram.

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 usesportletElementhrefProperty 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-portletElementhrefThe 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 isHelloWorld.
  • 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 standardjarCommand to build the war file. InhelloWorldRun 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

  1. As the portal Administrator (wpsadmin) Log on to the portal.
  2. In the upper-right corner of the default topic, selectAdministrationLink.

    Figure 1. Administrator Link

  3. Select on the leftPortletsPage, and selectInstallPortlet.

    Figure 2. Install the Portlet Management page

  4. Click.
  5. Locate the location of the war file, select, and click.

    Figure 3. file selection dialog box

  6. Click. This may take some time. Please wait for it to complete.
  7. Verify that the Portlet is helloworld. Click. Wait again until the installation is complete.

    Figure 4. Portlet installation verification Screen

  8. 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

  1. Select on the leftPortal user interfacePage.
  2. ClickManage pagesPortlet.

    Figure 5. Management page Portlet

  3. Then selectMy portalPage.
  4. Click.
  5. Enter a title for the new page.
  6. Click.
  7. Click.
  8. Click the Edit page icon next to the new page.
  9. Click.
  10. SearchHello, And click.
  11. Select the hello World Portlet and click.

    Figure 6. Search for portlets

  12. Click to complete the page layout design.

    Figure 7. Edit Layout Design

  13. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.