码迷,mamicode.com
首页 > 其他好文 > 详细

Flex与Servlet之间数据的交互

时间:2014-10-14 17:32:39      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   io   os   ar   java   sp   数据   

        总结一下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之间数据的交互

标签:style   http   color   io   os   ar   java   sp   数据   

原文地址:http://blog.csdn.net/gaohuanjie/article/details/40079577

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!