Writing the JSR-168 portlet Guide

Source: Internet
Author: User
Tags resource web services firewall
Summary
JSR-168 is a collection of Java APIs that are appropriate for portlet developers. There are many reasons to design a JSR-168 portlet that conforms to specifications. Portability is an obvious benefit. Code written according to specifications is easier to move between portal servers. Most Java-based portal servers support JSR-168 portlets.

Another advantage is that it is easier to unite. It is easier to expose JSR-168 portlets through web Services for Remote Portlets (WSRP) producers when the Portlet complies with the JSR-168 specification. WSRP provides a standard for federated Portlet content through Web service. The JSR-168 and WSRP 1.0 portlet functions are tightly coupled. The JSR-168 to WSRP Portlet Bridge uses the JSR-168 URL rewrite API. This article describes best practices for developing JSR-168 portlets for portability.

1. Always use the URL rewrite API to get the content in the Portlet
Java developers often write the URL of an image in the JSP shown below:

/images/logo.gif"/>

This is not true in the JSR-168 portlet. The correct approach is to:

<img src= "<%= Renderresponse.encodeurl (Renderrequest.getcontextpath () +
"/images/logo.gif")%> "/>

The Encodeurl () method can take either a full path URI or a fully qualified URL. The full path URI is the most common. You can use this technique when you use the JSR-168 portlet to embed resources in the Web application Archive (WAR). You can use a fully qualified URL when you place an image on a separate server. A caching server dedicated to serving static content is an example of unloading traffic from the portal server. Although you can refer to content outside of a portlet by using Encodeurl () with a fully qualified URL, you should only do so if the resource cannot be accessed through the client. If the client can browse resources directly, you do not need to use Encodeurl () for the URL. For example, if you have a Web server that you can use to obtain static content within a firewall that the portal user cannot directly browse, you need to invoke Encodeurl (). If the content is outside the firewall and the portal user can browse directly to the Web server, you do not need to invoke Encodeurl ().

2. Do not append the path to the rewrite URL
The URL in the Encodeurl () method passed in Renderrequest must be complete before the method is invoked. After you call this method, you cannot add portions of the URL. For example, if you want to generate a URL transformation from an XSLT transformation, you cannot pass the encoded base URL (http://foo.com/) as a parameter and attach the path (pages/bar.jsp) to the encoded base URL in the transformation.

The following call demonstrates the correct way to encode a URL into an image:

<@= Renderresponse.encodeurl (Renderrequest.getcontextpath () +
"/images/logo.gif") @>

It uses a. Portal file to generate the following HTML fragment in the BEA WebLogic Portal 9.2:

portal_tau=w3f6fbmllcgzq9fpv1jhls5rrjg8lgj2nndvjqdfshhrgfnsqckz!-545815275 "/>

The following call is not correct. The URL does not point to the resource you want.

<@= Renderresponse.encodeurl (Renderrequest.getcontextpath () +
"/images/") + "logo.gif" @>

It uses the. Portal file to generate the following HTML file in WebLogic Portal 9.2:


3. Qualifying client script variables and methods with namespaces
Suppose you want to use JavaScript in the Portlet to authenticate user input. The following JavaScript features can be useful:

<script>
function Validate (foo) {
if (foo.bar.value== "") {
return false;
}
return true;
}
</script>

Other portlets on the same page may also have a different logical JavaScript method named Validate (). The portal framework itself may use JavaScript methods. The solution to this problem is to use the namespace method and the top-level variable in client script. <portlet:namespace/> tags will generate a unique identifier for each portlet. The first step is to include the tag library in the JSP through taglib directive.

<% @taglib uri= "Http://java.sun.com/portlet" prefix= "Portlet"%>

The Validate () method in the script can differentiate the tags.

<script>
function validate<portlet:namespace/> (foo) {
if (foo.bar.value== "") {
return false;
}
return true;
}
</script>

Here's how to call JavaScript methods with namespaces:

<form action= "Http://www.somesite.org/servlet"
Method= "Get" onsubmit= "return validate<portlet:namespace/> (this);" >
<label for= "Bar" >text (required): </label>
<input type= "text" Name= "bar" id= "Bar" >
</form>

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.