0

EDIT: The problem was two-fold, first dictionary should be static and also i was using .contains() where i should have used .containsKey()

I'm trying to do a simple java client and server set up, this is what i have got and i can't seem to find anything wrong with the way i have done it but whenever i run the code i get the output:

Result = Added

Result = This word is not in the dictionary, please use the add function.

Which tells me that the server isn't storing the change made the the map when i am adding a word, is there something really simple i am missing here?

I can add anymore information needed if asked.

This is my client code:

public class Client {
 @WebServiceRef(wsdlLocation = 
        "http://localhost:8080/P1Server/ServerService?wsdl")

public static void main(String[] args) { 
try { 
    package1.ServerService service = new package1.ServerService(); 
    package1.Server port = service.getServerPort(); 

    String result = port.addWord("Test", "This is a test."); 
    System.out.println("Result = " + result); 

    result = port.getDefiniton("Test");
    System.out.println("Result = " + result); 
}catch(Exception ex)
{ 
    System.out.println("Gone Wrong"); 
}

This is my relevant server code:

@WebService
public class Server {

private **static**ConcurrentHashMap<String,String> dictionary;    

public Server() {
    this.dictionary = new ConcurrentHashMap<>();
}

@WebMethod
public String addWord(String word, String definition){
    if(dictionary.contains(word.toLowerCase())){
        return "This word is already in the dictionary, "
                + "please use the update function.";
    }else{
        dictionary.put(word.toLowerCase(), definition);
        return "Added";
    }
}
@WebMethod
public String getDefiniton(String word){
    if(dictionary.contains(word.toLowerCase())){
        return dictionary.get(word);

    }else{
        return "This word is not in the dictionary, "
                + "please use the add function.";
    }
}

3 Answers3

0

Web services are stateless by nature. Every web request will get its own contexts and instances. So, the instance of Server that served the port.addWord() request can be different than the one that served port.getDefinition(). In that case, the dictionary map that had the result placed into it is different than the one used to retrieve the results.

In order for this to work, the data needs to be persisted somehow on the Server side. This can be done via a database. Or, if you're just doing this for testing purposes, change the definition of dictionary to be static so that all instances of Server share the same map.

Ali Cheaito
  • 3,746
  • 3
  • 25
  • 30
  • Web services are stateless by nature? who says thats always true ? there are stateful services as well. http://stackoverflow.com/questions/94660/stateful-web-services – Mukul Goel Mar 11 '15 at 16:46
  • I have changed dictionary to static but i still have the same problem, NetBeans now also gives a warning saying the dictionary variable can be final – user4301818 Mar 11 '15 at 16:50
  • @MukulGoel Web services in general are stateless. I'm sure you can find some statefull web services, but every one I've worked with is stateless and there are good reasons for that, primarily that the HTTP protocol in of itself is stateless (read the answer to the question you linked to). – Ali Cheaito Mar 11 '15 at 16:51
  • @user4301818 See jitsonfire answer above. There is a flaw in your logic (use word.toLowerCase() to retrieve from the dictionary). You still need the map to be static though. – Ali Cheaito Mar 11 '15 at 16:55
  • @AliCheaito: You are totally right. But still web services are not stateless. They can be stateless or stateful but yes stateless are more common. More common to the extent that you would rarely come across a stateful. – Mukul Goel Mar 11 '15 at 17:07
0

Define dictionary as static variable. So that every instance of Web service instances that get created in the server side will use same dictionary to put/get data.

private static ConcurrentHashMap<String,String> dictionary;
Don Srinath
  • 1,565
  • 1
  • 21
  • 32
0

Your problem has noting to with webservice. Issue is with you logic

Modify your methods as follows :

public String addWord(String word, String definition) {
        if (dictionary.containsKey(word.toLowerCase())) {
            return "This word is already in the dictionary, "
                    + "please use the update function.";
        } else {
            dictionary.put(word.toLowerCase(), definition);
            return "Added";
        }
    }

    public String getDefiniton(String word) {
        if (dictionary.containsKey(word.toLowerCase())) {
            return dictionary.get(word.toLowerCase());

        } else {
            return "This word is not in the dictionary, "
                    + "please use the add function.";
        }
    }

It will work. Hope this helps.

jithin iyyani
  • 751
  • 1
  • 7
  • 19