Flex使用HTTPService与java交互

| 阅读数:--次| 作者:小豆豆
摘要:java写个简单的servlet,flex调用servlet返回登录是否成功

1.打开flex,新建一个mxml,把下面代码复制进去

flex代码
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="init()"
minWidth="955" minHeight="600">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<!--[CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;

[Bindable]
private var selectResponse:String= null;
[Bindable]
private var arraylist:ArrayCollection = new ArrayCollection([
{name:"CN",data:"中文"},
{name:"US",data:"英文"},
]);
private function init():void{
// fService.addEventListener(ResultEvent.RESULT,successhandler);
// fService.addEventListener(FaultEvent.FAULT,faulthandler);
}
private function dropdownlistChangeHandler(event:Event = null):void{
var httpservice:HTTPService = new HTTPService();
httpservice.resultFormat = "text";
httpservice.url = "http://localhost:8080/testjava/flexServlet";
httpservice.method = "POST";
var param:Object = new Object;
param.selects = select.selectedItem.data;
httpservice.send(param);
httpservice.addEventListener(ResultEvent.RESULT,successhandler);
// fService.sendRequest(select.selectedItem.data);
// Alert.show(select.selectedItem.data);
}
private function successhandler(event:ResultEvent):void{
selectResponse = event.message.body.toString();
languages.text = selectResponse;
// Alert.show(selectResponse);
}
// private function faulthandler(event:FaultEvent):void{
// selectResponse = event.message.toString();
// languages.text = selectResponse;
// }
]]-->
</fx:Script>
<s:DropDownList x="109" y="66" id="select" labelField="name" dataProvider="{this.arraylist}" change="dropdownlistChangeHandler(event)">
</s:DropDownList>
<mx:Text id="languages" text="{selectResponse}" x="288" y="198"/>
</s:Application>
2.下面是servlet的java和web.xml代码

java代码
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FlexServlet extends HttpServlet {

private static final long serialVersionUID = 1L;
public FlexServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) {
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) {
try {
request.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String selects = request.getParameter("selects");
response.setContentType("text/html;charset=UTF-8");
PrintWriter pout = null;
try {
pout = response.getWriter();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pout.print("选择:" + selects);
}
}
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>flexServlet</servlet-name>
<servlet-class>servlet.FlexServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>flexServlet</servlet-name>
<url-pattern>/flexServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>testflex.html</welcome-file>
</welcome-file-list>
</web-app>

返回顶部
学到老代码浏览 关闭浏览