indiWiz.com

Subhash's Tech Log

Serving Static and Dynamic Resources in JSR 286

with 2 comments

To serve static content packaged as part of the portlet WAR, we generally take this approach:

<%=response.encodeURL(request.getContextPath()+"/js/project.js")%>

For generating dynamic content what do we do? In JSR 168 this was not possible. Thankfully, JSR 286 introduced this important concept. For creating dynamic content, just override the void serveResource(ResourceRequest request, ResourceResponse response) method in javax.portlet.ResourceServingPortlet (implemented by javax.portlet.GenericPortlet). A simple example:

    @Override
    public void serveResource(ResourceRequest request, ResourceResponse response)
            throws PortletException, IOException {
        String paramValue = request.getParameter("abc");
        System.out.println(paramValue);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/plain");
        PrintWriter pw = response.getWriter();
        pw.println(paramValue);
        pw.close();
    }

To link to this dynamic content:

<portlet:resourceURL var="url">
  <portlet:param name="abc" value="Hello World"/>
</portlet:resourceURL>

<a href="<%=url%>">Dynamic link</a>

Written by Subhash Chandran

February 20th, 2009 at 5:15 pm

Posted in Uncategorized

Tagged with , ,

2 Responses to 'Serving Static and Dynamic Resources in JSR 286'

Subscribe to comments with RSS or TrackBack to 'Serving Static and Dynamic Resources in JSR 286'.

  1. This code snippet did not seem to work, Dynamic Link leads to an empty web page with nothing on it and a familiar very complex URL.

    Vivekanand Kurdikeri

    12 May 10 at 1:13 pm

  2. It could be a bug in your Portal server implementation. We used Liferay, and it worked perfectly.

    Subhash Chandran

    12 May 10 at 6:15 pm

Leave a Reply