What Is A Portlet?

Source: Internet
Author: User

Author: Sunil Patil; Observer
Address: http://www.onjava.com/pub/a/onjava/2005/10/19/challenging-java-dominance.html

Keywords: Portlet Java

Is a Web component-just like servlets-designed to aggregate the content on the compositing page. When you request a portal page, multiple portlets will be called.
. Each Portlet generates a tag segment, which is combined with the tag segments generated by other portlets and embedded in the tag on the portal page ." (From the Portlet rules
Fan, JSR 168)

This article discusses the following:
1. portal page elements
2. What is portal?
3. What is portlets?
4. Develop the "Hello World" Portlet
5. Deploy the helloworld Portlet on Pluto
6. How to create a portal page
7. Conclusion
8. Resources

The Portlet Specification defines the Portlet as a "Java-based Web component, managed by the Portlet container that processes requests and generates dynamic content ". Does this sound confusing? This article describes what portlets is and what it can do.

Figure 1 shows the page in the browser when accessing a Portal Server.

Figure 1 Typical Portal Server Page (click to view the source image)

If you carefully view the page in the browser, you will see that the page is composed of different "Windows. One window is used to refresh the weather, the other is used to refresh the news, and the other is used to refresh the stock price. Each
Window represents a portlets. If you have read more carefully, you will also find that each window has a title bar and some buttons, including the minimization and maximization buttons.

In the system
In the unified architecture, these windows are independently developed and different applications. The news Portlet developer creates an application and packs it into a war file. Then, the Portal Server administrator is on the server.
Deploy the war file and create a page. Then, each user selects the applications on the page. For example, if a user is not interested in the stock price but is interested in sports, he can replace the stock with the "Sports" window.
Price window.

Many new concepts need to be learned for the Portlet technology. This article cannot cover all of them. Therefore, this article is divided into two parts. In the first part, we will detail portals and portlets and develop a simple "Hello World" Portlet. In the second part, we will discuss some advanced topics.

We will use Apache's Pluto server (reference implementation of the Portlet API 1.0 specification) to test our example portlets. We will also spend some time exploring how to install and use the Pluto server.

Portal page elements

Figure 2 shows the elements of the portal page.

Figure 2 elements on the portal page

Each Portlet page consists of one or more Portlet windows. Each Portlet window is divided into two parts: one is the appearance, which determines the title bar, control, and border style of the Portlet window; the other is the Portlet segment, which is filled by the Portlet application.

The Portal Server determines the overall view of the portal page, such as the logo, title bar color, and control icon. By modifying several JSP and CSS template files, you can change the overall view of the portal. We will discuss this in depth in the "how to create a portal page" section.

What is portal?

Before learning about the Portlet, you must first understand the portal. In the Portlet specification, the portal is a Web application, which is usually used to provide personalized, Single login
Records and aggregates the content of various information sources, and acts as the host of the Information System Performance layer. Aggregation refers to the activity that integrates content from various information sources into a web page ".

Portal functions can be divided into three main aspects:
1. Portlet
Container: the Portlet container is very similar to the servlet container. All the Portlet is deployed in the Portlet container. The Portlet container controls the creation of the Portlet.
Life cycle and provide necessary resources and environment information. The Portlet container initializes and destroys portlets, sends user requests to portlets, and synthesize responses.
2. Content Aggregation: One of the main tasks of the portal defined in the Portlet specification is to aggregate content generated by various Portlet applications. We will further discuss this in the "how to create a portal page" section.
3. Public Service: one of the strengths of the Portlet server is its set of public services. These services are not required by the Portlet specification, but the commercial implementation version of the portal provides a wide range of public services to distinguish them from competitors. Several public services are expected to be found in most implementations:
O single Logon: you only need to log on to the Portal Server once to access all other applications, which means you do not need to log on to each application separately. For example, once I log on to my Intranet website, I can access the mail application, IM message application, and other Intranet applications without having to log on to these applications separately.

The Portal Server will assign you a pass library. You only need to set the user name and password once in the mail application, and the information will be stored in the pass library encrypted. After you have logged on
When an Intranet website accesses the mail application, the Portal Server will read your pass from the pass database and log on to the mail server for you. Your access to other applications will also
Handle as needed.
O personalization: the basic implementation of personalized services enables users to personalize their pages in two ways: first, users can decide the face of the title bar according to her preferences
Color and control icon. Second, you can decide which portlets are available on her page. For example, if I am a sports fan, I may use a sports fan to provide my favorite team's latest information.
Portlet to replace stock and news portlets.
Some leading commercial implementation versions in personalized services allow you to build applications that show users
Applicable standards (such as income and interest ). In this case, you can set up a Portlet like "display gifts to any user whose revenue is X" and "display discount products to any user whose revenue is X ".
Such commercial rules.

