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.
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);
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.
#include "NetServices.as"
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.
gatewayConnection = NetServices.createGatewayConnection();
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.
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".
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
Comments
kemen said on Oct 14, 2002 at 11:06 AM : ohiojavaprgrmr said on Nov 6, 2003 at 6:42 AM :