Flex调用jsp页面及其属性

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值