What is Portlet namespace

Portlet namespace is used to uniquely identify each portlet. It’s unique identifier associated to each portlet by portal.

portlet namespace

What is portlet namespace ?

Portlet namespace is a unique value associated to each portlet by portal. In other word, each portlet will have it’s own unique portlet namespace. You no need to worry about how it’s generated. Liferay portal will generate it for you.

Why portlet namespace is required ?

Consider a scenario where you are creating input element called ‘name‘ in view.jsp for given portlet. At the time portlet development, you don’t an idea what other portlets are placed on same page along with your portlet.

Assume that some other portlet which is placed on same page also has one html element called ‘name‘. In this scenario there will be a name collision when you try to access this element(name) through javascript.


Assume that you gave id=”test1″ to some html element for your portlet. There are high chances that some other portlet has some other html element with same id(test1). In this case if you try to access id=”test1″ through Javascript then again it’s name collision.

To avoid this, portlet namespace should be used. Since portlet namespace is unique for each portlet, if you append it along with name of html attribute, then there won’t be any chance of name collision (especially when trying to access html element through java script).

How to get portlet namespace value ?

There are following 2 ways to get portlet namespace

  • Using <portlet:namespace/> tag
  • Using PortletResponse.getNamespace() method.

Using <portlet:namespace/> tag

Portlet default implementation (available in portlet.jar @ tomcat-bundle/lib/ext folder) provides certain tags. ‘portlet‘ is one of such tag. You can get current portlet’s namespace by <portlet:namespace/> tag. To avail this tag you must have to include following taglib definition in JSP

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

You can prepend portlet namespace through this tag to any html element name to make it unique so that there won’t be any name collision.

for example, portlet namespace can be used like below code snippet

<input type="text" name="<portlet:namespace/>param1">


  • I have just append <portlet:namespace/> for name attribute in JSP. When this JSP renders, the above code will look like the below snippet

<input type="text" name="_requirednamespace_WAR_requirednamespaceportlet_param1">

  • _requirednamespace_WAR_requirednamespaceportlet_ is name space for portlet  required-namespace-portlet. this namespace is generated by <portlet:namespace/> tag.
  • Since each portlet has it’s own unique namespace, it will help to avoid name collision. Just like the name in the above example, you can prepand portlet namespace to other html attributes like id.
  • This portlet is non-instantiable. If it’s instantiable then Liferay portal generates 4 extra alpha-numeric instance Id in the above namespace. for example, if this portlet is instantiable then it’s namespace would be _requirednamespace_WAR_requirednamespaceportlet_abcd.
  • These last 4 alpha-numeric instance Id is unique per that portlet’s instance. There can be more than one instance for instantiable portlet and liferay portal will generate unique instance Id for each instance.

Using PortletResponse.getNamespace() method.

Liferay provide certain implicit objects. <portlet:defineObject /> tag is used to make these implicit objects available in JSP. Second way to get portlet name space is to call PortletResponse.getNamespace() method in scriptlet in JSP. for example

<%=portletResponse.getNamespace() %>

You can set it in some variable and refer it across the JSP page.

RenderResponse, actionResponse and ResoruceRequest are  subclass of PortletResponse. You can get portlet’s namespace by calling getNamespace() method on respective implicit objects of above classes as below.

  • renderResponse.getNamespace()
  • actionResponse.getNamespace()
  • resourceRquest.getNamespace()

Below are few example how to use portletResponse.getNamespace() to get portlet’s namespace

<span id="<%=renderResponse.getNamespace()%>sp1">

  <input type="text" name="<%=renderResponse.getNamespace()%>StudentName" 


there is no difference between getting namespace from the above two methods. However, the first approach (<portlet:namespace /> tag) is more preferable way.

There is some situation in which you have to use a second approach (portletResponse.getNamespace()) to get namespace.

For example, you are writing your own tag or if you are using some other tag and you need to pass portlet’s namespace in the attribute of this tag then resolving tag inside another tag is not possible but you can still use portletResponse.getNamespace() in the script (if that attribute allows accepting scriptlet value) to pass portlet’s namespace in that tag.

Summing Up

  • The namespace is a unique identifier generated by the portal for each portlet.
  • It is helpful to avoid name collision while accessing html elements in javascript.
  • <portlet:namespace/> tag is used to get portlet’s namespace in JSP
  • portletResponse.getNamespace() is another way to get portlet’s namespace in scriptlet.
  • You can refer liferay forum link to get more information about the portlet’s namespace.

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.

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.