This simplified Hello World portlet example allows a user to set the message that is displayed within a portlet.
Before writing any code, create a new IDK project as described in Setting Up a Custom Java IDK Project in Eclipse Stand-Alone (without WTP) or Setting Up a Custom Java IDK Project in Eclipse with WTP.
This example uses two pages: a portlet that displays the current setting value and a form for changing the value, and a page that sets the value in the ALI database and redirects to the portal page.
<%@ page language="java" import="com.plumtree.remote.portlet.*,java.util.Date" %> You refreshed at <%= new Date().toString()%><br/> <% //get the idk IPortletContext portletContext = PortletContextFactory.createPortletContext(request, response); IPortletRequest portletRequest = portletContext.getRequest(); String settingKey = "PortletEntry"; String settingValue = portletRequest.getSettingValue(SettingType.Portlet, settingKey); //if the entry has already been set, display it here if (null != settingValue) { %> <br/><b> Preference value is <%=settingValue%></b>!<br/> <% } //form to enter the preference %> <P>Enter your preference: <form METHOD="post" ACTION="setPrefs.jsp" name="form1"> <input type="text" name="<%=settingKey%>"> <br/> <input type="submit" name="Submit" value="Submit"> </form>Next, create the Set Preferences page (setPrefs.jsp). The code shown below gets the value for the PortletEntry Portlet setting from the request, then uses the IDK Portlet API IPortletResponse object to add the setting to the database and redirect to the portal. The redirect causes the portal page to refresh and display the updated setting in the portlet.
<%@ page language="java" import="com.plumtree.remote.portlet.*" %> <% //set the cache control so we don't get a cached page response.setHeader("Cache-control", "max-age=0"); //get the idk IPortletContext portletContext = PortletContextFactory.createPortletContext(request, response); //get IPortletResponse to set preferences and redirect back to the portal IPortletResponse portletResponse = portletContext.getResponse(); //get the setting value from the servlet request String settingKey = "PortletEntry"; String settingValue = request.getParameter(settingKey); //set the setting value portletResponse.setSettingValue(SettingType.Portlet, settingKey, settingValue); //redirect back to the portal portletResponse.returnToPortal(); %>
After you have completed the JSP pages, deploy your custom project as described in Deploying a Custom Java IDK Project in Eclipse Stand-Alone (without WTP) or Deploying a Custom Java IDK Project in Eclipse with WTP.