Server Side Include
Overview

When you want do render the content of a portlet, you can output strings into the PortletResponse like this:

XPortlet.java
							                    				
public class XPortlet
    extends PortletAdapter
{
	...
    protected void doView( PortletRequest  request,
                           PortletResponse response )
        throws PortletException, 
                   IOException
    {
        response.getWriter(  ).println( "<p>Hello Fred</p>" );
    }
    ...
}
    
							                    			

This method is fine, but not the best for complex user interface. What jPortlet offer is an infrastructure for Server Side Include that allow develloper to output into the PortletResponse the content of a file located either in the portlet's classpath of in the portlet's web-module via PortletAdapter.include() or PortletContext.include()
Using Server Side Include, the portlet code look like this:

XPortlet.java
							                    				
public class XPortlet
    extends PortletAdapter
{
	...
    protected void doView( PortletRequest  request,
                           PortletResponse response )
        throws PortletException, 
                   IOException
    {
        include( "view.html" );
    }
    ...
}
    
							                    			
view.html
							                    				
<p>
    Hello Fred
</p>
    
							                    			
 
Understanding include() function

The include() is a very powerful function, it allows the portlet to render any resource as specified by the given path. This function also support multi-markup languages and internationalization. For example, if the portlet com.foo.MyPortlet myportlet include the resource /mytemplate.html, for a french canadian Mozilla browser, the portlet container will:

  1. Detect the client markup language PortletRequest.getClient()
  2. Detect the client locale PortletRequest.getLocale()
  3. Search the resource from the portlet's classpath (This mean the file is located in the portlet's .jar) inside the same package as the portlet. The container will use the client's markup and locale and search /mytemplate.html with the following paths:
    1. com/foo/html/mytemplate_fr_CA.html
    2. com/foo/html/mytemplate_fr.html
    3. com/foo/html/mytemplate.html
    4. com/foo/mytemplate.html
  4. Search the resource from the portlet's web module (This mean the file is located in the portlet's .war) in a special directory named PORTLET-INF The container will use the client's markup and locale and search /mytemplate.html with the following paths:
    1. PORTLET-INF/myportlet/html/mytemplate_fr_CA.html
    2. PORTLET-INF/myportlet/html/mytemplate_fr.html
    3. PORTLET-INF/myportlet/html/mytemplate.html
    4. PORTLET-INF/myportlet/mytemplate.html

 
Integration with Velocity

The include() function works marvellously with static resources (HTML, XML, WML, TXT etc.) that require no processing. But most of the time, develloper need to output dynamic resources with a scripting language. For those situations, jPortlet uses Velocity Template Language.
Each Velocity file (.vm) will access the following variables:

A portlet that uses Velocity look like this:

XPortlet.java
							                    				
public class XPortlet
    extends PortletAdapter
{
	...
    protected void doView( PortletRequest  request,
                           PortletResponse response )
        throws PortletException, 
                   IOException
    {
    	request.setAttribute( "name", "Fred" );
        include( "view.vm" );
    }
    ...
}
    
							                    			
view.vm
							                    				
<p>
    Hello ${name}
</p>
    
							                    			
 
What about JSP ?

JSP not supported by jPortlet because of the inability to access JSP buffer using a consistent API.