ArrayList 在调用服务器之间清空
ArrayList empties in between calls to server
我正在 Java 中编写一个 XmlRcp 服务器,并尝试使用 ArrayList 跟踪有效令牌,但出于某种原因,在从客户端到服务器到 ArrayList 的调用之间是 recreated/emptied 并且什么都没有。
客户端首先连接到服务器,通过向服务器传递密码来请求令牌。如果密码正确,服务器 returns 向客户端发送一个令牌,并将该令牌添加到已验证令牌的 ArrayList 中。然后客户端发送另一个请求向服务器上的不同字符串 ArrayList 添加消息,但是无法验证客户端的令牌,因为现在保存令牌的 ArrayList 是空的。有谁知道为什么会这样?
下面是用于响应客户端调用的两种方法。
static MessageHandler mess = new MessageHandler(); //has a MessageHander, which is essential and ArrayList of strings that can add a message or get all
static TokenHandler th = new TokenHandler(); //private class that keeps a list of valid tokens to check against - located within this class
public String getAuthToken(String uid, String password) {
if(!password.equals("test123")){ //tests if the password is correct, if not it doesn't send a token.
System.out.println("User entered invalid password.");
return "You must enter a valid password to receive a token.";
}
System.out.println("uid: " + uid);
String token = uid + " 10011";
th.addToken(token);
System.out.println("User given token: " + token);
return token;
}
public String storeMessage(String token, String Message) {
//validate token
if(th.validateToken(token)){ //if valid token, then store the message
System.out.println("msg=" + Message);
mess.addMessage(Message);
return "Message succesfully stored.";
} else { //if the token passed was not valid, then don't store the message.
return "Invalid token - message could not be stored.";
}
}
每次有新请求时都会重新创建 TokenHandler
实例 (google "request scope vs session scope")。为了在调用之间保持状态(保存数据),您应该将其保存到数据库中并添加恢复它的逻辑。
另一种方法是让客户端保存数据。饼干。
我正在 Java 中编写一个 XmlRcp 服务器,并尝试使用 ArrayList 跟踪有效令牌,但出于某种原因,在从客户端到服务器到 ArrayList 的调用之间是 recreated/emptied 并且什么都没有。
客户端首先连接到服务器,通过向服务器传递密码来请求令牌。如果密码正确,服务器 returns 向客户端发送一个令牌,并将该令牌添加到已验证令牌的 ArrayList 中。然后客户端发送另一个请求向服务器上的不同字符串 ArrayList 添加消息,但是无法验证客户端的令牌,因为现在保存令牌的 ArrayList 是空的。有谁知道为什么会这样?
下面是用于响应客户端调用的两种方法。
static MessageHandler mess = new MessageHandler(); //has a MessageHander, which is essential and ArrayList of strings that can add a message or get all
static TokenHandler th = new TokenHandler(); //private class that keeps a list of valid tokens to check against - located within this class
public String getAuthToken(String uid, String password) {
if(!password.equals("test123")){ //tests if the password is correct, if not it doesn't send a token.
System.out.println("User entered invalid password.");
return "You must enter a valid password to receive a token.";
}
System.out.println("uid: " + uid);
String token = uid + " 10011";
th.addToken(token);
System.out.println("User given token: " + token);
return token;
}
public String storeMessage(String token, String Message) {
//validate token
if(th.validateToken(token)){ //if valid token, then store the message
System.out.println("msg=" + Message);
mess.addMessage(Message);
return "Message succesfully stored.";
} else { //if the token passed was not valid, then don't store the message.
return "Invalid token - message could not be stored.";
}
}
每次有新请求时都会重新创建 TokenHandler
实例 (google "request scope vs session scope")。为了在调用之间保持状态(保存数据),您应该将其保存到数据库中并添加恢复它的逻辑。
另一种方法是让客户端保存数据。饼干。