flex的简单调用jsp页面的方法:
navigateToURL(new URLRequest("jsp的url地址”), "_blank");
在开发web程序的过程中,我们经常要从session、application等JSP内置对象中获取变量值,在jsp页面、servlet中我们很容易就能办到,但是在Flex中就比较麻烦。不过,通过变通的方法我们还是可以从session、application对象中获取变量值的,其思路就是:通过HttpService组件访问一个通用的HttpServlet类,在HttpServlet类中根据不同的条件从不同的JSP内置对象中获取变量值。
下面给出主要的代码供参考:
一、HttpServlet类的源码:
public class JspServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html");
String scope = request.getParameter("scope");
String param = request.getParameter("param");
String result = null;
if(scope.equals("session")){
result = (String)request.getSession().getAttribute(param);
}else if(scope.equals("application")){
result = (String)getServletContext().getAttribute(param);
}
PrintWriter out = response.getWriter();
out.print(result);
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
doGet(request, response);
}
}
二、mxml文件的源码:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.Fault;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
private function call():void{
jspServlet.request.scope = "application"; //表示要从application对象获取变量值
jspServlet.request.param = "username"; //变量名
jspServlet.send();
}
private function resultHandler(event:ResultEvent):void{
txt1.text = event.result as String;
}
private function faultHandler(event:FaultEvent):void{
var fault:Fault = event.fault;
var s:String = (fault.faultDetail!=null) ? fault.faultDetail : fault.faultString;
Alert.show(s);
}
]]>
</mx:Script>
<mx:HTTPService id="jspServlet" url="../jspServlet"
result="resultHandler(event)"
fault="faultHandler(event)"
resultFormat="text"
method="POST"
useProxy="false"
showBusyCursor="true"/>
<mx:Button x="27" y="28" label="Load" click="call()"/>
<mx:TextArea x="27" y="58" width="450" height="143" id="txt1"/>
</mx:Application>
参考
http://chenjumin.iteye.com/blog/426549
http://huangfeng555.iteye.com/blog/813361