<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    【永恒的瞬間】
    ?Give me hapy ?
    Flex Remote Object中直接使用HttpSession的方法

    問題的背景

    Flex Remote Object可以是POJO,JavaBean或是EJB。在面向服務的架構中(Service Oriented Architecture),我們可以用Remote Object來作為Service Facade,利用應用服務器提供的persistent service來儲存狀態信息。

    Flex既可以提供stateful或stateless的remote object, 另外還有session servlet讓mxml獲取/和儲存session中的內容。這一切聽上去都很完美,但是有一個問題,Flex Remote Object本身是無法獲得任何有關Running Context的信息,也就是說,你無法從你的 Remote Object 中獲得 HttpSession, HttpRequest 和 ServletContext。 所謂的 Flex Session servlet只是讓MXML獲得session的內容,而不是直接讓Remote Object獲得session。

    Remote Object為什么需要獲得HttpRequest, HttpSession?
    既然Flex提供了stateful的remote object為什么還要讓remote object獲得Running Context呢?問題在于Flex中的stateful是基于應用服務器的http session,而且你無法控制AMFGateway建立remote object的過程。打個簡單的比方,我們知道一般的應用服務器中,session的時限只有20分鐘,而在很多系統的登陸過程中卻有選擇保持登陸幾個月的選項。

    其具體實現上就是利用cookie來儲存id和password hash,通過控制cookie的存活時間來實現的。而在服務器端,一旦session過期了,則可以從cookie中獲得id和password hash重新登陸一遍,從而達到自動認證用戶的目的。

    如果你的Remote Object無法獲得 HttpServletRequest, HttpSession,你就無法實現上述的情況。另外,對于小型的應用來說,直接在Remote object中獲得servlet context并利用它來儲存/獲得共享的資源,可以大大降低開發的復雜程度。

    解決方案
    要讓Remote Object獲得HttpSession,HttpRequest和ServletContext并不是一件容易的事情。這里提供了我的一種方法,供大家參考。希望能拋磚引玉,讓大家提出更好,更有效的方案。

    這個方法的基本思路是利用JAVA提供的 ThreadLocal Object。當服務器接收到一個HTTP請求后,這個請求的整個處理過程是運行在同一個線程中的。

    每個HTTP請求的處理會都運行在各自獨立的線程中。而在Flex中,所有AMF Remote Object 的請求都需要通過 AMF Gateway Servlet,而Remote Object 的建立和調用恰恰就是運行在這個HTTP請求的線程中。

    有了這個原則,我們就可以建立一個Context Object,每當請求建立的時候,就可以把這個請求放入 Context 的 ThreadLocal 中,而當 Remote Object 被AMF Gateway Servlet調用的時候,就可以通過訪問 Context 的ThreadLoca l來獲得其所對應的那個請求。

    而截獲發送到AMF Gateway的請求則可以通過Servlet Filter來實現。廢話不說了,看代碼吧!



    1. 添加以下內容到WEB-INF/web.xml中

    ?<filter>
    ? ? <filter-name>AMFSessionFilter </filter-name>
    ? ? <filter-class>com.netop.forum.servlets.AMFSessionFilter </filter-class>
    ? <filter>

    ? <filter-mapping>
    ? ? <filter-name>AMFSessionFilter </filter-name>
    ? ? <servlet-name>AMFGatewayServlet </servlet-name>
    ? <filter-mapping> ? ? ?


    2. AMFSessionFilter的代碼

    /*
    * Created on 1/07/2004
    */
    package com.netop.forum.servlets;


    import java.io.IOException;
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.*;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;

    /**
    * @author Zombie
    * @version 0.5
    */

    public class AMFSessionFilter implements Filter
    {
    ? ? ?private static Log log = LogFactory.getLog(AMFSessionFilter.class);
    ? ? ?
    ? ? ?public void init(FilterConfig config)
    ? ? ?{ ? ? ? ? ? ?
    ? ? ? ? ? ?log.info("Init AMFSessionFilter filter");
    ? ? ?}
    ? ? ?
    ? ? ?public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException,IOException
    ? ? ?{ ? ? ? ? ? ? ? ? ?
    ? ? ? ? ? ?AMFContext.setCurrentContext((HttpServletRequest)request, (HttpServletResponse)response); ? ? ? ? ? ?
    ? ? ? ? ? ?chain.doFilter(request,response); ? ? ? ? ? ?
    ? ? ?}
    ? ? ?
    ? ? ?public void destroy()
    ? ? ?{
    ? ? ? ? ? ?log.info("Destory AMFSessionFilter filter");
    ? ? ?}
    }


    3. AMFContext的代碼

    /*
    * Created on 1/07/2004
    */
    package com.netop.forum.servlets;


    import javax.servlet.*;
    import javax.servlet.http.*;
    import com.netop.forum.business.*;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;

    /**
    * @author Zombie
    * @version 0.5
    */
    public class AMFContext
    { ? ? ?
    ? ? ?
    ? ? ?/**
    ? ? ? * Context Attribute key for the connection the factory
    ? ? ? */
    ? ? ?public final static String CONNECTION_FACTORY_KEY = "sqlMapFactory"; ? ? ?

    ? ? ?/**
    ? ? ? * The log
    ? ? ? */
    ? ? ?private static Log log = LogFactory.getLog(AMFContext.class); ? ? ?

    ? ? ?/**
    ? ? ? * ThreadLocal object for storing object in current thread.
    ? ? ? */
    ? ? ?private static ThreadLocal tl = new ThreadLocal();
    ? ? ?
    ? ? ?/**
    ? ? ? * Set current context
    ? ? ? *
    ? ? ? * @param request The HttpRequest object
    ? ? ? * @param response The HttpResponses object
    ? ? ? */
    ? ? ?static public void setCurrentContext(HttpServletRequest request, HttpServletResponse response)
    ? ? ?{
    ? ? ? ? ? ?AMFContext c = getCurrentContext();
    ? ? ? ? ? ?if (c == null)
    ? ? ? ? ? ?{
    ? ? ? ? ? ? ? ? ?c = new AMFContext(request,response);
    ? ? ? ? ? ? ? ? ?tl.set(c);
    ? ? ? ? ? ?}
    ? ? ? ? ? ?else
    ? ? ? ? ? ?{
    ? ? ? ? ? ? ? ? ?c.setRequest(request);
    ? ? ? ? ? ? ? ? ?c.setResponse(response);
    ? ? ? ? ? ?} ? ? ? ? ? ?
    ? ? ?}
    ? ? ?
    ? ? ?/**
    ? ? ? * Get current context value
    ? ? ? * @return The current context
    ? ? ? */
    ? ? ?static public AMFContext getCurrentContext()
    ? ? ?{
    ? ? ? ? ? ?return (AMFContext)tl.get();
    ? ? ?}
    ? ? ?
    ? ? ?
    ? ? ?
    ? ? ?
    ? ? ?
    ? ? ?//----------------------------------------------------------
    ? ? ?//
    ? ? ?// Class members
    ? ? ?//
    ? ? ?//----------------------------------------------------------
    ? ? ?
    ? ? ?/**
    ? ? ? * The http request object. The lifecycle of the request object is defined as the request
    ? ? ? * scope. It may be reused in another incoming connection, so dont use it in another thread.
    ? ? ? */
    ? ? ?private HttpServletRequest request;
    ? ? ?
    ? ? ?/**
    ? ? ? * The http response object. The lifecycle of the response object is defined as the request
    ? ? ? * scope. Dont use it in another thread. Also dont write output to the response when it is
    ? ? ? * used in the context, but you may get or set some response header when it is safe.
    ? ? ? */
    ? ? ?private ?HttpServletResponse response;
    ? ? ?
    ? ? ?
    ? ? ?/**
    ? ? ? * The constructor is private, to get an instance of the AMFContext, please use
    ? ? ? * getCurrentContext() method.
    ? ? ? *
    ? ? ? * @param request
    ? ? ? * @param response
    ? ? ? */
    ? ? ?private AMFContext (HttpServletRequest request, HttpServletResponse response)
    ? ? ?{
    ? ? ? ? ? ?this.request = request;
    ? ? ? ? ? ?this.response = response;
    ? ? ?}
    ? ? ?
    ? ? ?
    ? ? ?/**
    ? ? ? * Get request object
    ? ? ? *
    ? ? ? * @return Http request object
    ? ? ? */
    ? ? ?public HttpServletRequest getRequest()
    ? ? ?{
    ? ? ? ? ? ?return request;
    ? ? ?}

    ? ? ?/**
    ? ? ? * Set request object
    ? ? ? *
    ? ? ? * @param Http request object
    ? ? ? */
    ? ? ?public void setRequest(HttpServletRequest request)
    ? ? ?{
    ? ? ? ? ? ?this.request = request;
    ? ? ?}

    ? ? ?/**
    ? ? ? * Get response object
    ? ? ? *
    ? ? ? * @return Http response object
    ? ? ? */
    ? ? ?public HttpServletResponse getResponse()
    ? ? ?{
    ? ? ? ? ? ?return response;
    ? ? ?}

    ? ? ?/**
    ? ? ? * Set response object
    ? ? ? *
    ? ? ? * @param response Http response object
    ? ? ? */
    ? ? ?public void setResponse(HttpServletResponse response)
    ? ? ?{
    ? ? ? ? ? ?this.response = response;
    ? ? ?} ? ? ?
    ? ? ?
    ? ? ?/**
    ? ? ? * Get the servlet context
    ? ? ? * @return
    ? ? ? */
    ? ? ?public ServletContext getServletContext()
    ? ? ?{
    ? ? ? ? ? ?HttpSession session = this.getSession();
    ? ? ? ? ? ?return session.getServletContext();
    ? ? ?}
    ? ? ?
    ? ? ?/**
    ? ? ? * Get the current running session
    ? ? ? * @return
    ? ? ? */
    ? ? ?public HttpSession getSession()
    ? ? ?{
    ? ? ? ? ? ?return request.getSession();
    ? ? ?}
    ? ? ?
    ? ? ?
    ? ? ?/**
    ? ? ? * Get an object stored in the session.
    ? ? ? *
    ? ? ? * @param attr Attribute Name
    ? ? ? * @return The value stored under the attribute name.
    ? ? ? */
    ? ? ?public Object getSessionAttribute(String attr)
    ? ? ?{
    ? ? ? ? ? ?HttpSession session = this.getSession();
    ? ? ? ? ? ?return session.getAttribute(attr);
    ? ? ?}
    ? ? ?
    ? ? ?/**
    ? ? ? * Store an object in the session.
    ? ? ? *
    ? ? ? * @param attr Attribute Name
    ? ? ? * @param value The value.
    ? ? ? */
    ? ? ?public void setSessionAttribute(String attr, Object value)
    ? ? ?{
    ? ? ? ? ? ?HttpSession session = this.getSession();
    ? ? ? ? ? ?session.setAttribute(attr, value);
    ? ? ?}
    ? ? ?
    ? ? ?/**
    ? ? ? * Get an object stored in the servlet context.
    ? ? ? *
    ? ? ? * @param attr Attribute Name
    ? ? ? * @return The value stored under the attribute name.
    ? ? ? */
    ? ? ?public Object getContextAttribute(String attr)
    ? ? ?{
    ? ? ? ? ? ?ServletContext sc = this.getServletContext();
    ? ? ? ? ? ?return sc.getAttribute(attr);
    ? ? ?}
    ? ? ?
    ? ? ?/**
    ? ? ? * Store an object in the servlet context.
    ? ? ? *
    ? ? ? * @param attr Attribute Name
    ? ? ? * @param value The value.
    ? ? ? */
    ? ? ?public void setContextAttribute(String attr, Object value)
    ? ? ?{
    ? ? ? ? ? ?ServletContext sc = this.getServletContext();
    ? ? ? ? ? ?sc.setAttribute(attr,value);
    ? ? ?}
    ? ? ?
    ? ? ?/**
    ? ? ? * Get an object stored in the current request.
    ? ? ? *
    ? ? ? * @param attr Attribute Name
    ? ? ? * @return The value stored under the attribute name.
    ? ? ? */
    ? ? ?public Object getRequestAttribute(String attr)
    ? ? ?{
    ? ? ? ? ? ?return request.getAttribute(attr);
    ? ? ?} ? ? ?
    ? ? ?
    ? ? ?/**
    ? ? ? * Store an object in the current request.
    ? ? ? *
    ? ? ? * @param attr Attribute Name
    ? ? ? * @param value The value.
    ? ? ? */
    ? ? ?public void setRequestAttribute(String attr, Object value)
    ? ? ?{
    ? ? ? ? ? ?request.setAttribute(attr,value);
    ? ? ?}

    ? ? ?
    ? ? ?/**
    ? ? ? * Get the connection factory from the servlet context. The connection factory is in the
    ? ? ? * application scope.
    ? ? ? *
    ? ? ? * @return The connection factory for creating sqlMap objects.
    ? ? ? */
    ? ? ?public ConnectionFactory getConnectionFactory()
    ? ? ?{
    ? ? ? ? ? ?ConnectionFactory factory =(ConnectionFactory)this.getContextAttribute(CONNECTION_FACTORY_KEY); ? ? ? ? ? ?
    ? ? ? ? ? ?if (factory == null)
    ? ? ? ? ? ?{
    ? ? ? ? ? ? ? ? ?try
    ? ? ? ? ? ? ? ? ?{
    ? ? ? ? ? ? ? ? ? ? ? ?factory = new ConnectionFactory();
    ? ? ? ? ? ? ? ? ? ? ? ?// ? ? ?factory is lazy instantiated, so we need to invoke it once to make sure it is ok.
    ? ? ? ? ? ? ? ? ? ? ? ?factory.getSqlMap();
    ? ? ? ? ? ? ? ? ? ? ? ?this.setContextAttribute(CONNECTION_FACTORY_KEY, factory); ? ? ? ? ? ? ? ? ? ? ? ?
    ? ? ? ? ? ? ? ? ?}
    ? ? ? ? ? ? ? ? ?catch (Throwable e)
    ? ? ? ? ? ? ? ? ?{
    ? ? ? ? ? ? ? ? ? ? ? ?log.fatal("Can not create connection factory: "+e.getMessage(), e); ? ? ? ? ? ? ? ? ? ? ? ?
    ? ? ? ? ? ? ? ? ?}
    ? ? ? ? ? ?} ? ? ? ? ? ?
    ? ? ? ? ? ?return factory; ? ? ? ? ? ?
    ? ? ?}
    ? ? ?
    }

    4. 如何在remote object中使用AMFContext


    class YouRemoteService
    {
    ?public void serviceMethod()
    ?{
    ? ?AMFContext context = AMFContext.getCurrentContext();
    ? ?HttpSession = context.getSession();
    ? ?ServletContext = context.getServletContext();

    ? ?HttpServletRequest request = context.getRequest();
    ? ?HttpServletResponse response = context.getResponse();

    ? ?context.setSessionAttribute("attr","value");
    ? ?context.setContextAttribute("attr","value");

    ?}
    }

    posted on 2007-02-28 13:38 ???MengChuChen 閱讀(1716) 評論(0)  編輯  收藏 所屬分類: flex2.0
    主站蜘蛛池模板: 国产又长又粗又爽免费视频| 成人影片麻豆国产影片免费观看| 亚洲高清偷拍一区二区三区| 亚洲欧美日韩中文二区| 在线观看成人免费视频不卡| 自怕偷自怕亚洲精品| 99久久综合精品免费| 亚洲短视频在线观看| 日本妇人成熟免费中文字幕| 亚洲一区二区三区高清在线观看| 日韩高清免费观看| 免费看又黄又爽又猛的视频软件 | 免费视频成人国产精品网站| 国产在线不卡免费播放| 国产精品自拍亚洲| 亚洲欧洲国产成人综合在线观看| 国产精品高清免费网站| 亚洲国产一区二区三区青草影视| 精品福利一区二区三区免费视频| 一本色道久久88亚洲精品综合 | 久久国产精品免费| 亚洲精品国产精品乱码在线观看| 免费国产成人α片| 亚洲国产av一区二区三区丶| 国产免费资源高清小视频在线观看| 日本激情猛烈在线看免费观看| 国产亚洲综合网曝门系列| 91在线老王精品免费播放| 亚洲欧美国产国产一区二区三区| 亚洲av午夜精品一区二区三区| 91免费在线视频| 亚洲乱码卡一卡二卡三| 亚洲国产中文字幕在线观看| 久草福利资源网站免费| 亚洲乱码国产乱码精华| 亚洲性猛交XXXX| 在人线av无码免费高潮喷水| 一级毛片a免费播放王色电影| 亚洲人成网址在线观看| 小小影视日本动漫观看免费| 香蕉成人免费看片视频app下载 |