隐锋的BLOG
ASP,.net开发

      在开发web程序的过程中,我们经常要从session、application等JSP内置对象中获取变量值,在jsp页面、servlet中我们很容易就能办到,但是在Flex中就比较麻烦。不过,通过变通的方法我们还是可以从session、application对象中获取变量值的,其思路就是:通过HttpService组件访问一个通用的HttpServlet类,在HttpServlet类中根据不同的条件从不同的JSP内置对象中获取变量值。

      下面给出主要的代码供参考:

 

一、HttpServlet类的源码:

Java代码 复制代码
  1. public class JspServlet extends HttpServlet {   
  2.     public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {   
  3.         response.setContentType("text/html");   
  4.            
  5.         String scope = request.getParameter("scope");   
  6.         String param = request.getParameter("param");   
  7.         String result = null;   
  8.            
  9.         if(scope.equals("session")){   
  10.             result = (String)request.getSession().getAttribute(param);   
  11.         }else if(scope.equals("application")){   
  12.             result = (String)getServletContext().getAttribute(param);   
  13.         }   
  14.            
  15.         PrintWriter out = response.getWriter();   
  16.         out.print(result);   
  17.         out.flush();   
  18.         out.close();   
  19.     }   
  20.   
  21.     public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {   
  22.         doGet(request, response);   
  23.     }   
  24. }  

 

二、mxml文件的源码:

Xml代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()">  
  3.     <mx:Script>  
  4.         <![CDATA[  
  5.             import mx.controls.Alert;  
  6.             import mx.rpc.Fault;  
  7.             import mx.rpc.events.FaultEvent;  
  8.             import mx.rpc.events.ResultEvent;  
  9.               
  10.             private function call():void{  
  11.                 jspServlet.request.scope = "application"; //表示要从application对象获取变量值  
  12.                 jspServlet.request.param = "username"; //变量名  
  13.                 jspServlet.send();  
  14.             }  
  15.                 
  16.             private function resultHandler(event:ResultEvent):void{     
  17.                 txt1.text = event.result as String;  
  18.             }  
  19.                  
  20.             private function faultHandler(event:FaultEvent):void{     
  21.                 var fault:Fault = event.fault;     
  22.                 var s:String = (fault.faultDetail!=null) ? fault.faultDetail : fault.faultString;     
  23.                 Alert.show(s);   
  24.             }  
  25.  
  26.         ]]>  
  27.     </mx:Script>  
  28.        
  29.     <mx:HTTPService id="jspServlet" url="../jspServlet"       
  30.         result="resultHandler(event)"     
  31.         fault="faultHandler(event)"     
  32.         resultFormat="text"     
  33.         method="POST"     
  34.         useProxy="false"    
  35.         showBusyCursor="true"/>    
  36.   
  37.     <mx:Button x="27" y="28" label="Load" click="call()"/>  
  38.     <mx:TextArea x="27" y="58" width="450" height="143" id="txt1"/>  
  39.        
  40. </mx:Application>  
posted on 2009-12-04 14:18  糊涂隐锋  阅读(847)  评论(0编辑  收藏  举报