ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

What Is a Portlet, Part 2
Pages: 1, 2, 3, 4, 5

Greet User with Personalized Message

In last section we talked about how the portlet container processes requests. In this section, we will clarify portlet request processing concepts by changing the HelloWorld portlet to handle the processAction() method call. What we will do is ask user for his or her name in EDIT mode, and then use that name to greet the user in VIEW mode. Follow these steps:

  1. Change the HelloWorld.java file, like this.

    protected void doView(RenderRequest renderRequest, 
    RenderResponse renderResponse)
    throws PortletException, IOException {
      String userName = (String) renderRequest.
      if (userName != null)
        renderResponse.getWriter().println("Hello " 
          + userName);
        renderResponse.getWriter().println("Hello how 
         are you doing. Go to edit mode and set name");
    protected void doEdit(RenderRequest renderRequest, 
    RenderResponse renderResponse)
    throws PortletException, IOException {
      PortletRequestDispatcher dispatcher = 
      dispatcher.include(renderRequest, renderResponse);
    public void processAction(ActionRequest 
    actionRequest, ActionResponse actionResponse) 
    throws PortletException, IOException {
      String userName =
        actionRequest.getParameter ("username");
        ("username", userName);

    As you can see, we have to make lots of changes in HelloWorld.java in order to greet the user with his or her name. Let's take a look at each of these changes.

    • doEdit(): Now we pass control to username.jsp to generate a form that will ask the user for his or her name.

    • processAction(): This method gets called only when user goes to EDIT mode and submits a form. Inside of this method, the first thing we do is to call actionRequest.getParameter() to find the value of the userName input submitted by the user.

      We then set this value as an attribute in the portletSession object. Please note that the ActionRequest object is different than RenderRequest, so you can set an attribute in ActionRequest and expect it to be available in RenderRequest. Also the portlet container will create only one instance of Portlet to serve multiple requests (similar to Servlet), so you cannot pass a value from the action phase to the render phase as an instance or static variable of the Portlet class. The last line is actionResponse.setPortletMode(PortletMode.VIEW). This is called because once the user submits a name, we want to change the mode back to VIEW mode. This way, when the render() method is called, it will route the user request to the doView() method instead of the doEdit() method.

    • doView(): In this method, we will check if the userName attribute is already set in the PortletSession object. If so, we use the value of that attribute to greet the user; if it is not set, then we display a message informing the user how to set userName.

  2. Create /html/username.jsp inside of the Web Content folder. This JSP will be used to solicit input from the user in EDIT mode.

    <%@ page language="java"%>
    <%@ taglib uri="http://java.sun.com/portlet" 
    <form action='<portlet:actionURL />'>
            <td>User Name</td>
                    <input type="text" name="username"/>
                    <input type="submit" label="Save"/>

    This JSP is like any typical JSP with the difference of the <portlet:defineObjects> tag. The portlet specification defines the portlet tag library with the aim of solving common problems faced by portlet developers. The portlet tag library is very important concept, and we'll revisit it shortly.

Build your application and deploy it on Pluto. When you go to VIEW mode, it will ask you to go to EDIT mode and enter a user name. Enter userName and click Submit; this should take you back to VIEW mode, which will display a message saying "Hello your-name." You know, one of the problems of being a UI developer is that you don't feel like you're learning something unless you see some change in the UI.

Pages: 1, 2, 3, 4, 5

Next Pagearrow