【发布时间】:2015-03-11 16:29:50
【问题描述】:
编辑:问题是双重的,第一个字典应该是静态的,而且我使用 .contains() 我应该使用 .containsKey()
我正在尝试设置一个简单的 java 客户端和服务器,这就是我所拥有的,我似乎无法发现我这样做的方式有什么问题,但是每当我运行代码时,我都会得到输出:
Result = Added
Result = This word is not in the dictionary, please use the add function.
这告诉我,当我添加一个单词时,服务器没有存储对地图所做的更改,我在这里缺少一些非常简单的东西吗?
如果需要,我可以添加更多信息。
这是我的客户端代码:
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");
}
这是我的相关服务器代码:
@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.";
}
}
【问题讨论】:
标签: java server concurrenthashmap