Using the serveResource() method for serving static Content

If you take a look at the implementation of serveResource() method in the class you will notice that it is implemented like this

public void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException, IOException {
if (request.getResourceID() != null) {
PortletRequestDispatcher rd = getPortletConfig().getPortletContext().getRequestDispatcher(
if (rd != null)
rd.forward(request, response);

As you can see the default implementation reads the request.getResourceID() and forwards requests to that URL for generating response.

So lets say if i have a sampleimg.gif in my portlet .war file then i can include it in JSP either using the encodeURL method or using the serveResource() method like this

<%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" session="false"%>
<%@taglib uri="" prefix="portlet"%>
<%@taglib uri="" prefix="portlet-client-model"%>
<%@taglib uri="" prefix="portletx"%>

<portlet:defineObjects />

javax.portlet.ResourceURL url = renderResponse.createResourceURL();

<img src='<%= url.toString() %>' />

If you take a look at the HTML generated by this


As you can see the resourceID is encoded in the URL and this URL is pointing to the portal engine so it will enter through portal engine and then control will be forwarded to the sampleimg.gif. Using the serveResource() for including static content is not a good idea because it will require more processing power than including a normal file serving.


Anonymous said...


You haven't created resourceURL anywhere in doView() or serveResource() in the portlet.

So how does Ajax know to call the serveResource() method of ValidationCacheSamplePortlet by using below line?"GET","",true);

Vikas Chaudhary said...

Battery Mantra is Authorized exide car battery dealer in Noida and Greater Noida. We are providing our service in Indirapuram, Delhi, Ashok Nagar.

Exide Battery Dealer in Noida
Battery Dealer in Noida
Authorized Battery Dealer in Noida
Car Battery Dealer in Noida
Car Battery Dealer
Exide Battery Dealer

srjwebsolutions said...

We are leading responsive website designing and development company in Noida.
We are offering mobile friendly responsive website designing, website development, e-commerce website, seo service and sem services in Noida.

Responsive Website Designing Company in Noida
Website Designing Company in Noida
SEO Services in Noida
SMO Services in Noida

EG MEDI said... is online medical store pharmacy in laxmi nagar Delhi. You can Order prescription/OTC medicines online.
Cash on Delivery available. Free Home Delivery

Online Pharmacy in Delhi
Buy Online medicine in Delhi
Online Pharmacy in laxmi nagar
Buy Online medicine in laxmi nagar
Onine Medical Store in Delhi
Online Medical store in laxmi nagar
Online medicine store in delhi
online medicine store in laxmi nagar
Purchase Medicine Online
Online Pharmacy India
Online Medical Store