This article will take you to the steps of creation and deploying a simple portlet in WebSphere Portal Version 5.
Introduction, some people often ask me this question: "If you start from the beginning, what is the simplest portlet created by WebSphere Portal Version 5?". The fixed answer is "Hello World". This article will take a long time to answer this question in detail. I am going to take you to learn how to create the easiest Protlet for WebSphere Portal V5. You will start from some Java code, then compile it, then package it. Next, you will create the deployment descriptor required for the application server and portal information about the information about portlets. Finally, we pack them together and deploy new portlets to the portal.
Creating a directory structure first creates a directory structure and you will create a portlet in this directory structure. Below is the directory structure I will use in this simple portlet:
HelloWorld / COM / IBM / Portlets / Sample - Position of the source code HelloWorld / Web-INF - Deploy the location of the descriptor HelloWorld / WEB-INF / LIB - JAR file storage
Creating a Java file Sample directory is where you will store the Java source code. Create a file called HelloWorld.java in the sample directory, then open the file with your favorite text editor. Here is the class you need to enter (or copy, paste) to the HelloWorld.java file:
Package com.ibm.portlets.sample;
// portlet APIT
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");
}
}
Compiling Code Once you have created a source file, you can compile the Java code. In a batch file, you can use the following scripts to compile portlets. You first define some environment variables (for example, the location of the Java home directory). The JDK with WebSphere Application Server is compiled for this is usually a good idea, because this is the environment you want to run. You have to set the PATH to access the Java compiler. In addition, it is also necessary to set the variable libpath to point to the directory where the WebSphere JAR file is located.
Next, you build a variable called CP for our compile class path. The CP variable can be constructed on one line, but to illustrate the different JAR files required, we disassemble the branch display. Then, you call the Java compiler to compile the code. This assumes that you are located in the HelloWorld directory. Please adjust the directory path to the path that is fit.
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/portalser/shared/app/portlet-api.jar
Javac-ClassPath% CP% COM / IBM / Portlets / Sample / HelloWorld.java
If you can't compile some reason, you must have a correct error before going down. Some common mistakes are:
Enter an error - class name, path, and variable name Enter the error file name - the file name must match the class name. In our example, the file name is HelloWorld.java, and the class name is HelloWorld. If you have changed a name, another name is also changed. Editor Error - Determines the editor to store files as text. Like a editor like writing, you will not store the file to text by default.
Creating a JAR file After completing the compilation of the Java source file, you need to create a JAR file in a web-inf / lib directory. Have a hypothesis that you are located in the HelloWorld directory. If you create a batch file in the Compile Code section, you can include part of the file as part of the file. This depends on the PATH set to point to the JAR 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 a 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 Web Deployment Descriptor is required for WebSphere Portal. Now the portlet extends servlet, so you need to use the web deployment descriptor to declare the Servlet (portlet) class.
XML Version =
"1.0" encoding =
"UTF-8"?>
Public "- // Sun microsystems, Inc.//dtd Web Application 2.3 // en"
"http://java.sun.com/dtd/web-app_2_3.dtd">
/ Display-name>
/ servlet-name>
/ servlet-clas>
servlet>
"Servletmapping_1"> / servlet-name> / url-pattern> servlet-maping> web-app> portlet.xml - Portlet Deployment Descriptor Portlet Deployment Descriptor Defines the portal. Each portlet is mapped to a servlet defined in the web deployment descriptor through the HREF property of the portlet element. These references are represented by bold. Portlets must define a unique ID, each of which can reference the ID. Each specific portlet uses the specific ID of the HREF attribute of the Concrete-Portlet element to reference the portlet. Represents these references in blue. XML Version = "1.0" encoding = "UTF-8"?>
PUBLIC "- // ibm // DTD portlet application 1.1 // en" "portlet_1.1.dtd"> "com.ibm.ports.sample.helloWorld.1" major-version = "1" Minor-version = "0"> / portlet-app-name> Minor-verion = "0"> / portlet-name> / expresss> / shared> cache> allows> "html"> Markup> supports> portlet> portlet-app> "com.ibm.ports.sample.helloWorld.1.2"> / portlet-app-name> / param-name> / param-value> context-param> / portlet-name> / Default-local> "en"> logage> concrete-portlet> concrete-portlet-app> portlet-app-def> Many places will be wrong when you create these XML files. These errors are only found when you try to deploy portlets to the portal. There are some places that need special attention: Check each XML element to make sure they have an off element. Check if the name is incorrect, for this example, check if the name of your giving class is HelloWorld. Make sure the ID and HREF match. Make sure the editor really saves files into text. Creating a WAR file Finally, we can create a distributed WAR file. We will use standard JAR commands to build a WAR file. Run the following command in the HelloWorld directory: Set java_home = c: / WebSphere / AppserVer / Java SET PATH =% java_home% / bin Jar -cf HelloWorld.war Web-INF Deploy WAR file Log in to the portal in the identity (wpsadmin) of the portal administrator. Select the Administration link in the upper right corner of the default theme. Figure 1. The administrator links to the left to select the portlets page and select Install portlet. Figure 2. Install the portlet management page Click. Find the location of the WAR file, selected, and then click. Figure 3. File Selection Dialog Click. This may take some time, please wait patiently to complete it. 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 you encounter a class error message during the deployment process, this usually means that you have an error in an XML file. Carefully analyze the installation page presentation to your error message, you usually find the reason for the error. This information will also be recorded in the latest log files:% wps_home% / log / wps_yyyy.mm.dd-hh.mm.ss.log Creating a deployment descriptor lists common errors for XML files. You should also check the following: The XML declaration must be on the first line of the file (no space). In the case of correct case, the XML file has the correct name. Add portlet to a page Select the Portal User Interface page on the left. Click on the Manage Pages Portlet. Figure 5. Management page portlet then select My Portal page. Click. Type a title for the new page. Click. I saw later. Click the Edit page icon on the side of the new page. Click. Search Hello, click. Check the Hello World Portlet and click. Figure 6. Find portlets Click to complete the page layout design. Figure 7. Editing Layout Design Click the My Portal link in the upper right corner, then select the page you created. Figure 8. Portal showing the portlet we created End language now you have created a new portlet, you should be happy! You now know how to create a simple portlet from your head. First write, compile and package Java code. Then create a deployment descriptor and package the portlet to distribute and deploy. Finally, deploy portlets into your portal. From now on, writing any portlet is possible for you. I wish you good luck and have fun! Back to top Reference WebSphere Portal Product Documentation WebSphere Portal Zone