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


Here MyPortlet is portlet name.

Sample Code


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{

     * 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

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

     * Process an action request.
     * @see javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
    public void processAction(ActionRequest request, ActionResponse response) throws PortletException, {
        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";
            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";



<%@page session="false" contentType="text/html" pageEncoding="ISO-8859-1" import="java.util.*,javax.portlet.*,*" %>
<%@ taglib uri="" prefix="portlet"%>               
<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"/>


<?xml version="1.0" encoding="UTF-8"?>
<portlet-app xmlns="" version="2.0" xmlns:xsi="" xsi:schemaLocation="" id="">
        <display-name xml:lang="en">MyPortlet</display-name>


Click here to download the sample code


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

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