What Is A Portlet?

Source: Internet
Author: User
Portlets
"Portlets is a Web component-just like servlets-designed to aggregate the content on the compositing page. When you request a portal page, multiple portlets are 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 specification, 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. Every window here 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, these windows are developed independently and different applications. The news Portlet developer creates an application and packs it into a war file. Then, the Portal Server administrator deploys the war file on the server and creates a page, next, each user selects the applications on his or her page. For example, if a user is not interested in the stock price but is interested in sports, he can use the "Sports" window to replace the "stock 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-time login, and gather the content of various information sources, and act as the host of the Information System presentation 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 containers are deployed in the Portlet container. The Portlet container controls the lifecycle of the Portlet and provides necessary resources and environment information for it. 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. When you have logged on to the Intranet website and want to access the mail application, the Portal Server will read your pass from the pass library and log on to the mail server for you. Your access to other applications will also be handled accordingly.
O personalization: the basic implementation of personalized services enables users to personalize their pages in two ways: first, users can decide the color and control icon of the title bar according to their preferences. Second, you can decide which portlets are available on her page. For example, if I am a sports fan, I may replace stock and news portlets WITH A Portlet that provides my favorite team's latest information.
Some of the leading commercial implementation versions of personalized services allow you to establish standards (such as income and interest) on which applications are displayed to users ). In this case, you can set business rules such as "display gifts to any user whose revenue is X" and "display the Portlet of discount products for any user whose revenue is X.

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, rather than 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 cannot generate content at will, because the content generated by the Portlet will eventually become 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. Request Processing: portlets provides more fine-grained request processing. When a user acts on A Portlet, the request is sent to the Portlet (A status called active period), or the page refresh request is triggered by the user's action on another Portlet, the Portal Server provides two different callback methods for processing.
3. Portlet mode: portlets uses the mode concept to indicate what the user is doing. When using the mail application, you may use it to read, write, or check emails-these are pre-defined functions of the mail application. portlets generally provides these functions 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 application behavior, so 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 the URL address for tracking news updates. I want to use it to display internal news of the company to users. Another requirement is that I don't want users to use this application to track any other news sources. As an administrator, I can specify a URL address for internal news updates for all users, and revoke 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 status: the window status determines the space left on the portal page for the Portlet to generate content. If you click the maximize button, the Portlet occupies the entire screen and becomes the only available Portlet for the user. In the minimized state, the Portlet is only displayed as the title bar. As a developer, content should be customized based on the size of available space.

5. 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 phone number. The Portlet API provides the concept of user attributes. developers can access these attributes in a standard way and the Administrator is responsible for these attributes and the real User Information Database (usually the LDAP server) establish a ing between them.

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 override all these methods, we only need to extend the genericportlet class, which is an adapter class that implements the Portlet interface. The genericportlet class provides default implementations for all lifecycle methods, so we only need to implement the methods we need.

All we need to do in the helloworld Portlet is to display "Hello Portlet", so we will overwrite the doview () method of the genericportlet class. This method uses portletrequest and portletresponse as parameters. In the doview () method, call response. setcontenttype () to notify the Portlet container of the type of content that the Portlet will generate. Otherwise, the illegalstateexception will occur. Once the content type is set, you can get printwriter from 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:

<portlet>
  <description>HelloWorldDescription
        </description>
    <portlet-name>HelloWorld
        </portlet-name>
    <display-name>Hello World
        </display-name>

    <portlet-class>com.test.HelloWorld
        </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>

        <portlet-info>
          <title>Hello World</title>
          <short-title>Hello World
          </short-title>
          <keywords>Hello,pluto</keywords>
      </portlet-info>
</portlet>

The <Portlet-Name> element declares the name of the Portlet. 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 that some of your actions on the Portlet may cause content refresh, which has nothing to do with the cache time.
The <supports> element specifies the modes available for a given <mime-type>. In this example, we assume that helloworld can only generate text/HTML content, and only view mode can support this content type. To support other content types, add a new <support> element and specify the modes that support the MIME type. Generally, the Portlet supports view, edit, and help modes for the text/html type, while the wml mime type only supports view mode.
You can also use the <supported-locale> element to specify the localization supported by the Portlet. <Title> the element is used to specify the title of the Portlet. 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 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.

<web-app>
  <display-name>Hello World Portlet
  </display-name>
  <welcome-file-list
    <welcome-file>index.jsp
        </welcome-file>
  </welcome-file-list>
</web-app>

6. Compile and package these files as war files. You can do this by yourself, or download the sample code with build. XML (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 server, you need to download both the compilation and source code versions. Note that the following instructions are applicable to Windows. Unix users can obtain 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, now you can access the Pluto server through the address http: // localhost: 8080/Pluto/portal.
5. Extract pluto-src-1.0.1-rc1.zip to the C:/plutoinstallation/plutosrc folder.
6. Enter the C:/plutoinstallation/plutosrc folder, execute Maven distribute: All., compile and download the relevant resource files necessary to run the general management task. Now you can install helloworldportlet. War as a Portlet.
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.
10. to install the Portlet, go to the C:/plutoinstallation/plutosrc/deploy folder and run Maven deploy-ddeploy = C:/plutoinstallation/portlets/helloworldportlet. war, you should be able to see the "build successful" information.
11. There should be a helloworldportlet folder under the C:/plutoinstallation/pluto-1.0.1-rc1/webapps folder.
12. now go to the C:/plutoinstallation/pluto-1.0.1-rc1/webapps/helloworld/WEB-INF/folder and open the web of the Portlet. XML file, you will find that there are several more lines automatically in it, as shown below:

<servlet>
  <servlet-name>HelloWorld</servlet-name>
     <display-name>HelloWorld Wrapper</display-name>
      <description>Automated generated
      Portlet Wrapper</description>
      <servlet-class>org.apache.pluto.core.PortletServlet
      </servlet-class>
      <init-param>
         <param-name>portlet-class</param-name>
         <param-value>com.test.HelloWorld
         </param-value>
      </init-param>
      <init-param>
         <param-name>portlet-guid</param-name>
         <param-value>HelloPluto.HelloWorld
         </param-value>
      </init-param>
</servlet>

13. Add the Portlet to the page. Go to the C:/plutoinstallation/pluto-1.0.1-rc1/webapps/Pluto/WEB-INF/data folder and you will 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>
     </portlet>
</application>

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="5.1"/>
</fragment>

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. View Pluto installation directory will find that Pluto is just a common web application deployed on the Tomcat server, 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 to 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 folder under the JSP file. Rootfragment. jsp is the main JSP file, which determines the overall perception and alignment. It also contains the content of heads to define in the

Based on the settings in pageregistry. XML, Pluto determines the number of rows in the page and fills it with rowfragment. jsp. Columnfragment. jsp is used to fill each column. Portletfragmentheader. jsp is used to fill 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 look at the HTML code of the portal page, you will find that each Portlet window is nothing more than a content block embedded with the <TD> label.

Conclusion

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

As the Portlet is compatible with the existing application server architecture, this is a good opportunity for the Portlet API 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-centered field, the Portlet can be as good as the servlet.

Portlets provides an abstraction layer. Now you don't have to worry about the HTTP method used by the client, or write programs to capture client events such as clicking a button. Last but not least, portlets solves most of the problems that servlets cannot solve by providing services such as single logon and personalization.

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.