In addition, some public services, such as machine translation, are used by the Portal Server to translate the content generated by the Portlet into the language required by the user. Most commercial portal servers support access from handheld devices and can generate different content for different browser terminals.

What is portlets?

Similar to servlets, portlets is a Web Component deployed in a container to generate dynamic content. Technically, A Portlet is a class that implements the javax. Portlet. Portlet interface. It is packaged into a war file and deployed to the Portlet container.

Portlets is similar to servlets in the following aspects:
1. portlets is managed by specific containers.
2. portlets generates dynamic content.
3. the life cycle of the Portlet is managed by the container.
4. portlets interacts with the Web Client through the request/response mode.

portlets is different from servlets in the following aspects:
1. portlets can only generate tag segments, not the entire document.
2. portlets does not have a URL for direct access. However, you can still allow others to access the Portlet through the URL, and you can send the URL of the page containing the Portlet to him.
3. portlets
you cannot generate content randomly because the content generated by the Portlet is eventually part of the portal page. If the Portal Server requires the HTML/text type,
All portlets should generate html/text content. For example, if the Portal Server requires the WML type, all portlets should
Generate the WML type content.

Portlets also provides some additional features:
1. Persistent storage of set parameters: portlets provides a portletpreferences object to save the user's set parameters. These parameters are stored in a persistent database, so that the data remains valid after the server is restarted. Developers do not have to worry about the specific implementation mechanism of such data storage.
2. Please
Processing: portlets provides more fine-grained request processing. Requests sent to the Portlet when the user acts on the Portlet (A status called active period), or
The Portal Server provides two different callback methods for page refresh requests triggered by other Portlet actions.
3. Portlet
Mode: portlets uses the pattern to indicate what the user is doing. When using the mail application, you may use it to read, write, or check emails-these are mail application reservations.
Feature. portlets generally provides these features in view mode. However, there are also some activities, such as specifying the refresh time or (re-setting the user name and password, which allow users to customize the app behavior,
Therefore, they use the edit mode. The help mode is used for the mail application help function.

If you think about it, there is nothing new here, but most of them are common business requirements. The role of the Portlet specification is that it provides an abstraction layer, which is the value of the Portlet for all people-end users, developers, and administrators.

As a developer, I will put all the business logic related to the view mode into the doview () method, and put the business logic related to the application configuration into the doedit () method, add the help-related logic to the dohelp () method

This simplifies the Administrator's access control and management of the Portlet application, because it only needs to change the access permissions of the Portlet to determine what the user can do. For example, if a user of the mail application can set the user name and password in edit mode, it can be determined that the user has access permission in edit mode.

Consider this situation: I am an administrator of an Intranet website. My company bought a third-party Portlet application that displays news. This application allows users to specify a new tracking function.
I want to use the updated URL to display the company's internal news to users. Another requirement is that I don't want users to use this application to track any other news sources. As an administrator
The User specifies a URL address for internal news updates, and cancels the permission of others to modify the address by changing the deployment descriptor of the Portlet application.

Because all the Portlet applications have similar UI interfaces, using portlets can make the website more attractive to end users. If she wants to read the help information of any application, she can click the Help button. She also knows that clicking the edit button will allow her to enter the application configuration screen. Standardized user interfaces make your Portlet applications more appealing.

4. Window
Port Status: the window status determines the space left for the Portlet to generate content on the portal page. If you click the maximize button, the Portlet occupies the entire screen and becomes the only available
In the minimized state, the Portlet only displays as the title bar. As a developer, content should be customized based on the size of available space.

5. Use
User information: Usually portlets provides personalized content to the user who sends the request. In order to be more effective, portlets needs to access the user's property information, such as name, email, and electricity.
. Portlet
The API provides the concept of user attributes. developers can access these attributes in a standard way, and the Administrator is responsible for creating these attributes with the real User Information Database (usually the LDAP server ).
Establish a ing relationship.

In the second part of this article, we will discuss these features in depth-request processing, user information, and the Portlet mode.

Develop a "hello World" Portlet

Now let's develop a simple helloworld Portlet.
1. Create a web project named helloworld which, like a common servlet project, has a/WEB-INF/Web. xml file as the deployment descriptor of the project.

2. Add the portlet-api-1.0.jar file in build path, which is part of the Pluto release package.

3. Create the helloworld. Java file in the source folder as follows:

Public class helloworld extends genericportlet {
Protected void doview (renderrequest request,
Renderresponse response) throws
Portletexception, ioexception {
Response. setcontenttype ("text/html ");
Response. getwriter (). println ("Hello Portlet ");

Each Portlet must implement the Portlet interface, which defines the lifecycle method for the Portlet. Because we don't want to overwrite all these methods, we only
The genericportlet class is extended. It is an adapter class that implements the Portlet interface. The genericportlet class provides default instances for all lifecycle methods.
So we only need to implement the method we need.

All we have to do in the helloworld Portlet is to display "hello"
Therefore, we will overwrite the doview () method of the genericportlet class. This method uses portletrequest and
Portletresponse is used as the parameter. In the doview () method, call response. setcontenttype () to notify the Portlet
The type of content that the Portlet will generate-if this is not done, illegalstateexception will occur. Once the content type is set, you can
Get printwriter in the response object and start writing.

4. Each Portlet application has a Portlet. xml file in the/WEB-INF folder, which is the deployment descriptor of the Portlet application. Create A Portlet. xml file as follows:

<Description> helloworlddescription
<Portlet-Name> helloworld
<Display-Name> Hello World

<Portlet-class> com. Test. helloworld
<Mime-type> text/html </mime-type>
<Portlet-mode> View
<Supported-locale> en

<Title> Hello World </title>
<Short-title> Hello World
<Keywords> hello, Pluto </keywords>

The <Portlet-Name> element declares the name of the Portlet, And the <Portlet-class> element specifies
The fully qualified class name of the Portlet. The <expiration-Cache> element specifies the content expiration time in seconds. Note the following:
Some actions on the Portlet may cause content refresh, which is irrelevant to the cache time.
The <supports> element specifies the <mime-
Type> modes available. In this example, we assume that helloworld can only generate text/HTML content, and only the view mode supports this content class.
Type. To support other content types, add a new <support> element and specify the modes that support the MIME type. Normally
The text/html types include view, edit, and help modes, while the wml mime type only supports view mode.
Use the <supported-locale> element to specify the localization supported by the Portlet. <Title> the element is used to specify
Title. If you want to internationalize the title, you can use the element <resource-bundle> to specify the file name of the resource (proportional properties file. In this
In this case, the container selects the title from the appropriate properties file based on the region where the user is located.

5. Each Portlet application is a Web application. Therefore, in addition to the Portlet. xml file, a Web. xml file is also required.

<Display-Name> Hello World Portlet
<Welcome-File> index. jsp

6. Compile and package these files as war files. You can do this by yourself, or download an example with build. xmlCode(See the "resource" Section) to create a war file.
Deploy helloworld Portlet on Pluto

Pluto is still in the early stages of development, so there is no easy-to-use management tool. To use the Pluto serverSource codeDownload both versions. Note that
The following description is applicable to Windows. Unix users get similar results by modifying the slash symbol and executing the sh shell script (not a bat batch command file.

1. Create a folder, such as c: \ plutoinstallation.
2. Download pluto-1.0.1-rc1.zipand pluto-src-1.0.1-rc1.zip from the pluto's website.
3. Unzip pluto-1.0.1-rc1.zip to the C: \ plutoinstallation. folder, which should be decompressed to the C: \ plutoinstallation \ pluto-1.0.1-rc1 folder.
4. Execute c: \ plutoinstallation \ pluto-1.0.1-rc1 \ bin \ Startup. BAT to start Pluto, you can now access the Pluto server through the address http: // localhost: 8080/Pluto/portal.
5. Unzip pluto-src-1.0.1-rc1.zip to the C: \ plutoinstallation \ plutosrc folder.
6. Forward
Enter the c: \ plutoinstallation \ plutosrc folder and execute Maven
Distribute: All., compile and download the related resource files necessary to run a general management task. Now you can use helloworldportlet. War
The Portlet is installed.
7. First copy the helloworldportlet. War file to the C: \ plutoinstallation \ portlets directory. If this directory is not available, create it.
8. Rename c: \ plutoinstallation \ plutosrc \ build. properties. Sample to build. properties.
9. Edit build. properties and point maven. tomcat. Home to the installation location of the Pluto build version. In this example, change maven. tomcat. Home = C:/plutoinstallation/pluto-1.0.1-rc1.
Install the Portlet, go to the C: \ plutoinstallation \ plutosrc \ deploy folder, and run Maven deploy
-Ddeploy = c: \ plutoinstallation \ portlets \ helloworldportlet. War. The "Build
Successful "information.
11. Under the c: \ plutoinstallation \ pluto-1.0.1-rc1 \ webapps folder, there should be a helloworldportlet folder.
12. now go to the C: \ plutoinstallation \ pluto-1.0.1-rc1 \ webapps \ helloworld \ WEB-INF \ Folder folder to open the web of the Portlet. XML file, you will find that there are several more lines automatically in it, as shown below:

<Servlet-Name> helloworld </servlet-Name>
<Display-Name> helloworld wrapper </display-Name>
<Description> automated generated
Portlet wrapper </description>
<Servlet-class> org. Apache. Pluto. Core. portletservlet
<Param-Name> Portlet-class </param-Name>
<Param-value> com. Test. helloworld
<Param-Name> Portlet-guid </param-Name>
<Param-value> hellopluto. helloworld

13. Add the Portlet to the page. Enter c: \ plutoinstallation \ Pluto-1.0.1-
RC1 \ webapps \ Pluto \ WEB-INF \ data folder, you can see two XML files: pageregistry. xml and
Portletentityregistry. xml.
14. portletentityregistry. xml contains the Portlet definition. Add the following lines to the file:

<Application ID = "5">
<Definition-ID> helloworld </definition-ID>
<Portlet id = "1">
<Definition-ID> helloworld. helloworld </definition-ID>

The <definition-ID> of the application should be the name of the folder where the web application is located. The <definition-ID> Of the Portlet should be consistent with the Portlet-guid generated in Web. xml.
15. pageregistry. xml defines the portlets contained in the page and makes the following changes to the file:

<Fragment name = "p2" type = "Portlet">
<Property name = "Portlet" value = "/blog/5.1"/>

16. Run the shutdown command and startup command to restart the Pluto server. Return to the address http: // localhost: 8080/Pluto/portal and click "test link". Our

Helloworld Portlet.

The right side of Figure 3 shows what the helloworld Portlet looks like.

Figure 3 Portlet Screen

How to create a portal page

Figure 4 shows how the portal container assembles the separated portlets into a page.

Figure 4 portal creation page

Most of the portal servers are Web applications deployed on the application server. servlet is used to process requests to access the Portal Server. Check Pluto's
The installation directory will find that Pluto is just a common web application deployed on the Tomcat server, and then look at c: \ plutoinstallation \ Pluto-
1.0.1-RC1 \ webapps \ Pluto \ WEB-INF \ WEB. XML will find that all requests sent to the Pluto server are mapped
Org. Apache. Pluto. portalimpl. servlet.

In the "portal page elements" section at the beginning of this article, we mentioned that the portal page consists of two parts. One part is the content generated by portlets on the page, and the other part is the content generated by the Portal Server.

In Pluto, as long as the user sends a request, the servlet controls the list of portlets to be displayed based on the page requested by the user. Once a list is generated, the servlet transfers the control to these portlets threads and collects the content generated by them.

The content generated by the Portal Server (such as the view of the portal website and the appearance and control of each Portlet) depends on
C: \ plutoinstallation \ pluto-1.0.1-rc1 \ webapps \ Pluto \ WEB-INF \ aggregation File
The JSP file under the folder. Rootfragment. jsp is the main JSP file, which determines the overall perception and alignment. It also contains heads to define in the generated page
The content in the The image is also expanded to pluto.png ). Tabnavigation. jsp is used to determine the navigation scheme of the portal website. This means you only need to modify a few
JSP files can change the view of the entire portal website.

Pluto determines the number of rows on the page based on the settings in pageregistry. XML, and
Fill in with rowfragment. jsp. Columnfragment. jsp is used to fill each column. Portletfragmentheader. jsp
Fill in the page headers of each Portlet, such as the title bar and maximize and minimize control. Footer. jsp is used to fill the footer of the JSP. If you want to see the HTML of the portal page
The code will find that each Portlet window is nothing more than a content block embedded with the <TD> label.


To be successful, any new technology must meet the following conditions: first, it can improve existing technologies; second, it can solve common problems encountered by existing technologies; and again, it can provide more than one abstraction layer (it is said that each abstraction layer solves the problem in half ).

Because the Portlet is compatible with the existing application server architecture
API is a good opportunity to develop Servlet technology. You can call ejb from a Portlet, or use it to start and participate in global transactions controlled by the application server. In other words
In the business logic-centric field, the Portlet can be as good as the servlet.

Portlets provides an abstraction layer, so you don't have to worry about it now.
The client does not have to write any HTTP method.ProgramCapture client events such as clicking a button. Last but not least, portlets provides
Services such as logon and personalization solve most of the problems that servlets cannot solve.

· Sample code in this article
· JSR 168 home: http://www.jcp.org/en/jsr/detail? Id = 168
· Pluto home: http://portals.apache.org/pluto/
· Onjava. com: onjava.com
· Matrix-Java developerCommunity: Http://www.matrix.org.cn

Sunil Patil has been engaged in J2EE technology for five years. He is interested in object relationship Ing tools, UI frameworks, and portals.

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.