Axis2 released WebService (4) session Management of-webservice

Source: Internet
Author: User

First, the webservice added session management code, the use of the Messagecontext class and Servicecontext class. The code is as follows:

 PackageCom.hoo.service;ImportOrg.apache.axis2.context.MessageContext;ImportOrg.apache.axis2.context.ServiceContext; Public classLoginservice {//Login Method     Public BooleanLogin (String username,string password) {//instantiate a Messagecontext objectMessagecontext context =Messagecontext.getcurrentmessagecontext (); //gets the Servicecontext object that is used to manage the session object in a single webservice.Servicecontext CTX =Context.getservicecontext (); if("admin". Equals (UserName) && "123456". Equals (password)) {                        //adding attributes and values to the sessionCtx.setproperty ("username", UserName); Ctx.setproperty ("Password", password); Ctx.setproperty ("MSG", "Landing success"); return true; }Else{Ctx.setproperty ("MSG", "Login Failed"); return false; }    }         PublicString getloginmessage () {Messagecontext context=Messagecontext.getcurrentmessagecontext (); Servicecontext CTX=Context.getservicecontext (); //gets the value of the attribute in the sessionString msg = Ctx.getproperty ("username") + "," +ctx.getproperty ("MSG"); returnmsg; }}
The session information is saved here through the Messagecontext context to obtain the servicecontext context, and then through the SetProperty to save the session information, through the GetProperty to get session information.

The function of the session is to save the user's login status or session message when the WebService is logged in.

The WebService scope defaults to request, and it has three additional values, namely: Application, Soapsession, transportsession ; We can choose to use transportsession and application to implement session management for the same WebService class and cross-WebService classes, respectively.

Second, release WebService, modify the Services.xml file

After releasing WebService with Eclipse's Axis2 plugin, you need to open the Aar file with an extract file and locate the Services.xml file under the Web-inf folder.

In <service name="Loginservice" scope="Transportsession"> Add a description of the scope.

Third, write the request code of the WebService client
ImportJavax.xml.namespace.QName;ImportOrg.apache.axis2.AxisFault;Importorg.apache.axis2.addressing.EndpointReference;Importorg.apache.axis2.client.Options;Importorg.apache.axis2.rpc.client.RPCServiceClient; Public classloginwebserviceclient { Public Static voidMain (string[] args)throwsAxisfault {String target= "Http://localhost:8080/axis2/services/LoginService"; Rpcserviceclient Client=Newrpcserviceclient (); Options Options=client.getoptions (); //Open the support for the sessionOptions.setmanagesession (true); EndpointReference EPR=Newendpointreference (target);                Options.setto (EPR); //Name of functionQName QName =NewQName ("http://service.hoo.com", "Login"); //specify the method to invoke and pass the parameter data, and set the type of the return valueObject[] result = client.invokeblocking (QName,NewObject[] {"admin", "123456"},NewClass[] {Boolean.class }); System.out.println (result[0]); QName=NewQName ("http://service.hoo.com", "Getloginmessage"); Result= Client.invokeblocking (QName,NewObject[] {NULL},NewClass[] {String.class }); System.out.println (result[0]); }}

After execution, the results are as follows:

True

Admin, Login successful

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.