我必须创建一个小应用程序,用户在其中以 html 形式输入他/她的用户名和密码,然后在具有用户和密码的 HashMap 的 servlet 中进行处理。HashMap 应该在 servlet 的 init 方法中使用一些用户名和密码进行初始化。我必须使用 HashMap 条目检查用户输入,这就是我卡住的地方。我不知道如何将 HashMap 条目与用户输入的用户名和密码进行比较。我到目前为止的代码如下:
public class User {
String username;
String password;
public User (String user, String pass){
this.username = user;
this.password = pass;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/AthorizationServlet")
public class AthorizationServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public AthorizationServlet() {
super();
}
public void init(ServletConfig config) throws ServletException {
super.init(config);
HashMap<Integer, User> uc = new HashMap <Integer, User>(); {
uc.put(1, new User("user1", "pass1"));
uc.put(2, new User ("user2", "pass2"));
uc.put(3, new User ("user3", "pass3"));
uc.put(4, new User ("user4", "pass4"));
}
Iterator<Entry<Integer, User>> it = uc.entrySet().iterator();
while (it.hasNext()){
//some code to validate user input against HashMap entries
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
catspeake
相关分类