首页 > 代码库 > Flex与Servlet之间数据的交互

Flex与Servlet之间数据的交互

        总结一下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 = http://www.mamicode.com/new URLVariables("name=高焕杰"); //name为传向Servlet的参数名>        代码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=http://www.mamicode.com/"swf/flexAndServlet.swf" />>        【0分下载示例

Flex与Servlet之间数据的交互