Making AMF calls from AJAX (JSP, Java)

AMF (Action Message Format) Calls can be triggered from Java Client as well. AMFConnection class can be used for the same. Documentation is available at: http://livedocs.adobe.com/blazeds/1/javadoc/flex/messaging/io/amf/client/AMFConnection.html

Below is a simple example usage:

  • Setup BlazeDS and create simple destination as explained by Sujit Reddy on his blog. URL: http://sujitreddyg.wordpress.com/2008/01/14/invoking-java-methods-from-adobe-flex/
  • Create following two JSP files – ‘index.jsp’, ‘callAMF.jsp’. Copy these files on server. In my case, application name was ‘JavaAMFClientExample’. I copied files to root of ‘JavaAMFClientExample’ application on server.
  • Create Java Class ‘JavaAMFClientImpl’; add BlazeDS JAR files to Java project. Copy class file in appropriate directory on server. In my case path of class file was – <TomcatServer\webapps>\JavaAMFClientExample\WEB-INF\classes\com\tushar\JavaAMFClientExample\JavaAMFClientImpl.class
  • Run index.jsp and click ‘Call Service’ button. This will make an AJAX call to ‘callAMF.jsp’ and will display server response in same page.

index.jsp:

<HTML>
<head>
<style type="text/css">
table {
 border-width: 0px;
 border-spacing: 2px;
 border-style: dashed;
 border-color: gray;
 border-collapse: separate;
 background-color: white;
}
table td {
 border-width: 0px;
 padding: 0px;
 border-style: inset;
 border-color: gray;
 background-color: white;
 -moz-border-radius: 0px 0px 0px 0px;
}
p, li, td { font-family: verdana;  font-size: 12px }

</style>
<script type="text/javascript">
{
 var xmlHttp;
 try    {  // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
 catch (e) {  // Internet Explorer
 try {
 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
 }
 catch (e) {
 try {
 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
 catch (e) {
 alert("Your browser does not support AJAX!");
 }
 }
 }

 xmlHttp.onreadystatechange=function()
 {
 if(xmlHttp.readyState==4) {
 document.getElementById("response").innerHTML = xmlHttp.responseText;
 }
 }

 function onFormSubmit()
 {
 var destination = document.getElementById("destination");
 var method = document.getElementById("method");
 var name = document.getElementById("name");

 if(destination != null){
 destination = destination.value
 }
 if(method != null){
 method = method.value
 }
 if(name != null){
 name = name.value
 }
 var params = "destination="+destination+"&method="+method+"&name="+name+"&sid="+Math.random();
 var url="callAMF.jsp?"+params;
 xmlHttp.open("GET",url,true);
 xmlHttp.send(null);
 }
}
</script>
</head>
<BODY>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
 <tr>
 <td>
 <table border="1" width="100">
 <tr>
 <td colspan="2" align="center"><div><h5>Test AMF Call</h1></div></td>
 </tr>
 <tr>
 <td>Destination:</td>
 <td><INPUT TYPE=TEXT id="destination" value="CreatingRpc" SIZE=20></td>
 </tr>
 <tr>
 <td>Method:</td>
 <td><INPUT TYPE=TEXT id="method" value="getResults" SIZE=20></td>
 </tr>
 <tr>
 <td>Name:</td>
 <td><INPUT TYPE=TEXT id="name" value="Tushar" SIZE=20></td>
 </tr>
 <tr>
 <td></td>
 <td><input type="submit" value="Call Service" onclick="onFormSubmit()" /></td>
 </tr>
 </table>
 </td>
 </tr>
 <tr>
 <td align="left"><div id="response"></td>
 </tr>
</BODY>
</HTML>

callAMF.jsp:

<%@ page language="java" %>
<%@ page import="com.tushar.JavaAMFClientExample.*" %>

<%
String destination=request.getParameter("destination");
String method=request.getParameter("method");
String name=request.getParameter("name");

JavaAMFClientImpl javaAMFClientService = new JavaAMFClientImpl();
Object obj = javaAMFClientService.sendAMFMessage(destination, method, name);
%>
<p>Message from Server: <%=obj%><p>

JavaAMFClientImpl.java

package com.tushar.JavaAMFClientExample;

import flex.messaging.io.amf.client.AMFConnection;
import flex.messaging.io.amf.client.exceptions.ClientStatusException;
import flex.messaging.io.amf.client.exceptions.ServerStatusException;

public class JavaAMFClientImpl {

 public Object sendAMFMessage(String destination, String method, String name) throws ClientStatusException, ServerStatusException {
 // Create the AMF connection.
 AMFConnection amfConnection = new AMFConnection();
 // Connect to the remote url.
 String url = "http://localhost:8080/JavaAMFClientExample/messagebroker/amf";
 try
 {
 amfConnection.connect(url);
 }
 catch (ClientStatusException cse)
 {
 System.out.println(cse);
 return null;
 }

 // Make a remoting call and retrieve the result.
 try
 {
 Object result = amfConnection.call(destination+"."+method, name);
 return result;
 }
 catch (ClientStatusException cse)
 {
 System.out.println(cse);
 return null;
 }
 catch (ServerStatusException sse)
 {
 System.out.println(sse);
 return null;
 }
 finally {
 amfConnection.close();
 }
 }
}

Java Project:

Flex Client:

JSP Client:

JavaScript AMF Implementation:

James Ward has created a Pure JavaScript AMF implementation. Here are more details: http://css.dzone.com/news/amfjs-%E2%80%93-pure-javascript-amf

6 thoughts on “Making AMF calls from AJAX (JSP, Java)

  1. James Ward’s Javascript AMF implementation is only AMF deserialization, i.e. it only works for server-to-client updates but not client-to-server updates.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s