Resource URL by portlet tag portlet:resourceURL

portlet:resourceURL is one of the custom tag provided by default portlet implementation (resides in portlet.jar).This tag is used to create resource URL in portlet JSP.

In this article, I will be showing you how to create resource URL by portlet tag. Sun provides implementation of portlet API which resides in portlet.jar file. This jar file contains classes and interfaces under javax.portlet pacakage.

This implementation (given in portlet.jar file under tomcat/lib/ext path) provides certain custom tags to create Resource URL. Resource URL is used to execute serve resource phase (serveResource lifecycle method) of portlet.

portlet-resourceURL_tag - portlet:resourceURL

Create resource URL by portlet:resourceURL tag

I am using Liferay MVC portlet to demonstrate creation of resource URL by portlet tag. You can refer my blog on Creating MVC Portlet in Liferay to create project structure.

I gave plugin project name as resourceURL-by-portlet-tag and -portlet will be appended by Liferay IDE while creating Liferay plugin project. I gave portlet class as com.opensource.techblog.portlet.ResourceURLByPortletTagPortlet.

At this point, project structure should look like below screenshot.

project_structure - portlet:resourceURL

 Add following code in portlet class –
	private static Log _log = LogFactoryUtil.getLog(ResourceURLByPortletTagPortlet.class.getName());
	public void render(RenderRequest request, RenderResponse response)
			throws PortletException, IOException {"This is render method....");
		super.render(request, response);
	public void serveResource(ResourceRequest resourceRequest,
			ResourceResponse resourceResponse) throws IOException,
			PortletException {"This is serve resource method....");
		PrintWriter out = resourceResponse.getWriter();
		out.println("This is sample Text");
		super.serveResource(resourceRequest, resourceResponse);


  • In first line, I am creating log object which will is used to print logs for this portlet. LogFactoryUtil class provided by liferay is used to get log object.
  • Next, I overridden render method. It represent render phase of portlet. Inside this render method, there is on log statement.
  • When serve resource phase is executed by portal, it won’t call render (or any other portlet ) phase. So you won’t see log statement defined in render method when serverResource method finish its execution.
  • Next, I override serveResource method. Default implementation of serveResource method is provided in GenericPortlet.
  • The code written inside serveResource method is more or less looks like servlet code. What we are doing is, getting PrintWriter object, adding some text and flush it. This is what you are doing in case if you send some output from servlet.
  • At the last super.serveResource will call super class’s (MVCPortlet) serveResource method which will do all low level things. It’s important that you make a call to super.serveResource at the last in serveResource method of your custom portlet. Otherwise it won’t work properly.

serveResource method represent serve resource phase of portlet. It is called when user clicks on resourceURL from JSP. To see how to create resource URL in JSP, update your view.jsp with below code snippet.

<%@ taglib uri="" prefix="portlet" %>

<portlet:defineObjects />

<portlet:resourceURL var="resourceUrl1">
	<portlet:param name="param1" value="value1"/>

<a href="${resourceUrl1}">This is resourceURL</a>


  • portlet:resourceURL tag is used to create resourceURL in JSP. To avail this tag, you have to define <%@ taglib uri=”″ prefix=”portlet” %> taglib in your Jsp.
  • <portlet:defineObjects /> tag is used to make portlet implicit objects available in JSP.
  • portlet:param inside portlet:resourceURL is used to pass additional parameter which can be access in serveResource method by resourceRequest.getParameter api.
  • var attribute for portlet:resourceURL is used to define variable which holds the generated resource URL. You can refer the generated resource URL by this variable in other part of page(Jsp).
  • At the last, I created one anchor link and set it’s href to generated resource URL (By referring variable in EL which holds resource URL).

Save portlet class and view.jsp, deploy the portlet and place it on some page. You will see one link. Click on it and you will see output like below screen shot.

resource_URL_Output - portlet:resourceURL

Why you need to call resourceURL in Ajax ?

You will see that whole portal output vanished (header, footer etc). You will see the text (or html) you set in serveResource method on page. Why this happened ? What is wrong here ?

Let’s recall that portal will not call any other phase (lifecycle method) after execution of serve resource phase. Due to this behavior, portal will send the response ( generated in serveResource method ) directly to client.

If you trigger resourceURL in ajax call, you will get its response in dynamically manner which is more managable. (e.g. updating DOM element with output or calling JS to update page html with output).

This way whole page is not vanished. You will get the output of serveResource() method without refreshing whole page. To see how you can achieve this, change view.jsp code as below snippet.

<%@ taglib uri="" prefix="portlet" %>

<portlet:defineObjects />
<portlet:resourceURL var="resourceUrl1">
	<portlet:param name="param1" value="value1"/>

<a href="#" onclick="callServeResource()">This is resourceURL</a>

<script type="text/javascript">
function callServeResource(){
    AUI().use('aui-io-request', function(A){'${resourceUrl1}', {
               method: 'post',
               data: {
            	   <portlet:namespace />param2: 'value2',
               on: {
                   success: function() {


  • I have created a javascript function called callServeResource() in which I am making an ajax call.
  • AUI Javascript library is used to make Ajax call. You can refer to my blog on making Ajax call with AUI in Liferay Portlet
  • I am calling callServeResource javascript function with onclick event of link. Since we are calling resourceURL in ajax, I am setting # to href.
Save the view.jsp, deploy the portlet, and refresh the page. Click on the link and you will see below output.
resourceURL_In_Ajax - portlet:resourceURL

 Summing Up

  • resource URL can be created by portlet tag portlet:resourceURL in Jsp.
  • serveResource method will be called when user clicks on resource URL
  • Since the portal is not calling any other portlet phase after completion of serve resource phase, you need call resource URL in Ajax.

Download Portlet Source – (version Liferay 6.2)

Download Source

Recommended For You

About the Author: Nilang

Nilang Patel is a technology evangelist who loves to spread knowledge and helping people in all possible ways. He is an author of two technical books - Java 9 Dependency and Spring 5.0 Projects.

5 Comments to “Resource URL by portlet tag portlet:resourceURL”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.