Managing sessions in Web services

Source: Internet
Author: User
Tags soap sessions web services

There are two generally accepted techniques for managing sessions in Web service, one using HTTP and HTTP cookies, and the other with SOAP headers. Axis can help you achieve both of these technologies.

There is no standard method of managing session in Web service, there are only two recognized technologies, one is relying on HTTP and HTTP cookies, the other, and perhaps the most important method, is to use SOAP headers. Axis can help developers implement both technologies.

HTTP managed sessions is used by default in axis. This is easy to do in a server because most of the management of the Axis Web service is done through an instance of Org.apache.axis.MessageContext. In an axis Web service you can get an instance of Messagecontext by calling the static method in the Messagecontext class:

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

There is a method named Setmaintainsession in the Messagecontext that invokes it to activate the session. However, when writing (Axis 1.1 RC2), the session object can only be activated when it is accessed, as shown in the following code:

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

This causes the axis schema to generate a Set-cookie Header:set-cookie:

JSESSIONID=49EBBB19A1B2F8D10EE075F6F14CB8C9;
Path=/axissessions

The client needs to return this cookie in the cookie header to hold the session. In order for the client in Axis running state to achieve this, the Setmaintainsession method of the Org.apache.axis.client.Service interface must be invoked. The interface is implemented by the locator class generated by the Wsdl2java build tool. After the method is invoked, the axis schema automatically returns the cookie to the server:

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

The header looks like this:

Cookie: JSESSIONID=49EBBB19A1B2F8D10EE075F6F14CB8C9

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.