Axis2 (5): session management

Source: Internet
Author: User

WebService the most intuitive feeling is that it consists of methods, and call these methods on the client through the soap protocol. These methods may return values, or they may not. Although some tools can be completed in this way, the called methods are isolated. After a method is called, The status after the method is called cannot be obtained in other methods, that is to say, the status cannot be retained.

As you can imagine, this status cannot be retained for a complete Program , this means that it is difficult to complete all the work by relying on WebService . For example, You need to log on to a complete application system. in Web use session in the application to save the user logon status, however, if you use the WebService method to log on, It is very embarrassing to save the logon status. Of course, this can also be solved through other methods, such as using the static variable on the server to save the user status, and send a id to the client, the user status is obtained by passing the id on the server and client. This is very similar to the Web session and cookies to manage user statuses. But this requires developers to do a lot of work, but fortunately axis2 provides us with the WebService Status management function.

use axis2 is transparent to developers. You must use the Org. apache. axis2.context. messagecontext and Org. apache. axis2.context. servicecontext class to save and obtain the status information stored on the server, this is like using the httpsession interface getattribute and setattribute method acquisition and setting sessio N domain attribute.

You must modify services. XML file content, attribute to the element. The attribute has four optional values: application, soapsession, transportsession, request , although the official documentation of axis2 adds Both the initial and abbreviation letters are written in uppercase. However, after the test, all lowercase letters are valid, that is, the four values should be: application , soapsession , transportsession , request , the request is the default value of the scope attribute. You can select transportsession and application implements the same WebService class and cross WebService class session management.

UseSetmanagesession (true)OpenSessionManagement function.

To sum up, implement the sameWebServiceOfSessionThe following three steps are required:

1.UseMessagecontextAndServicecontextObtain and setKey-ValueYes.

2.ForSessionManagedWebServiceThe<Service>AddScopeProperty, and set this property valueTransportsession.

3.Use on the clientSetmanagesession (true)OpenSessionManagement function.

Below isWebServiceClass ManagementSession.

Create a WebService class first,CodeAs follows:


Package Service;
Import Org. Apache. axis2.context. servicecontext;
Import Org. Apache. axis2.context. messagecontext;
Public   Class Loginservice
{
Public   Boolean Login (string username, string password)
{
If ( " Bill " . Equals (username) &&   " 1234 " . Equals (password ))
{
// Step 2: Set the key-Value Pair
Messagecontext MC = Messagecontext. getcurrentmessagecontext ();
Servicecontext SC = MC. getservicecontext ();
SC. setproperty ( " Login " , " Logon successful " );
Return   True ;
}
Else
{
Return   False ;
}
}
Public String getloginmsg ()
{
// Step 2: obtain the value in the key-Value Pair
Messagecontext MC = Messagecontext. getcurrentmessagecontext ();
Servicecontext SC = MC. getservicecontext ();
Return (String) SC. getproperty ( " Login " );
}
}


InLoginserviceThere are two methods in the class:LoginAndGetloginmsg, IfLoginMethod to log on successfully. The "Successful Logon" string is saved inServicecontextObject. IfLoginMethod returnTrueCall laterGetloginmsgThe system returns "Successful Logon ".

Below isLoginserviceClass configuration code (Services. xml):

<! -- Step 2: add scope attributes -->
< Service Name = "Loginservice" Scope = "Transportsession" >
< Description >
LOGON SERVICE
</ Description >
< Parameter Name = "Serviceclass" >
Service. loginservice
</ Parameter >
< Messagereceivers >
< Messagereceiver MEP = "Http://www.w3.org/2004/08/wsdl/in-out"
Class = "Org. Apache. axis2.rpc. Receivers. rpcmessagereceiver"   />
</ Messagereceivers >
</ Service >


Use the following command to generateStubClass:

% Axis2_home % \ bin \ wsdl2java-Uri http: // localhost: 8080 /Axis2/services/loginservice? WSDL-P client-S-O stub


InStub \ SRC \ ClientDirectory to generateLoginservicestub. JavaClass, find the following constructor method in the class:

Public Loginservicestub (Org. Apache. axis2.context. configurationcontext,
Java. Lang. String targetendpoint, Boolean Useseparatelistener)
Throws Org. Apache. axis2.axisfault
{

_ Serviceclient. getoptions (). setsoapversionuri (
Org. Apache. Axiom. Soap. soap12constants. soap_envelope_namespace_uri );
}


Add the following code at the end of the method:

// Step 2: Enable the session management function of the Client
_ Serviceclient. getoptions (). setmanagesession ( True );


Use the following client codeLoginservicestubThe object has accessed the createdWebService:

Loginservicestub stub =   New Loginservicestub ();
Loginservicestub. login Login =   New Loginservicestub. login ();
Login. setusername ( " Bill " );
Login. setpassword ( " 1234 " );
If (Stub. login (LOGIN). local_return)
{
System. Out. println (stub. getloginmsg (). local_return );
}


After running the above Code, the message "Successful Logon" is displayed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.