如何比较地图内的元素与另一个元素?

时间:2015-01-05 12:06:19

标签: java jsp servlets

我正在尝试使用JSP表单和通过servlet显示HomePage的简单登录页面。我在hashmap中存储了几个用户名和密码。我想比较用户输入的用户名和密码与现有的用户名和密码如果用户名或密码错误,则使用hashmap并显示错误消息。如何实现此目的? TIA

的Login.jsp

  <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
  <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <title>Login</title>
  </head>
  <body>
  <center>
  <b>LOGIN PAGE</b><br>
  </center>
  <form name="login" method="post" action="Servlet1">
  <center>
  USER NAME: <input type="text" name="username"> 
  <br>
  <br>
  PASSWORD: <input type="password"> 
  <br>
  <br> 
  <input type="submit" value="LOGIN">
  </center>
  </form>
  </body>

Servlet1.java

  public class Servlet1 extends HttpServlet{

  public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException{

    String username;
    String password;
    String uname;
    String pwd;

    username = request.getParameter("username");
    password = request.getParameter("password");
    PrintWriter out = response.getWriter();

    Map<String,String>users = new HashMap<String,String>();
    users.put("Sheetal", "sss");
    users.put("Raj","rrr");

    }
    }

5 个答案:

答案 0 :(得分:3)

public boolean checkAccess(String username,String password){
   return password.equals(users.get(username));
}

答案 1 :(得分:2)

从地图中检索用户的密码,并根据提供的密码进行检查。

boolean valid = password.equals(users.get(username));

答案 2 :(得分:1)

import java.util.HashMap;
import java.util.Map;

class Main {

    public static void main(String args[]) {
            Map<String, String> credentials = new HashMap<String, String>();
            credentials.put("user", "correctpass");

            String inputUsername = "user";
            String inputPassword = "correctpass";

            String passOnMap = credentials.get(inputUsername);
            if (passOnMap != null
                    && passOnMap.equals(inputPassword)) {
                    // correct login
            } else {
                    // wrong user/passw
            }
    }
}

答案 3 :(得分:0)

public class Servlet1 extends HttpServlet{    

        final Map<String, String> users = new HashMap<>();

        @Override
        public void init() throws ServletException {
            // user name as key and it is unique for every user. password as value.
            users.put("user1", "pwd1"); 
            users.put("user2", "pwd2");
            users.put("user3", "pwd3");
            users.put("user4", "pwd4");
            users.put("user5", "pwd5");
        }


         @Override
            public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
            {
                String strUserName = request.getParameter("user1"); 
                String strPassword = request.getParameter("pwd1");

                String userpassword = users.get(strUserName);
                if (userpassword != null
                        && userpassword.equals(strPassword)) {
                        // login success
                } else {
                        // invalid user
                }

            }
      }

答案 4 :(得分:0)

public boolean checkAccess(Map<String, String> users, String username,String password) {
    boolean retunValue = Boolean.FALSE;
    if (users != null && users.size() > 0 && !username.isEmpty()
            && !password.isEmpty()) {
        if (users.get(username) != null) {
            if (password.equals(users.get(username)))
                retunValue = true;
        }
    }
    return retunValue;
}