猿问

在 Servlet 上使用 HashMap 进行用户验证

我必须创建一个小应用程序,用户在其中以 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 {


    }


HUH函数
浏览 185回答 1
1回答

catspeake

我想说你被卡住的原因是将用户名字符串映射到密码的 HashMap 需要一个HashMap<String, String> uc,但你已经创建了一个HashMap<Integer, User> uc,这消除了使用 HashMap 的价值!如果您打算利用 java.util.HashMap 提供的“优化”——这意味着字符串被“散列”以便在数据结构中快速轻松地查找,您可以按如下方式保存 HashMap:HashMap<Integer, User> uc = new HashMap <Integer, User>(); {&nbsp; &nbsp; uc.put("user1", "pass1");&nbsp; &nbsp; uc.put("user2", "pass2");&nbsp; &nbsp; uc.put("user3", "pass3");&nbsp; &nbsp; uc.put("user4", "pass4");}以这种方式,您可以按如下方式查找用户名和密码组合:public boolean checkPassword(String enteredUserName, String enteredPassword){&nbsp; &nbsp; String userPassword = uc.get(enteredUserName);&nbsp; &nbsp; return enteredPassword.equals(userPassword);}如果您绝对必须在 HashMap 中使用“用户”数据结构,另一种选择是:&nbsp; &nbsp; HashMap<Integer, User> uc = new HashMap <Integer, User>(); {&nbsp; &nbsp; &nbsp; &nbsp; uc.put("user1", new User("user1", "pass1"));&nbsp; &nbsp; &nbsp; &nbsp; uc.put("user2", new User ("user2", "pass2"));&nbsp; &nbsp; &nbsp; &nbsp; uc.put("user3", new User ("user3", "pass3"));&nbsp; &nbsp; &nbsp; &nbsp; uc.put("user4", new User ("user4", "pass4"));&nbsp; &nbsp; }此时获取密码可能如下所示:public boolean checkPassword(String enteredUserName, String enteredPassword){&nbsp; &nbsp; String userPassword = uc.get(enteredUserName).getPassword();&nbsp; &nbsp; return enteredPassword.equals(userPassword);}我不确定您用来启动的“整数”(用户 ID?)是什么HashMap<Integer, User>-但是- 要意识到的最重要的事情是 HashMap 的目的是在表中“查找”要快得多,因为对查找值执行“哈希”函数。如果您不打算使用“用户 ID 整数”进行查找,那么将其存储为 HashMap 的键根本没有多大用处。您的迭代器必须遍历整个表才能找到用户:public boolean checkPassword(String enteredUserName, String enteredPassord){&nbsp; &nbsp; &nbsp; &nbsp; Iterator<Entry<Integer, User>> it = uc.entrySet().iterator();&nbsp; &nbsp; &nbsp; &nbsp; while (it.hasNext()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //some code to validate user input against HashMap entries&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; User u = it.next().getValue();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (u.getUserName().equals(enteredUserName())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return u.getPassword().equals(enteredPassword);&nbsp; &nbsp; &nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答