Sample Resource Serving Portlet

This is a sample portlet that can be deployed in any Portlet Container and demonstrates how to make a Ajax Resource Call to the portlet without using any JavaScript framework like dojo. The JavaScript code is lengthy then if you use JavaScript framework. But sometimes you cant/dont want to use JavaScript framework for 1 call.

First i did build a JSR 286 compliant ValidationCacheSamplePortlet portlet like this.

public class ValidationCacheSamplePortlet extends GenericPortlet{
protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
System.out.println("Entering ValidationCacheSamplePortlet.doView()");
response.setContentType("text/html");
getPortletContext().getRequestDispatcher("/validation.jsp").include(request, response);
System.out.println("Exiting ValidationCacheSamplePortlet.doView()");
}
public void serveResource(ResourceRequest request, ResourceResponse response)
throws PortletException, IOException {
System.out.println("Entering ValidationCacheSamplePortlet.serveResource()");
response.setContentType("text/html");
response.getWriter().println("Hello from ValidationCacheSamplePortlet.serveResource");
System.out.println("Exiting ValidationCacheSamplePortlet.serveResource()");
}
}


The ValidationCacheSamplePortlet is very simple in the doView() method it is passing control to validate.jsp for generating response markup and in the serveResource() method is returning Hello from ValidationCacheSamplePortlet.serveResource String.

This is how the validation.jsp looks like, it has a button Resource button, when you click on that button it is making a HTTP GET call to <portlet:resourceURL/> and displaying the response using alert.


<%@page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" session="false"%>
<%@taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<portlet:defineObjects />
<script type="text/javascript">
function createXMLHttpRequestObject() {
var xmlHttp;
try {
xmlHttp = new XMLHttpRequest();
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
} catch (e) {
}
}
if (!xmlHttp)
alert("Error creating the XMLHttpRequest object.");
else
return xmlHttp;
}
var xmlHttp = createXMLHttpRequestObject();
function makeResourceCall(){
if (xmlHttp){
try{
xmlHttp.open("GET", "<portlet:resourceURL/>", true);
xmlHttp.onreadystatechange = handleRequestStateChange;
xmlHttp.send(null);
}catch (e){
alert("Can't connect to server:\n" + e.toString());
}
}
}

function handleRequestStateChange(){
if (xmlHttp.readyState == 4){
if (xmlHttp.status == 200){
alert(xmlHttp.responseText);
}
}
}
</script>
<input type="button" onclick="JavaScript:makeResourceCall()" value="Resource Call" />
<div id="resourceResponse"></div>


4 comments:

  1. hi,
    I tried what u have said here.but i get an error saying "This portlet is temporarily disabled" .
    what can be the possible issue here.

    Thanks

    ReplyDelete
  2. HI vogue,
    Which Portal Server you are using?

    ReplyDelete
  3. if u are using any portal server,u need to add this portlet to one page.And You have to Active that Page.

    ReplyDelete