在Flex中访问JSP的session、application属性值[转]

      在开发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. }  
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代码 复制代码
  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>  

转载于:https://www.cnblogs.com/coolattt/archive/2009/12/04/1616989.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值