Serving Static and Dynamic Resources in JSR 286
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>