首页 > 解决方案 > User validation with HashMap on Servlet

问题描述

I have to create a small application where a user enters his/her username and password in an html form and then it is processed in a servlet which has a HashMap of users and passwords. The HashMap should be initialized with some usernames and passwords in the init method of the servlet. I have to check the user input with the HashMap entries and this is where I am stuck. I do not know how to compare the HashMap entries with the username and password entered by the user. The code I have so far is as below:

    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 {

    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String user = request.getParameter("username");
        String pass = request.getParameter("password"); 


    }

}

标签: javahtmlservletshashmapuser-input

解决方案


我想说您被卡住的原因是,将用户名字符串映射到密码的 HashMap 需要 a HashMap<String, String> uc,但是您已经创建了 a HashMap<Integer, User> uc,而这消除了使用 HashMap 的价值!如果您打算利用 java.util.HashMap 提供的“优化” - 这意味着 String 被“散列”以便在数据结构中快速轻松地查找,您可以按如下方式保存 HashMap:

HashMap<Integer, User> uc = new HashMap <Integer, User>(); {
    uc.put("user1", "pass1");
    uc.put("user2", "pass2");
    uc.put("user3", "pass3");
    uc.put("user4", "pass4");
}

以这种方式,您可以按如下方式查找用户名和密码组合:

public boolean checkPassword(String enteredUserName, String enteredPassword)
{
    String userPassword = uc.get(enteredUserName);
    return enteredPassword.equals(userPassword);
}

如果您绝对必须在 HashMap 中使用“用户”数据结构,另一种选择是:

    HashMap<Integer, User> uc = new HashMap <Integer, User>(); {
        uc.put("user1", new User("user1", "pass1"));
        uc.put("user2", new User ("user2", "pass2"));
        uc.put("user3", new User ("user3", "pass3"));
        uc.put("user4", new User ("user4", "pass4"));
    }

此时获取密码可能如下所示:

public boolean checkPassword(String enteredUserName, String enteredPassword)
{
    String userPassword = uc.get(enteredUserName).getPassword();
    return enteredPassword.equals(userPassword);
}

我不确定您用来启动的“整数”(用户 ID?)是什么HashMap<Integer, User>-但是- 最重要的是要意识到 HashMap 的目的是在表中“查找”要快得多,因为对查找值执行“哈希”函数。

如果您不打算使用“用户 ID 整数”进行查找,那么将其存储为 HashMap 的键根本没有多大用处。

您的迭代器必须遍历整个表才能找到用户:

public boolean checkPassword(String enteredUserName, String enteredPassord)
{
        Iterator<Entry<Integer, User>> it = uc.entrySet().iterator();
        while (it.hasNext()){
            //some code to validate user input against HashMap entries
            User u = it.next().getValue();
            if (u.getUserName().equals(enteredUserName())
                return u.getPassword().equals(enteredPassword);
        }

}

推荐阅读