不管前端展现技术是jsp或者是Flex抑或其他web语言,对于Server而言只会看到通过HTTP协议传送过来的数据(不论是POST还是GET方法);不同的只是jsp和Flex提交请求的方式(语法不同) 和 对服务器返回的数据展现的方式。
下面给出了一个Flex 和 Servlet(struts的Action) 通信的例子。
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:HTTPService id="srv" url="http://127.0.0.1:8080/Flex/flex.do" method="POST" result="mx.controls.Alert.show(srv.lastResult.toString())"> <mx:request> <first> {first.text} </first> <last> {last.text} </last> <email> {email.text} </email> </mx:request> </mx:HTTPService> <mx:Form> <mx:FormItem label="First Name"> <mx:TextInput id="first"/> </mx:FormItem> <mx:FormItem label="Last Name"> <mx:TextInput id="last"/> </mx:FormItem> <mx:FormItem label="Email"> <mx:TextInput id="email"/> </mx:FormItem> <mx:FormItem> <mx:Button label="Subscribe" click="srv.send()"/> </mx:FormItem> </mx:Form> </mx:Application>
package com.trendmicro.struts.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class FllexAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String first =request.getParameter("first");
String last =request.getParameter("last");
String email =request.getParameter("email");
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
try {
PrintWriter pw = response.getWriter();
pw.write(first+last+email);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}