V. WebService Session management

Source: Internet
Author: User

1. the WebService test code for creating a Session is simple. Is to record the user login status information, put in MessageContext ServiceContext. The Code is as follows:

Code package com. hoo. service; import org. apache. axis2.context. messageContext; import org. apache. axis2.context. serviceContext;/*** <B> function: </B> WebService Session message * @ author hoojo * @ createDate 2011-3-8 04:11:27 * @ file LoginService. java * @ package com. hoo. service * @ project Axis2WebService * @ blog http://blog.csdn.net/IBM_hoojo * @ email hoojo_@126.com * @ version 1.0 */public class LoginService {public boolean login (String userName, String password) {MessageContext context = MessageContext. getCurrentMessageContext (); ServiceContext ctx = context. getServiceContext (); if ("admin ". equals (userName) & "123456 ". equals (password) {ctx. setProperty ("userName", userName); ctx. setProperty ("password", password); ctx. setProperty ("msg", "Login successful"); return true;} ctx. setProperty ("msg", "Logon Failed"); return false;} public String getLoginMessage () {MessageContext context = MessageContext. getCurrentMessageContext (); ServiceContext ctx = context. getServiceContext (); return ctx. getProperty ("userName") + "#" + ctx. getProperty ("msg ");}}

In this example, the context of ServiceContext is obtained through MessageContext, setProperty is used to save the Session information, and getProperty is used to obtain the session information.

The role of Session can be used to save the user's logon status or Session message when using WebService logon. However, when using session, you need to set the scope of WebService when publishing the Service. The default scope is request, which has three other values: application, soapsession, and transportsession; we can choose to use transportsession and application to manage sessions of the same WebService class and cross-WebService class respectively.

2. Use axis2 to generate the aar file and publish the LoginService service.

The steps for publishing LoginService here are not described in detail, as mentioned above. When you use this step to publish WebService, you can open the aar file in the compressed file mode (in fact, it is not necessary to use the aar file, but the officially recommended aar file is also acceptable ), there is a service under the META-INF directory. xml file, see its <service> label, no scope attribute is set.

The default scope is request. We need to modify the content of services. xml.

After modification, the content of services. xml is as follows:

Code <service name = "LoginService" scope = "transportsession"> <description> Please Type your service description here </description> <messageReceivers> <messageReceiver mep = "http://www.w3.org/2004/08/wsdl/in-only" class =" org. apache. axis2.rpc. receivers. RPCInOnlyMessageReceiver "/> <messageReceiver mep =" http://www.w3.org/2004/08/wsdl/in-out "class =" org. apache. axis2.rpc. receivers. RPCMessageReceiver "/> </messageReceivers> <parameter name =" ServiceClass "> com. hoo. service. loginService </parameter> </service>

Scope in this place is the key. If it is not set here, the session will not be able to store information.

3, Use the console doscommand to publish WebService

This is a manual package, that is, the jar command we often use.

First, we need a service. xml file with the above content.

<Service name ="LoginService"Scope ="Transportsession">

Name is the name of the currently published WebService, and scope is the session scope. The transportsession is used to store the data in the Session.

<Parameter name ="ServiceClass"> Com. hoo. service. LoginService </parameter>

This is the class path of the current Service, package name + class name

<MessageReceivers>

<MessageReceiver mep =Http://www.w3.org/2004/08/wsdl/in-only"

Class ="Org. apache. axis2.rpc. receivers. RPCInOnlyMessageReceiver"/>

<MessageReceiver mep =Http://www.w3.org/2004/08/wsdl/in-out"

Class ="Org. apache. axis2.rpc. receivers. RPCMessageReceiver"/>

</MessageReceivers>

This should be the parsing class for parameters and returned values. in-only is the input, that is, the parameter, and in-out is the output, that is, the return value.

A. First, create A sessionService directory under drive C, and then. in the xml file ServiceClass class file copy to this directory, note to carry the package directory, and then in the sessionService directory to create a new META-INF above services. put the xml file in this directory.

B. Run cmd to go to the console and enter the sessionService directory. Enter the following command:

Jar cvf service. aar.

Service. aar is the name of the packaged file. It refers to the current directory.

If you do not understand the jar command, you can enter the jar command with a command syntax prompt.

Go to the sessionService directory and you will see the service. aar file. Then copy the file to the services directory under the axis2 WEB-INF directory, that is

[Tomcat-home] \ webapps \ axis2 \ WEB-INF \ services

Then you can use: http: // localhost: 8080/axis2/services/listServices

You can view the service just released.

This completes the manual release, packaging, and publishing of WebService.

C. Compile the Request Code of the WebService Client

Code package com. hoo. service; import javax. xml. namespace. QName; import org. apache. axis2.AxisFault; import org. apache. axis2.addressing. endpointReference; import org. apache. axis2.client. options; import org. apache. axis2.rpc. client. RPCServiceClient;/*** <B> function: </B> the LoginService Session client code * @ author hoojo * @ createDate 2011-3-9 11:21:59 am * @ file LoginServiceClient. java * @ package com. hoo. service * @ project Axis2WebService * @ blog http://blog.csdn.net/IBM_hoojo * @ email hoojo_@126.com * @ version 1.0 */public class LoginServiceClient {public static void main (String [] args) throws AxisFault {String target = "http: // localhost: 8080/axis2/services/LoginService"; // target = "http: // localhost: 8080/axis2/services/MyLoginService "; RPCServiceClient client = new RPCServiceClient (); Options options = client. getOptions (); options. setManageSession (true); EndpointReference epr = new EndpointReference (target); options. setTo (epr); QName qname = new QName ("http://service.hoo.com", "login"); // specify the call method and pass parameter data, and set the type of the returned value Object [] result = client. invokeBlocking (qname, new Object [] {"admin", "123456"}, new Class [] {boolean. class}); System. out. println (result [0]); qname = new QName ("http://service.hoo.com", "getLoginMessage"); result = client. invokeBlocking (qname, new Object [] {null}, new Class [] {String. class}); System. out. println (result [0]) ;}}

The result is as follows:

True

Admin # login successful

The above describes options. setManageSession (True); Enables Session management.

Related Article

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.