Portlets
"Portlets is a Web component-just as servlets-is designed to bring together content from a composite page." Usually requesting a portal page causes multiple portlets to be invoked. Each portlet generates a tag segment and is grouped together with other portlets-generated markup segments to embed in the markup of the portal page. "(Excerpt from the Portlet specification, JSR 168)
This article explores the following:
1. Elements of the portal page
2. What is portal.
3. What is portlets.
4. Developing the "Hello World" Portlet
5. Deployment of HelloWorld portlets on Pluto
6. How to create a portal page
7. Concluding remarks
8. Resources
The Portlet specification defines the portlet as a "java-based Web Component managed by the Portlet container that handles requests and generates dynamic content." This passage sounds a little convoluted. This article will show you what portlets is and what you can do with it.
Figure 1 shows the appearance of the page in the browser when accessing a portal server.
Figure 1 A typical Portal Server page (click to view the original image)
If you look closely at the page in your browser, you will see that the page is made up of different windows. One window for refreshing the weather, another for news, and one for refreshing stock prices, and so on. Each of these windows represents a portlets. If you look more carefully, you will also find that each window has a title bar and some buttons, including minimized and maximized buttons.
In the system, these windows are mutually independent development, each different application. The developer of the news portlet creates files that are applied and packaged into the war format, and then the administrator of the Portal Server deploys the war file on the server and creates the page, and then each user chooses which apps to use on his page. For example, if a user is interested in sports if they are not interested in the stock price, he can replace the share price window with the sports window.
Portlet technology needs to learn a lot of new concepts, this article is not all covered, so this article is divided into two parts. In the first part we detail portals and portlets, and develop a simple "Hello world" portlet; in the second part we'll explore some advanced topics.
We will test our sample portlets with the Apache Pluto Server (the reference implementation of the Portlet API 1.0 specification) and we will spend some time exploring how to install and use the Pluto server.
Elements of portal page
Figure 2 shows the various elements of the portal page.
Figure 2 Elements of the portal page
Each portlet page consists of one or more portlet windows, each of which is divided into two parts: one is the appearance, it determines the style of the Portlet window's title bar, control, and bounds, and the other is a portlet segment, which is populated by the Portlet application.
The portal Server determines the overall look and feel of the portal page, such as logo, title bar color, control icon, and so on. By modifying several JSP and CSS template files, you can change the overall perception of the portal. We'll discuss this in depth in the "How To Create Portal page" section.
what the portal is.
It is necessary to understand the portal before you understand the portlet. This is said in the Portlet specification: "Portal is a Web application that is often used to provide personalization, single sign-on, aggregation of information sources, and as a host of information system presentation layers." Aggregation is the activity of integrating content from various sources into a Web page.
Portal functions can be divided into three main areas:
1. Portlet containers: The Portlet container is very similar to the servlet container, where all portlets are deployed in the Portlet container, and the Portlet container controls the lifecycle of the Portlet and provides it with the necessary resource and environment information. The Portlet container is responsible for initializing and destroying portlets, transmitting user requests to portlets and synthesizing responses.
2. Content aggregation: One of the main tasks of portal in the Portlet specification is to aggregate content generated by various portlet applications, and we will discuss this further in the "How To Create Portal page" section.
3. Public service: One of the strengths of a portlet server is the set of public services it provides. These services are not required by the Portlet specification, but the portal's commercial implementation version provides a rich public service to differentiate them from their competitors. Some of the public services that are expected to be found in most implementations are:
o single sign-on: Just log on to the portal server once to access all other applications, which means you no longer have to log in to each application separately. For example, once I log on to my intranet site, I can access mail applications, IM messaging applications, and other intranet applications without having to log in to these applications separately.
The Portal Server will assign you a pass library. You only need to set the username and password once in the mail application, which will be stored in the Passport library in an encrypted manner. When you are logged on to an intranet site and want to access mail applications, the Portal server reads your passes from the pass library and logs you on to the mail server. Your access to other applications will be handled as well.
o Personalization: The basic implementation of personalized service enables users to personalize her pages from two aspects: first, the user can decide the color and control icon of the title bar according to her own preferences. Second, the user can decide what portlets on her page. For example, if I were a sports fan, I might replace stock and news portlets with a portlet that would provide the latest information about my favorite team.
Some of the leading business implementations in personalized services allow you to establish the criteria (such as income and interest) on which applications are displayed for the user. In this case, you can set up business rules such as "portlets that display gifts for users of any revenue x" and "portlets that show discounted items for users of any revenue x".
There are also public services, such as machine translation, where the portal server translates portlet-generated content into the language required by the user. Most commercial portal servers support handheld device access and have the ability to generate different content for different browsing terminals.
what Portlets is.
Like Servlets, Portlets is a Web component that is deployed within a container to generate dynamic content. From a technical standpoint, a portlet is a class that implements the Javax.portlet.Portlet interface, which is packaged into a war file format and deployed into a portlet container.
Portlets is similar to servlets in the following ways:
1. Portlets is managed by a specific container.
2. portlets generates dynamic content.
3. The lifecycle of the Portlet is managed by the container.
4. Portlets interacts with the Web client through the request/response pattern.
Portlets differ from Servlets in the following ways:
1. Portlets can only generate tag segments, not entire documents.
2. Portlets does not have a URL address to direct access to. But you can still get someone to access the portlet through a URL, and you could send the URL of the page containing the portlet to him.
3. Portlets is not free to generate content because the Portlet-generated content eventually becomes part of the portal page. If the portal server requires a Html/text type, then all portlets should generate the Html/text type of content. For example, if the Portal Server requires a WML type, then all portlets should generate the content of the WML type.
Portlets also provides some additional features:
1. Set the persistent storage of the parameter: Portlets provides a Portletpreferences object to hold the user's setup parameters. These parameters are stored in a persistent database so that the data is still valid after the server restarts. Developers do not have to care about the specific implementation mechanism of these data stores.
2. Request Processing: Portlets provides finer granularity of request processing. The Portal Server provides two different callback methods to handle requests made to the portlet when the user moves on the portlet (a state known as the active period), or to a refresh page request that is raised by a user acting on another portlet.
3. Portlet mode: Portlets uses the concept of a pattern to indicate what the user is doing. When you use mail, you might use it to read letters, write letters, or check letters-these are the predefined features of mail applications, and portlets usually provide these features in view mode. However, there are activities such as specifying refresh time or (re) setting user names and passwords that allow users to customize the behavior of the application, so they use edit mode. The Help function used by mail is in aid mode.
If you think about the fact that there is nothing new here, they are mostly ordinary business needs. The role of the Portlet specification is that it provides an abstraction layer, which is the value of all the people associated with it-end users, developers, and administrators.
As a developer, I put all the business logic associated with the view mode into the Doview () method, put the business logic associated with the application configuration into the Doedit () method, and put the help-related logic into the Dohelp () method
This simplifies the administrator's access control management to the Portlet application because he can determine what the user can do simply by changing the access rights of the portlet. For example, if a user in mail application can set the username and password in edit mode, he or she can determine that he has edit mode access.
Consider a situation where I am an administrator of an intranet site, and my company buys a third-party portlet application that displays news information that allows the user to specify a URL for tracking news updates, and I want to use it to show the company's internal news to the user. Another requirement is that I don't want users to track any other sources of news information through the application. As an administrator, I can specify a URL address for the internal news update for all users, and remove the permissions for others to modify the address by changing the deployment descriptor applied by the portlet.
Because all portlet applications have a similar UI interface, using Portlets makes the site more appealing to end users. If she wants to read any help for an application, she can click on the Help button, and she knows that clicking the Edit button allows her to enter the application's configuration screen. A standardized user interface makes your portlet application more appealing.
4. Window state: The window state 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 portlet available to the user, and in the minimized state, the portlet is only displayed as a title bar. As a developer, the content should be tailored to the size of the available space.
5. User information: Usually portlets to the users of the request to provide personalized content, in order to be more effective, portlets need to access the user's attribute information, such as name, email, telephone and so on. The Portlet API provides the concept of user attributes to enable developers to access these properties in a standard manner, and the Administrator is responsible for mapping these properties to the real user information database (usually the LDAP server).
We will discuss these features in depth in the second part of this article-request processing, user information, and portlet patterns.
developing the "Hello World" Portlet
Now we're going to develop a simple HelloWorld portlet.
1. Create a WEB project named HelloWorld, similar to the usual Servlet project, with a/web-inf/web.xml file as the deployment descriptor for the project.
2. In Build path, add the Portlet-api-1.0.jar file, which is part of the Pluto release package.
3. Create the Helloworld.java file in the source folder as follows:
Each portlet implements the Portlet interface, which defines the lifecycle method for the portlet. Since we do not want to overwrite all of these methods, we only extend the Genericportlet class, which is an adapter class that implements the Portlet interface. The Genericportlet class provides the default implementation of all lifecycle methods, so we only need to implement the methods we require.
All we have to do in the HelloWorld portlet is to display the "Hello Portlet", so we'll overwrite the Doview () method of the Genericportlet class, which portletrequest and Portletresponse as a parameter. Call Response.setcontenttype () first in the Doview () method to inform the Portlet container what type of content the Portlet will generate-if not, it can cause a illegalstateexception exception. Once you have set the type of content, you can get PrintWriter from the response object and begin writing.
4. Each portlet application has a portlet.xml file in the/web-inf folder, which is the deployment descriptor for the Portlet application. Create the Portlet.xml file as follows:
<portlet-name> Element declares the name of the Portlet,<portlet-class> element specifies the fully qualified class name of the Portlet,<expiration-cache> The element specifies the time of the content to be extended in seconds. Here's one thing to note: Some of your actions on a portlet may cause content to refresh, regardless of cache time.
The <supports> element specifies which schemas are available for the given <mime-type>. In the example we assume that HelloWorld can only generate text/html type of content, and that only the view mode can support that content type. If you want to increase support for other content types, you need to add a new <support> element and specify which patterns are supported for that MIME type. Typically, portlets have view, edit, and help modes for text/html types, whereas for WML MIME types, only view mode is available.
You can also use the <supported-locale> element to specify which localization the portlet supports. The <title> element is used to specify the title of the Portlet. If you want to internationalize the title, you can use the element <resource-bundle> 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 area where the user is located.
5. Each portlet application is a Web application, so there is a need for web.xml files in addition to portlet.xml files.
6. The files are then compiled and packaged as war files. You can do this yourself, or download the sample code with Build.xml (see the "Resources" section) to create the war file.
Deploying the HelloWorld Portlet on Pluto
Pluto is still in the early stages of development, so there is no easy to use management tool. In order to be able to use the Pluto server, you need to download both the compilation and the source code two versions. Note that the following instructions are for the Windows platform, and UNIX users will get similar results by modifying the slash symbol and executing the SH shell script (not the Bat Batch command file).
1. Create a folder, such as C:/plutoinstallation.
2. Download Pluto-1.0.1-rc1.zip and Pluto-src-1.0.1-rc1.zip from Pluto's website.
3. Extract Pluto-1.0.1-rc1.zip to c:/plutoinstallation. folder, it should be extracted 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 and now can be http://localhost:8080/pluto/by address Portal accesses Pluto server.
5. Extract the Pluto-src-1.0.1-rc1.zip to the C:/plutoinstallation/plutosrc folder.
6. Go to the C:/plutoinstallation/plutosrc folder and execute Maven Distribute:all., compile and download the related resource files necessary to run general administrative tasks. You can now install Helloworldportlet.war as a portlet.
7. First copy the Helloworldportlet.war file to the C:/plutoinstallation/portlets directory and create it without this directory.
8. Rename the c:/plutoinstallation/plutosrc/build.properties.sample to Build.properties.
9. Edit Build.properties to point maven.tomcat.home to the installation location of Pluto's compiled version, in this case to maven.tomcat.home=c:/plutoinstallation/ Pluto-1.0.1-rc1.
10. To install the portlet, go to the C:/plutoinstallation/plutosrc/deploy folder and execute Maven deploy-ddeploy=c:/plutoinstallation/portlets/ Helloworldportlet.war, you should be able to see the "build successful" information.
11. Under the C:/plutoinstallation/pluto-1.0.1-rc1/webapps folder, there should be a Helloworldportlet folder.
12. Now enter the C:/plutoinstallation/pluto-1.0.1-rc1/webapps/helloworld/web-inf/folder folder, Open the Portlet's Web.xml file and you'll find a few more lines in it, as follows:
13. Next we add the Portlet to the page. Enter the C:/plutoinstallation/pluto-1.0.1-rc1/webapps/pluto/web-inf/data folder, You can see that there are two XML files: Pageregistry.xml and Portletentityregistry.xml.
Portletentityregistry.xml contains the definition of the portlet, adding the following lines to the file:
The applied <definition-id> should be the name of the folder where the Web application resides, and the <definition-id> of the portlet should be consistent with the portlet-guid generated in the Web.xml.
Pageregistry.xml defines which portlets are included in the page and makes the following changes to the file:
16. Execute the SHUTDOWN command and startup command to restart the Pluto server, return to address Http://localhost:8080/pluto/portal and click "Test Link"-This time the page will appear with our
HelloWorld portlets.
The right side of Figure 3 shows what the HelloWorld portlet looks like.
Figure 3 Screenshot of the Portlet
How to create a portal page
Figure 4 shows how the portal container assembles the detached portlets into a page.
Figure 4 Creating the portal page
Most portal servers are basically Web applications deployed on the application server, and the servlet handles requests to access the portal server. Look at Pluto's installation directory and find that Pluto is just a common web application deployed on a tomcat server, and then look at the c:/plutoinstallation/pluto-1.0.1-rc1/webapps/pluto/ Web-inf/web.xml will find that all requests destined for the Pluto server are mapped to the Org.apache.pluto.portalImpl.Servlet.
In the first part of this article, "Elements of the portal page," we mentioned that the portal page is made up of two parts. Part is the content generated by the portlets in the page, and the other part is generated by the portal server.
In Pluto, as long as the user makes a request, it is controlled by the servlet, which determines the list of portlets to display according to the page requested by the user. Once the list is generated, the servlet transfers control to these portlets threads and collects the content generated by them.
The content generated by the portal server (such as the perception of the portal site and the appearance and control of each portlet) depends on the c:/plutoinstallation/pluto-1.0.1-rc1/webapps/pluto/ The JSP file under the Web-inf/aggregation folder. Rootfragment.jsp is the main JSP file that determines the overall perception and alignment, and it also contains heads to define the contents of <HEAD> tags in the generated page. TABNAVIGATION.JSP is used to select what to display in banner (the Pluto.png picture is also expanded by default in the banner display list). Tabnavigation.jsp is used to determine the navigation scheme for the portal site. This means that you can change the perception of the entire portal site by simply changing a small number of JSP files in the folder.
Pluto determines how many rows are in the page based on the settings in the Pageregistry.xml and fills them with rowfragment.jsp. Columnfragment.jsp is used to populate each column. Portletfragmentheader.jsp is used to populate the headers of each portlet, like title bars and maximize and minimize control. footer.jsp the footer used to populate the JSP. If you look at the HTML code for the portal page, you'll find that each portlet window is nothing more than an embedded <TD> tagged content block.
Concluding remarks
Any new technology to be successful should have the following conditions: first, it can improve the existing technology; second, it can solve the general problem of existing technology; again, it can provide more than one layer of abstraction (it is said that the problem is half done per abstraction).
Because the Portlet is compatible with the existing application server architecture, this is a good opportunity to develop the servlet technology for the Portlet API. You can invoke the EJB from the portlet, or use it to start and participate in global transactions controlled by the application server. In other words, a portlet can do as well as a servlet in an area that is at the core of business logic.
Portlets provides an abstraction layer, and now you don't have to worry about what HTTP methods the client uses or write a program to capture client events like click buttons. Last but not least, portlets solves most of the problems that Servlets cannot solve by providing services such as single sign-on, personalization, and so on.