We’ve introduced all of these concepts that would be used while you’re dealing with the Portlet development. Portlet and Portlet container had used several objects that make obligation between both of them to get their functionality achieved.

However, no one of these tutorials have provided you a full detailed explanation for these fine-grained objects. To get into real development, you should be aware of these objects as well as you must have to know what these objects are closely,  how can they are used and when their usage should be to get the most benefit.

Portlet Tutorial

This tutorial intended to help you getting these information in an organised manner as it’s also will make you use all of them willingly. Following below core concepts that you would pass through:

Portlet Requests

As we’ve stated earlier, the PortletRequest interface represents the common functionality that’s lying in RenderRequest interface and ActionRequest interface as both of them have extended the PortletRequest interface.

PortletRequest interface provides methods for accessing various types of needed information. These information would be:

Request Attributes:  request attributes used mainly to pass Java objects between Portlets, Servlets and JSP pages. These attributes are formed as name/value pairs, in that the String will form the name of the attribute while Java Object is the value.

As soon as the Portlet receives a new action request, the attributes that were sent previously are no longer available and they will be replaced by new ones. The Portlet may set, update, delete and read attributes during action processing and render processing by invoking request.getAttribute(), request.setAttribute() and request.getAttributeNames().

Basically, any attributes you’ve set against request while executing of processAction() phase won’t be available by the request object while executing of render() phase. Even Java Portlet Specification 2.0 has  provided the PLT.10.4.4 Runtime Option javax.portlet.actionScopedRequestAttributes that will make your processAction() attributes available while executing of render() phase but unfortunately this feature isn’t implemented in Apache Pluto 2.0.3 as you can refer for Apache Pluto’s JIRA.

A more detailed sample would be discussed, especially when it comes to use the JSF bridge that already facilitate sending objects through using of request attributes.

Request Parameters:Request parameters aren’t so differ from discussed request attributes, they’re pairs of name/value where both of them are a String object. The most important issue here are the sources that request parameter come from. Typically, request parameters are those appended onto Portlet URL (e.g ActionURL, RenderURL), submitted by the form or set during execution of action request.

The parameters sent to the Portlet from an action request are valid only while the action request is processed. Meanwhile, the render parameters are valid until the Portlet has received a new request. According to Java Portlet Specification 2.0, all these requests that are responsible for change the Portlet’s window state or mode from the user interface elements on the Portal page shouldn’t reset the render parameters for the Portlet.

Both of processAction() and render are read the request parameters using the same methods; getParamaterMap(), getParameter(), getParameterNames() and getParamaterValues() are used for this purpose. Following sample shows you how can we develop simple Login form where you must enter journaldev as a username and password to get logged into.

portlet.xml

Here’s detailed explanation for the code listed above:

  • You can use RenderResponse to create actionURL or renderURL.
  • It’s possible to pass the parameters as appended name/value into both of action request and render request.
  • Action request has the ability to add any new parameters for render request.
  • If you’ve tried to get username and password parameters by using render request, you will get null for both of them.

Context Path: The Portlet’s context path is that part of URL corresponds to the Portlet’s context. Acquiring of context path is very helpful especially when a reference to a web resources is required. You may redirect your request into other web resources like Servlet by adding the context path into resource name.

Context path can be specified by using either of action or render requests.

Preferred Locales & Internationalization: It’s also possible for you to query the current locale that will be used for rendering the content. You may invoke the getLocale() or getLocales() for getting the current locale used and the list of locales for which the Portlet could provide the content for the Portal.

Retrieving The Schema, Server Name and Port: Similar for HttpServletRequest, PortletRequest provides you the needed methods for inquiring the schema of the URL used for the request (e.g http, https, ftp, etc), the server’s name and the port number that the application is listening for.

Even you’re probably won’t get used these methods as you don’t need to create the needed URLs by yourself. Methods like createActionURL() and createRenderURL() will be used alternatively.

Render Request

As we’ve seen previously, render() method takes a RenderRequest as one of the arguments. RenderRequest is used to represent the request for the render phase.

Action Request & File Uploading

At the same time, the request that’s passed for processAction() method is of type ActionRequest and it represents the request for the action phase.

ActionReqeust provides you two different methods that will help you read the content of the request body, getReader() and getPortletInputStream() are used for this purpose. Mainly, if your form posted data its MIME type is application/x-www-form-urlencoded, the Portlet container will read the request body on behalf of you and you will get the form’s data as a request parameters.

