I have made an JSR 168 portlet as follows:
public class GetTest extends GenericPortlet {
@Override
public void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
PortletRequestDispatcher rd =
getPortletContext().getRequestDispatcher("/getTest.jsp");
rd.include(request, response);
}
}
The portlet for this is named as getTest.portlet and is at WebContent folder. The jsp page for this:
<%
String params = request.getParameter("params");
out.print("Params: " + params);
%>
Now I want to make an Ajax get request to this portlet using DISC framework of Weblogic. How can I do this?
I searched on net regarding this but didnt any useful example which I can use. What I have tried is as follows:
in some other.jsp:
.....
<script type="text/javascript">
var dataUrl = "/getTest.portlet?params=hi";
var xmlhttp = new bea.wlp.disc.io.XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
alert(xmlhttp.responseText);
}
}
xmlhttp.open('GET', dataUrl, true);
xmlhttp.send(null);
</script>
....
In alert I get blank. I should get "Params: hi" as it is in jsp page of this portlet. How can I achieve this?
I read following articles but did not find anything useful or may be I missed something.
I have also enabled disc for the desktop portal in which this portlet is attached.
Finally, I found it myself. I need to use Resource Serving feature of JSR 286 portlet to get data through Ajax request. What you need to do is:
<portlet:resourceURL var="homeURL" id="home" escapeXml="false" />
Use this url to make an Ajax request to your portlet.
var path="<%=homeURL.toString()%>";
request.onreadystatechange=function () {
if (request.readyState == 4) {
if (request.status == 200) {
alert(request.responseText);
} else {
alert("Problem retrieving data from server.");
}
}
};
request.open("GET", path, true);
request.send(null);
serveResource(ResourceRequest request, ResourceResponse response)
method. Set data values as attributes to request and then forward request, response to JSP page using request dispathcer. Or you can directly write data values in JSON format to response writer.(response.getWriter()
)request.send(null)
instead of null
pass your data.Hope this helps future visitors. :-)
Still I am not sure if this is the best way to do this. Any other solutions are always welcome.