zoukankan      html  css  js  c++  java
  • 在Web Services中管理Sessions

    Managing the session using the service client

    Managing the session on the client side involves bit of a work. As mentioned above, both in a SOAP session and Transport session a client has to send the session-related data if he wants to live in the same session. Maybe he can do that for a SOAP session by coping with required reference parameters, but with a Transport session a user must get access to the transport to copy and send cookies.

    To make life easier, Axis2 has the built-in capability of managing sessions in the client session by just setting a flag. Then, depending on the service side session, it will send the corresponding data as long as you use the same service client. So, the main requirement is to use the same service client to invoke the service if you want to live in the same session.

    If you want to live in the same session, you can create service client as shown below and re-use the created service client object to invoke the service.

    Options options = new Options();
    options.setManageSession(true);
    ServiceClient sender = new ServiceClient();
    sender.setOptions(options);
    

    Once you create the service client as shown above, if the service deploys in a SOAP session it will copy the serviceGroupId and send that from the second invocation onwards. If the server sends the session id, such as HTTP cookies, it will copy that to the service context (on the client side) and send it back to the server when the client invokes the service for a second time.


    在Web Services中管理Sessions (服务器端)

    http://www.apusic.com/article/article15.htm

        在Web service中通常采用两种公认技术来管理session,一种是借助HTTP和HTTP cookies,另一种是用SOAP headers。Axis能帮你实现这两种技术。

        在Web service中没有一种管理session的标准方法,只有两种公认的技术,一种是依靠HTTP和HTTP cookies,另一种,或许也是最重要的一种方法,就是用SOAP headers。Axis能帮助开发人员实现这两种技术。


        在Axis中缺省使用的是HTTP managed sessions。在一个服务器中这么做是十分容易的,因为大多数对Axis Web service的管理是通过org.apache.axis.MessageContext的一个实例来完成的。在一个Axis Web service中你可以通过调用MessageContext类中的静态方法来得到MessageContext的一个实例:




    public class SessionService
    {
      public String echo(String in)
      {
         MessageContext mc =
            MessageContext.getCurrentContext();


        MessageContext中有一个名为setMaintainSession的方法,调用它便可激活session。但在编写(Axis 1.1 RC2)时,session对象只有在被访问时才能激活,如下列代码所示:




    public class SessionService
    {
      public String echo(String in)
      {
         MessageContext mc = MessageContext.
            getCurrentContext();
         Session session = mc.getSession();
         String name = (String)session.get("name");
         return in;
      }
    }


        这样会导致Axis架构生成一个set-cookie header:




    Set-Cookie: 
    JSESSIONID=49EBBB19A1B2F8D10EE075F6F14CB8C9;
    Path=/axissessions


        客户端需要在Cookie header中返回这个Cookie来保持该session。为了使axis运行状态下的客户端能够实现这一点,就必须调用org.apache.axis.client.Service接口的setMaintainSession方法。该接口是由WSDL2Java生成工具所生成的Locator类实现的。调用该方法之后,Axis架构会自动将该cookie返回到服务器中:




     public static void main(String["> args)
      {
         UseSessionsServiceLocator locator = new
            UseSessionsServiceLocator();
         locator.setMaintainSession(true);


        header看起来就像这样:




     Cookie: 
    JSESSIONID=49EBBB19A1B2F8D10EE075F6F14CB8C9


        通过HTTP传输cookie是没有问题的,但如果客户端或服务器不通过HTTP,或使用的是通过多个Web services传入调用的multihop service,那么这种方法就不那么有效了。一种更好的方法是用SOAP headers来加载session id。


        Axis架构支持多个Handlers。通过在一个Web service请求过程中调用调栈(call stack),Handlers能够被放置到很多地方,它可以和传输过程结合起来,或者和一个Web service一起使用。Handlers可以被插入其中来处理Web service请求中的请求和/或响应语句。


        Axis带有一个名为org.apache.axis.handlers.SimpleSessionHandler的handler,它用于提供基于session管理的SOAP header。要使用这个简单的带有Web service的session handler,你必须告知Axis架构将该handler添加到handler链中。你可以通过将该handler信息添加到server-config.wsdd来实现这一点;一个简单的处理方法是定义一个包含额外配置Web service所需的WSDD文件,然后用Axis部署工具来部署这个配置文件。


        这个WSDD文件看起来就像这样:




          "http://xml.apache.org/axis/wsdd/"
      xmlns:java=
         "http://xml.apache.org/axis/wsdd/
            providers/java">

      type="java:org.apache.axis.handlers.
         SimpleSessionHandler"/>

               "java:RPC" style="wrapped">
         urn:kevinj:Sessions
         
               
         

         
               
         

                     "kevinj.UseSessions"/>
         
      




        该handler是和service分开定义并引用的,虽然你可以在service内部定义它。注意这个handler是同时为了请求和响应而定义的;这就确保了这些headers能够在请求中被读取并添加到响应中去。你可以用这个管理工具来部署它:




    java  -cp [classpath to axis bits here"> / 
            org.apache.axis.client.AdminClient /
            -lhttp://localhost/myservice/AxisServlet
            deploy.wsdd


        现在服务器就可以运行了,在使用该handler时服务器无需处理任何事情;而headers能够自动被添加进去,就像这样:





                              soapenv:mustUnderstand="0"
                              xsi:type="xsd:long"
                           xmlns:ns1=
                              "http://xml.apache.org/axis/
                              session">
                  -1919645576528915916
      



        要使用这个header,Web service客户端必须能够读取它并了解其语法;而Axis客户端可以解决这个问题。


        要创建一个使用这个简单session的Axis客户端,你需要配置Axis客户端框架来使用该handler。过程同服务器端很相似,但不是部署到服务器,而是在本地创建config文件。你可以通过运行org.apache.axis.utils.Admin来实现这一点。运行以下代码:




    org.apache.axis.utils.Admin client deploy.wsdd


        这样就创建了一个client-config.wsdd文件,它同样也包含handler代码。





      "http://xml.apache.org/axis/wsdd/"
    xmlns:java=   "http://xml.apache.org/axis/
      wsdd/providers/java">
         
                              value="admin"/>
                              value=
                     "org.apache.axis.attachments.
                     AttachmentsImpl"/>
                              "sendMultiRefs" value="true"/>
                              "true"/>
                              "sendXMLDeclaration" value="true"/>
                              value="true"/>
               
                                       "java:org.apache.axis.handlers.
                     SimpleSessionHandler"/>
               

               
                                          "java:org.apache.axis.handlers.
                        SimpleSessionHandler"/>
               

         

                  "java:org.apache.axis.handlers.
               SimpleSessionHandler"/>
                  "java:RPC" style="wrapped" use="literal">
               
                     
               

               
                     
               

               
                              "kevinj.UseSessions"/>
               urn:kevinj:Sessions
         

                  "java:org.apache.axis.transport.
            java.JavaSender"/>
                  "java:org.apache.axis.transport.
               http.HTTPSender"/>
                  "java:org.apache.axis.transport.
            local.LocalSender"/>


        为了使客户端能够利用这个handler,你必须将client-config.wsdd文件添加到客户端的classpath中。然后由Axis框架代表客户端来读取并响应这些headers。同样,客户端代码无需处理任何事情便可以使用它了。

  • 相关阅读:
    从清月高中物理动学课件制作工具说【FarseerPhysics引擎之WheelJoint】及【PropetryGrid之动态下拉列表】
    一种从纹理图片提取多边形的方法
    蒸汽世界满手尘土生命、水、光照锁定修改器
    【五子棋AI循序渐进】——整合完成
    洛谷-07敦刻尔克大撤退-[再也不坑]【二战2】二战系列2:狼烟四起
    洛谷-火柴棒等式-NOIP2008提高组复赛
    洛谷-笨小猴-NOIP2008提高组复赛
    NOIP2013-普及组复赛-第一题-计数问题
    NOIP2010-普及组复赛-第四题-三国游戏
    NOIP2012-普及组复赛-第二题-寻宝
  • 原文地址:https://www.cnblogs.com/huqingyu/p/1134202.html
Copyright © 2011-2022 走看看