As soon as you’ve called a getReader() or getPortletInputStream() while reading the request body from a form’s its MIME type is application/x-www-form-urlencoded, you’ve surly got an IllegalStateException that print out the message says User request HTTP POST data is of type application/x-www-form-urlencoded. This data has been already processed by the portlet-container and is available as request parameters.

You’re able of getting read the form’s data that MIME value is multipart/form-data. But be careful as you can open one reader at the same time. As soon as the getReader() has been used, using of getPortletInputStream() will throw an IllegalStateException and the same case when opposite is happened. Following sample shows you how can we submit two different forms, one of them is aimed to upload the file.

Portlet Tutorial ActionRequest - FORM - URL Encoded - Initiating

Portlet Tutorial ActionRequest - FORM - URL Encoded - SubmittingPortlet Tutorial ActionRequest - FORM - Multipart - Initiating

Portlet Tutorial ActionRequest - FORM - Multipart - Submitting

Here’s detailed explanation for the code listed above:

  • GetContentType is used for determining which type of request we’ve handled.
  • URL-encoded request cannot be read as the Portlet container will do that. So you can find your form’s parameters through using of request.getParameters().
  • Multipart request can be read using getReader() or getPortletInputstream(). But one can be used per request.
  • Instead of processing the request manually to get the file uploaded, we used Apache commons upload file library. This library is very smart one as it can handle normal file upload request or Portlet file upload request. See required libraries provided within the pom.xml.
  • Although, we handle the uploaded file using the very trivial way (Just Print out some of file information), but you can use ImageIO api for getting image saved onto your disk space.
  • To get ActionRequestReader Portlet deployed, don’t forget to list it inside portlet.xml file.

Portlet Response

The Portlet sends the response object back to the Portal after every request. The response object contains the Portlet fragment that will be drawn into Portal page. Fragments collective mission is a Portal job.

PortletResponse provides you a three methods only, setProperty(), getProperty() and encodeURL(). These methods that are relevant for properties will be discussed while introducing the Portlet cache concept. Using of encodeURL() is mandatory for some of Portlet container implementations, in that some resources (e.g Servlet, JSP, etc) within the Portlet application need to be referenced using some additional data like SessionID and may others.

According to Java Portlet Specification, if encoding isn’t needed, the Portlet container will return the URL unchanged.

Render Response

Mainly, we’ve used RenderResponse object for writing content fragment into Portal page. RenderResponse object has provided two methods for getting content drawn, getWriter() and getPortletOutputStream() are available for that purpose.

But as soon as you’ve used getWriter(), you cannot able of using the getPortletOutputStream() and the opposition is valid. In case you’ve used both of them to handle the content draw, you will get an IllegalStateException.

The major facility has provided by the RenderResponse object is the getNamespace() method that’s used in a JavaScript code for variable naming identification. Following sample shows you two Portlets have referenced the same Portlet class and they’re provided the same JavaScript method. Let’s look at the scenario before getting getNamespace() used.

Login-Form-1024x225

Here’s detailed explanation for the code listed above:

  • As the same message variable is defined for both of the Portlet, the last value of message variable will be used.
  • Both of Portlets will use the same message variable and specifically the last one of this variable.

To get this issue resloved, getNamespace() can be used, following below the solution that you may use as long as your JavaScript code is listed on the Portlet itself.

Here’s detailed explanation for the code listed above:

  • If you’ve used the sample above, you should find two different handleSubmit() method as well as two different message variables that are named Pluto_PortletConcepts_RenderResponseNamespaceOne__1332422274_0_message and Pluto_PortletConcepts_RenderResponseNamespaceTwo__1332422274_1_message respectively.
  • This strategy would help as long as the JavaScript code is provided within the Portlet. If you’ve set your JavaScript code inside an external file, this strategy isn’t so helpful.

Action Response

Typically, action response used for two different major missions; for passing the parameters for the render phase through using of setRenderParamater() and for redirect the request into any other resources than let the normal flow proceeds. Redirection has been done using of sendRedirect() as you can redirect into any resources like Servlet or JSP. Both of these behaviors are discussed and we also had provided samples for both of them.

Summary

Portlet API contains a lot of concepts that we must be aware of. This tutorial has discussed the most APIs that you should be familiar with as they would be helpful in the next coming tutorials. Contribute us by commenting below and find downloaded source code.

By admin

Leave a Reply