Question

I am very new to web service stuff so please be kind.

I have written a simple POJO class, and deployed it on an axis2 server:

public class Database {

    private Project project;

    public void login(){
        project = new Project();
        project.setDescription("Hello there");
        project.setName("To me");
    }

    public Project getProject(){
        return project;
    }

}

I call the service from a c# client:

localhost.Database db = new WindowsFormsApplication1.localhost.Database();
db.login();

localhost.getProjectResponse pr = new WindowsFormsApplication1.localhost.getProjectResponse();

pr = db.getProject();

When I debug the response is null. At the java end, when I call getProject, the project object is null.

What's happening? How do I preserve the state of project between service calls?

Was it helpful?

Solution

For most toolkits, web services are stateless by default. I think axis is no different.

If you want to maintain state between calls then you will need to enable sessions. An example on how to maintain sessions in axis can be found at:

http://kickjava.com/src/test/session/TestSimpleSession.java.htm

On the .NET side you will need to assign a CookieContainer to your request to store the session identifier. See HOW TO: Use CookieContainer to Maintain a State in Web Services for more information.

I think your code would look something like this:

localhost.Database db = new WindowsFormsApplication1.localhost.Database();
// Assign the CookieContainer to the proxy class.  
db.CookieContainer = new System.Net.CookieContainer();

db.login();

localhost.getProjectResponse pr = new WindowsFormsApplication1.localhost.getProjectResponse();
pr.CookieContainer = db.CookieContainer;

pr = db.getProject();


I think that should let you do what you want -- but I wouldn't recommend it.

Designing service interfaces is a bit different than designing object oriented interfaces. Service interfaces typically eschew the use of state and instead require the consumer to provide all of the relevant information in the request.

From Service-Oriented Architecture:

Services should be independent, self-contained requests, which do not require information or state from one request to another when implemented.

I would definitely recommend reading that article and perhaps revisiting your design.

OTHER TIPS

I'm not sure why @shivaspk left a comment instead of writing an answer, it is quite correct: web service calls (not just axis calls) are meant to be stateless, so although the project object gets created by

db.login();

when you call

db.getProject();

It is being called on a different instance of your Database class that was created by Axis to service the second call.

There is no really good answer to your question, except for you to rethink what you are trying to do. If you need some kind of authentication (via login), then that authentication needs to be part of every web service call.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top