Friday 16 March 2012

Working with portlet container in websphere application server 7.0-Part2

I have developed Sample portlet and deployed the portlet in WAS 7.0.

My context root of  this application is /myportlet.

I am able at access the  application URL

https://localhost:10002/myportlet/MyPortlet

Here MyPortlet is portlet name.

Sample Code

MyPortlet.java

package com.ibm.myportlet;

import java.io.*;
import java.util.*;
import javax.portlet.*;

/**
 * A sample portlet based on GenericPortlet
 */
public class MyPortlet extends GenericPortlet {

    public static final String JSP_FOLDER    = "/_MyPortlet/jsp/";    // JSP folder name

    public static final String VIEW_JSP      = "MyPortletView";         // JSP file name to be rendered on the view mode
    /**
     * @see javax.portlet.Portlet#init()
     */
    public void init() throws PortletException{
        super.init();
    }

    /**
     * Serve up the <code>view</code> mode.
     *
     * @see javax.portlet.GenericPortlet#doView(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
     */
    public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
        // Set the MIME type for the render response
        response.setContentType(request.getResponseContentType());

        // Invoke the JSP to render
        PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(getJspFilePath(request, VIEW_JSP));
        rd.include(request,response);
    }

    /**
     * Process an action request.
     *
     * @see javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
     */
    public void processAction(ActionRequest request, ActionResponse response) throws PortletException, java.io.IOException {
        System.out.println("calling process action");
        response.setRenderParameter("myname", request.getParameter("myname"));
    }

    /**
     * Returns JSP file path.
     *
     * @param request Render request
     * @param jspFile JSP file name
     * @return JSP file path
     */
    private static String getJspFilePath(RenderRequest request, String jspFile) {
        String markup = request.getProperty("wps.markup");
        if( markup == null )
            markup = getMarkup(request.getResponseContentType());
        return JSP_FOLDER + markup + "/" + jspFile + "." + getJspExtension(markup);
    }

    /**
     * Convert MIME type to markup name.
     *
     * @param contentType MIME type
     * @return Markup name
     */
    private static String getMarkup(String contentType) {
        if( "text/vnd.wap.wml".equals(contentType) )
            return "wml";
        else
            return "html";
    }

    /**
     * Returns the file extension for the JSP file
     *
     * @param markupName Markup name
     * @return JSP extension
     */
    private static String getJspExtension(String markupName) {
        return "jsp";
    }

}

MyPortletView.jsp

<%@page session="false" contentType="text/html" pageEncoding="ISO-8859-1" import="java.util.*,javax.portlet.*,com.ibm.myportlet.*" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>               
<portlet:defineObjects/>
<DIV style="margin: 6px">

<H3 style="margin-bottom: 3px">Welcome! ${param.myname}</H3>
Enter your Name<BR>
<form action='<portlet:actionURL/>'>
<input type="text" name="myname"/>
<input type="submit" name="submit" value="submit"/>
</form>
</DIV>

Portlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" id="com.ibm.myportlet.MyPortlet.a8804fa063">
    <portlet>
        <portlet-name>MyPortlet</portlet-name>
        <display-name xml:lang="en">MyPortlet</display-name>
        <display-name>MyPortlet</display-name>
        <portlet-class>com.ibm.myportlet.MyPortlet</portlet-class>
        <init-param>
            <name>wps.markup</name>
            <value>html</value>
        </init-param>
        <expiration-cache>0</expiration-cache>
        <supports>
            <mime-type>text/html</mime-type>
            <portlet-mode>view</portlet-mode>
        </supports>
        <supported-locale>en</supported-locale>
        <resource-bundle>com.ibm.myportlet.nl.MyPortletResource</resource-bundle>
        <portlet-info>
            <title>MyPortlet</title>
            <short-title>MyPortlet</short-title>
            <keywords>MyPortlet</keywords>
        </portlet-info>
        <portlet-preferences>
        <preference>
        <name>URL</name>
        <value>www.google.com</value>
        </preference>
        </portlet-preferences>
    </portlet>
    <default-namespace>http://MyPortlet/</default-namespace>
</portlet-app>

ScreenShot

Click here to download the sample code

2 comments:

  1. I am getting 'Direct portlet access prevented by WebSphere Portal' when I ran this example. Do you know how to resole this?

    ReplyDelete
  2. You need to run in server1 profile.In Webspshere_Portal profile ,u cannot access directly.This is limitation of portlet container.

    ReplyDelete