首页 > Java开发 > Flex与Servlet之间数据的交互

Flex与Servlet之间数据的交互

 总结一下Flex与Servlet之间数据的交互:
        代码1—Flex代码:

[plain]

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="sendRequest();">  
  3.     <mx:Script>  
  4.         <![CDATA[  
  5.             import mx.controls.Alert;  
  6.                      
  7.             public function sendRequest():void{  
  8.                 var request:URLRequest=new URLRequest("http://localhost:8080/flexAndServlet/TestServlet");//首先发送servlet请求:flexAndServlet为Java Web工程名、TestServlet为处理Flex发送过来的数据的Servlet  
  9.                 request.method=URLRequestMethod.POST;  
  10.                 request.data = new URLVariables("name=高焕杰"); //name为传向Servlet的参数名  
  11.                 var loader:URLLoader=new URLLoader();  
  12.                 loader.load(request);  
  13.                 loader.addEventListener(Event.COMPLETE, onLoaderComplete);  
  14.             }  
  15.           
  16.             private function onLoaderComplete(event:Event):void{  
  17.                 var responseResult:String = ((event.target) as URLLoader).data;//servlet端传回来的数据  
  18.                 Alert.show(responseResult, "");  
  19.             }  
  20.         ]]>  
  21.     </mx:Script>  
  22. </mx:Application>  

        代码2—Servlet代码:

[java]

  1. package com.ghj.packageofservlet;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5.   
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.http.HttpServlet;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. public class TestServlet extends HttpServlet {  
  12.   
  13.     private static final long serialVersionUID = -2928176190622974743L;  
  14.   
  15.     public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {  
  16.         doPost(request, response);  
  17.     }  
  18.   
  19.     public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {  
  20.         request.setCharacterEncoding("UTF-8");  
  21.         response.setContentType("text/pain;charset=UTF-8");  
  22.         PrintWriter out = response.getWriter();  
  23.         try {  
  24.             String name = request.getParameter("name");//获取Flex发送回来的名为name的参数对应的参数值  
  25.             System.out.print("name:" + name);  
  26.             out.println("Servlet获取的值为" + name);  
  27.         } finally {  
  28.             out.close();  
  29.         }  
  30.     }  
  31. }  

        代码3—web.xml代码:

[html]

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.     <servlet>  
  7.         <servlet-name>TestServlet</servlet-name>  
  8.         <servlet-class>com.ghj.packageofservlet.TestServlet</servlet-class>  
  9.     </servlet>  
  10.   
  11.     <servlet-mapping>  
  12.         <servlet-name>TestServlet</servlet-name>  
  13.         <url-pattern>/TestServlet</url-pattern>  
  14.     </servlet-mapping>  
  15.       
  16.     <welcome-file-list>  
  17.         <welcome-file>index.jsp</welcome-file>  
  18.     </welcome-file-list>  
  19. </web-app>  

        代码4—jsp代码:

[plain]

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>  
  2. <%  
  3.     String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";  
  4. %>  
  5.   
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  7. <html>  
  8.     <head>  
  9.         <title>Flex与Servlet的交互</title>  
  10.     </head>  
  11.       
  12.     <body>  
  13.         <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"  
  14.             id="tempPictureUpload" width="100%" height="100%"  
  15.             codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">  
  16.             <param name="movie"  
  17.                 value="<%=basePath%>swf/flexAndServlet.swf" />  
  18.             <param name="quality" value="high" />  
  19.             <param name="bgcolor" value="#ffffff" />  
  20.             <param name="allowScriptAccess" value="sameDomain" />  
  21.               
  22.             <embed name="tempPictureUpload"  
  23.                 width="100%" height="100%"   
  24.                 src="<%=basePath %>swf/flexAndServlet.swf"  
  25.                 quality="high" bgcolor="#869ca7"   
  26.                 align="middle" play="true" loop="false"  
  27.                 allowScriptAccess="sameDomain" type="application/x-shockwave-flash"  
  28.                 pluginspage="http://www.adobe.com/go/getflashplayer">  
  29.             </embed>  
  30.         </object>  
  31.     </body>  
  32. </html> 

本文固定链接: http://www.devba.com/index.php/archives/6022.html | 开发吧

报歉!评论已关闭.