How the PortletResponse.encodeUrl() method works

I was looking for information about encodeURL() method and this is what i found

Portlets should encode all resource URLs pointing to resources in the portlet application via this method in order to ensure that they get served via the portal application. Some portal/portlet-container implementation may require those URLs to contain implementation specific data encoded in it. Because of that, portlets should use this method to create such URLs.
The encodeURL method may include the session ID and other portal/portlet-container specific information into the URL. If encoding is not needed, it returns the URL unchanged. Portlet developer should be aware that the returned URL might not be a well formed URL but a special token at the time the portlet is generating its content. Thus portlets should not add additional parameters on the resulting URL or expect to be able to parse the URL. As a result, the outcome of the encodeURL call may be different than calling encodeURL in the servlet world.


As you can see its very difficult to understand how it works so i did some experiment to figure out the exact behavior

When you want to include a static resource in your portlet for example a image or JavaScript file that is packaged inside your portlets .war file you can include it like this

<%= renderResponse.encodeURL(renderRequest.getContextPath() + "filepathinsidewebmodule") %>


For example i want to include sampleimg.gif which is at the root of my web application i.e. at the same as that of WEB-INF then i can include it my application using this URL


<img src='<%= renderResponse.encodeURL(renderRequest.getContextPath() + "sampleimg.gif") %>'>


This JSP code will get converted into following HTML markup

/wps/PA_ResourceServing/sampleimg.gif


In this the /wps/PA_ResourceServing part is the application context of the portlet which contains this URL.

You can find the context root of the portlet in two ways. While deploying the portlet in 6.1 it lets you set the context root for the portlet. You can find the context root of a portlet which is already installed by going to /wp_profile/installedApps/DefaultNode/yourportlet.ear directory and then inside META-INF there will be application.xml, open it and you will notice it is something like this


<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="Application_ID" version="1.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd">
<display-name>ResourceServing_war</display-name>
<module>
<web>
<web-uri>ResourceServing.war</web-uri>
<context-root>/wps/PA_ResourceServing</context-root>
</web>
</module>
</application>


As you can see in my case the ResourceServing.war file is installed at the /wps/PA_ResourceServing URL.

Inside your JSP you can find this context root by calling renderRequest.getContextPath(). The encodeUrl() does not have any effect of the URL. But you should always include your URL

2 comments:

AGR said...

Very nice article to understand the RenderResponse.encodeURL(...) method. Thank you Sunil.

Abhi said...

Thanks for info....
SEO Company in Bangalore