Accessing Portlet-specific Objects in JSP Pages

This article is based on Portlets in Action, to be published June 2011. I t is being reproduced here by permission from Manning Publications. Manning publishes MEAP (Manning Early Access Program,) ebooks and pbooks. MEAPs are sold exclusively through All print book purchases include an ebook free of charge. When mobile formats become available all customers will be contacted and upgraded. Visit for more information.

also read:


In a lot of cases, JavaServer Pages (JSPs) may rely on JSP implicit objects to achieve their functionality. It is perfectly fine to use JSP implicit variables in your JSP pages, but, on the downside, your JSP is dealing with servlet-specific objects (like HttpServletRequest and HttpSession) and not portlet-specific objects (like RenderRequest and PortletSession).

If we add an attribute to RenderRequest, it’s mirrored by HttpServletRequest and vice versa. The same applies to HttpSession and PortletSession. So what difference does it make if we use portlet-specific objects in JSPs? Subtle differences between servlet and portlet API can answer this question. For instance, in your JSP page, you can’t use HttpServletResponse to create a portlet URL (only a RenderResponse object provides methods to create portlet URLs). HttpServletRequest can’t provide the information about the portlet mode and the window state (PortletRequest provides methods to obtain the portlet mode and the window state). This is like rain and freezing rain—both are forms of precipitation with the only difference in what happens when raindrops hit the ground.

In general, your JSP pages will use a combination of portlet and servlet objects to generate content. For instance, if you want to set the expiration time for the content in the JSP page, you will use the RenderResponse portlet object. If you need to obtain the value of an APPLICATION_SCOPE portlet session attribute, you may prefer to use session (which refers to HttpSession object) or sessionScope (which is a java.util.Map of session attributes stored in HttpSession object) for a JSP implicit object to obtain its value.


JSPs included by portlets have access to JSP implicit objects because portlet application is also a web application.

The name defineObjects gives the impression that it can be used by JSP developers to define custom objects in their JSP pages, but that’s not the case. The defineObjects tag of portlet tag library provides JSPs with access to portlet objects like RenderRequest, RenderResponse, PortletSession, and so on. Table 1 lists the scripting variables (and their type) that are available to JSP pages that use defineObjects tag.

Table 1 shows the scripting variables defined by the defineObject tag. As you can see, defineObjects provides JSP pages with all Portlet 2.0 API objects they’ll ever need to use portlet-specific features. In JSP pages, you’ll usually use a combination of JSP implicit objects and the implicit objects made available by defineObjects tag. You’ll probably never use actionRequest, actionResponse, eventRequest, and eventResponse objects in your JSP pages because the portlet container ignores the content written out to the response in action and event processing phases.


defineObjects tag doesn’t define any attributes.

The sample Book Catalog portlet in this article uses scripting variables introduced by defineObjects tag to show the debugging information, as shown earlier in figure 1.

Listing 1 shows debug.jsp page and is included by all JSP pages in the Book Catalog portlet. The debug.jsp page prints the information about the portlet mode, the window state, the value of myaction attribute from request, the content expiration time, and so on, most of which is obtained using scripting variables introduced by defineObjects tag.

Listing 1 debug.jsp—Using variables introduced by defineObjects tag

<%@ taglib prefix="portlet" 						#1
		uri=""%> 			#1
	<%@ page contentType="text/html" isELIgnored="false"
	<portlet:defineObjects /> 						#2
	Value of myaction request attribute:
		${requestScope.myaction} 					#3
	Portlet mode of request:
		<%=renderRequest.getPortletMode()%> 				#4
	Window state of request:
		<%=renderRequest.getWindowState()%> 				#5
	Content Expiration time:
		<%=renderResponse.getCacheControl().getExpirationTime()%> 	#6
	uploadFolder init parameter value:
		<%=portletConfig.getInitParameter("uploadFolder")%> 		#7
	myaction attribute value in PortletSession:
		${portletSessionScope.myaction} 				#8
	Search criteria stored in PortletSession:
		Book name –
		<%=portletSession.getAttribute("bookNameSearchField", 		#9
		PortletSession.APPLICATION_SCOPE)%> 				#9
		Author name –
		<%=session.getAttribute("authorNameSearchField")%> 		#10
		#1 Declare portlet tab library
		#2 defineObjects tag
		#3 myaction HttpRequest attribute
		#4 Portlet mode from RenderRequest
		#5 Window state from RenderRequest
		#6 Cache expiration time from RenderResponse
		#7 uploadFolder portlet initialization parameter
		#8 myaction PortletSession attribute
		#9 bookNameSearchField PortletSession attribute
		#10 authorNameSearchField HttpSession attribute

At #1, the JSP page declares the portlet tag library using the taglib directory. The value of the uri attribute is if you want to use Portlet 2.0 tag library. If you are using Portlet 1.0 tag library, specify the value of uri as The defineObjects tag in Portlet 2.0 introduced additional variables like portletSessionScope, portletSession, and so on, which makes it easy to develop portlets using JSP as the view technology. At #2, the defineObjects tag is used to introduce scripting variables in the JSP page. At #3, the requestScope JSP implicit variable is used to obtain the value of the myaction attribute. At #4, the renderRequest variable is used to obtain the current portlet mode. At #5, the renderRequest variable is used to obtain the window state information. At #6, the renderResponse variable is used to obtain CacheControl object, which, in turn, is used to obtain the expiration time for the generated content. At #7, the portletConfig object is used to obtain the uploadFolder portlet initialization parameter. At #8, the portletSessionScope variable is used to obtain the value of the myaction attribute from PortletSession. At #9, the portletSession variable is used to obtain the value of the bookNameSearchField attribute from APPLICATION_SCOPE of PortletSession. At #10, the session JSP implicit variable is used to obtain the value of the authorNameSearchField attribute from HttpSession. We see here that using the session JSP implicit variable makes it easy to retrieve the APPLICATION_SCOPE attribute stored in PortletSession.

Listing 1 shows that we’ll usually use JSP implicit variables and variables introduced by the defineObjects tag to create JSP pages. If you browse through the pages of the Book Catalog portlet, the debugging information will change to reflect the current value of various attributes displayed by the debug.jsp page.


In this article, we saw how the portlet tag library tags can be used to simplify development of JSP pages used by portlets. Specifically, we learned how to use JSP implicit variables and variables introduced by the defineObjects tag to create JSP pages.

Leave a Reply

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

Pin It on Pinterest

Share This

Share this post with your friends!