View comments | RSS feed

Calling servlets and JSPs from Flash

The following sections describe how to get a reference to a servlet or a JSP defined as a servlet in a web.xml file, and call the servlet or JSP.

Note:   Servlets are supported on Servlet 2.2- and Servlet 2.3-compliant application servers. JSPs are only supported on Servlet 2.3-compliant application servers.

Coding a servlet to use with Flash Remoting MX

Although you can use any servlet with Flash Remoting MX, Flash Remoting MX provides a FlashServlet that subclasses the Servlet API and provides a better API than the Request scope. To use the FlashServlet, your servlet must be in the Flash Remoting web application. It must subclass the flashgateway.adapter.java.FlashServlet class and implement the following abstract method:

public Object service(ServletRequest req, ServletResponse resp, List arguments);

Getting a reference to a web application in ActionScript

Before calling a servlet or a JSP defined as a servlet, you must get a reference to the context root of the web application that contains the servlet or JSP.

To get a reference to a web application that contains a servlet or JSP:

  1. Include the NetServices.as file:
    #include "NetServices.as"
    
  2. Specify the default Flash Remoting gateway URL:
    NetServices.setDefaultGatewayUrl("http://localhost/flashservices/gateway"); 
    

    Note:   There are several other ways to specify the gateway URL. For more information, see "Configuring Flash Remoting MX," in Chapter�2.

  3. Connect to the Flash Remoting gateway:
    gatewayConnection = NetServices.createGatewayConnection();
    
  4. Get a reference to the context root of the web application that contains the servlet or JSP, as shown in the following example:
    servletService = gatewayConnection.getService("mycontextroot", this);
    

    The first parameter of the getService function must be the context root of the web application that contains the servlet or JSP. The second parameter of the getService function, this, specifies that the results of service function calls are returned to this Flash timeline.

Calling a servlet or JSP

To call a servlet or a JSP defined as a servlet from ActionScript, use the servlet name specified in the web application's web.xml deployment descriptor file as an ActionScript function name. For example, the servlet name is MyServlet in the following example:

function go_Clicked()
{
  servletService.MyServlet();
}

The web.xml file contains the following servlet definition:

<servlet>
        <servlet-name>MyServlet</servlet-name>
        <display-name>MyServlet</display-name>
        <description>Simple text servlet</description>
        <servlet-class>MyServlet</servlet-class>
</servlet>

Note:   On Servlet 2.3-compliant application servers, you can define a JSP as a servlet by specifying a JSP file name in a jsp-file element, rather than a servlet class in a servlet-class element.

Request arguments sent from Flash as parameters of the ServletName(); function are available from the Request scope as the parameter "FLASH.PARAMS". You can return results to Flash using the request parameter "FLASH.RESULT", as shown in the following servlet:

import javax.servlet.*;
import java.io.IOException;
import java.util.List;

public class MyServlet implements Servlet
{
  private String message = null;

  public void init(ServletConfig config) throws ServletException
  {
    message = "Hello from MyServlet";
  }
  public void service(ServletRequest request, ServletResponse response)
      throws ServletException, IOException
  {
    //The args could be used here too...
    /*
    Object o = request.getAttribute("FLASH.PARAMS");
    if (o instanceof List)
    {
      List args = (List)o;
    }
    Object arg0 = args.get(0);
    Object arg1 = args.get(1);
    */
    request.setAttribute("FLASH.RESULT", message);
  }
  public String getServletInfo()
  {
    return "A test servlet.";
  }
  public ServletConfig getServletConfig()
  {
    return null;
  }
  public void destroy()
  {
    message = null;
  }
}

To handle the function results in ActionScript, you use a result handler function like this one:

function MyServlet_Result (result)
{
  ResultBox.text = result;
}

For more information about handling function results in ActionScript, see "Handling function results in ActionScript".


			  
						  
						  
                   












			
			
			

Comments


kemen said on Oct 14, 2002 at 11:06 AM :
there is not enough documentation about contextroot, web.xml congiguration.
I think a directory tree is a good solution to understand the path where put the file jsp or servlet

I received always an error: "No service named is known to Flash Remoting"




ohiojavaprgrmr said on Nov 6, 2003 at 6:42 AM :
Where is the Javadoc for the java classes that come with Flash Remoting? I tried to implement a servlet as a Flash Service and could not find any specification that describes the behavior of the flashgateway.adapter.java.FlashServlet methods.
jrunrandy said on Nov 12, 2003 at 11:34 AM :
The only method in FlashServlet is the service method, which is documented on this page. It also sets up FLASH.PARAMS and FLASH.RESULT request variables, which this page also describes.
sboub1 said on Jun 27, 2004 at 2:39 AM :
1000$ for ONE undocumented method????
can i send you my CV?
pavanathreya12 said on Jul 5, 2004 at 3:43 AM :
There is no Enough Documentation in Live Doc's about the topic "Returning an XMLObject from a Servlet to Flash" If possible please give a detailed Documentation with an example
thankyou
pavanathreya12@yahoo.com
No screen name said on Dec 20, 2004 at 3:07 AM :
I don't know how to use the ServletName() function to pass parameters from Flash to a Servlet.

thanks
No screen name said on Sep 30, 2005 at 8:00 AM :
Hi ,
In above code for calling Servlet through remoting
it should be

servletService = gatewayConnection.getService("mycontextroot/ServletName", this);


for calling JSP
servletService = gatewayConnection.getService("mycontextroot", this);

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flashremoting/mx/Using_Flash_Remoting_MX/usingFRJ2EE5.htm