Flex與Servlet之間資料的互動,FlexServlet資料
總結一下Flex與Servlet之間資料的互動:
代碼1—Flex代碼:
<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="sendRequest();"><mx:Script><![CDATA[ import mx.controls.Alert; public function sendRequest():void{ var request:URLRequest=new URLRequest("http://localhost:8080/flexAndServlet/TestServlet");//首先發送servlet請求:flexAndServlet為Java Web工程名、TestServlet為處理Flex發送過來的資料的Servlet request.method=URLRequestMethod.POST; request.data = new URLVariables("name=高煥傑"); //name為傳向Servlet的參數名 var loader:URLLoader=new URLLoader(); loader.load(request); loader.addEventListener(Event.COMPLETE, onLoaderComplete); } private function onLoaderComplete(event:Event):void{ var responseResult:String = ((event.target) as URLLoader).data;//servlet端傳回來的資料 Alert.show(responseResult, ""); }]]></mx:Script></mx:Application> 代碼2—Servlet代碼:
package com.ghj.packageofservlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class TestServlet extends HttpServlet {private static final long serialVersionUID = -2928176190622974743L;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("UTF-8");response.setContentType("text/pain;charset=UTF-8");PrintWriter out = response.getWriter();try {String name = request.getParameter("name");//擷取Flex發送回來的名為name的參數對應的參數值System.out.print("name:" + name);out.println("Servlet擷取的值為" + name);} finally {out.close();}}} 代碼3—web.xml代碼:
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><servlet><servlet-name>TestServlet</servlet-name><servlet-class>com.ghj.packageofservlet.TestServlet</servlet-class></servlet><servlet-mapping><servlet-name>TestServlet</servlet-name><url-pattern>/TestServlet</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
代碼4—jsp代碼:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>Flex與Servlet的互動</title></head><body><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"id="tempPictureUpload" width="100%" height="100%"codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab"><param name="movie"value="<%=basePath%>swf/flexAndServlet.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><param name="allowScriptAccess" value="sameDomain" /><embed name="tempPictureUpload"width="100%" height="100%" src="<%=basePath %>swf/flexAndServlet.swf"quality="high" bgcolor="#869ca7" align="middle" play="true" loop="false"allowScriptAccess="sameDomain" type="application/x-shockwave-flash"pluginspage="http://www.adobe.com/go/getflashplayer"></embed></object></body></html>
【
0分下載樣本】
flex servlet互動
用的是什麼架構?以什麼方式擷取servlet資訊?
servlet怎得到從flex傳過來的參數
這個好辦啊:
像這種資料互動,建議使用xml來傳遞資料,flex用戶端請求這個servlet時候,flex用戶端將所有資料都寫到一個xml中,再用post請求,send一個xml出去給servlet··
在servlet中有一個HttpServletRequest的參數,這個你總知道的吧,用HttpServletRequest.getInputStream();就能讀到flex發過來的請求,然後就dom4j來解析:SAXReader reader = new SAXReader(); 和Document document = reader.read(in);(這個in 就是用HttpServletRequest得到的輸入資料流),後面的你用dom4j怎麼都能得到吧····
有分送嗎? 呵呵~~