Reputation: 199
I am trying a simple login page using JSP form and Displaying HomePage through a servlet.I am storing a couple of username and passwords inside a hashmap.I would like to compare the username and password entered by the user with those existing inside the hashmap and display an error message if username or password is wrong.How can I achieve this? 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");
}
}
Upvotes: 2
Views: 192
Reputation: 37
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;
}
Upvotes: 0
Reputation: 225
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
}
}
}
Upvotes: 0
Reputation: 2542
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
}
}
}
Upvotes: 1
Reputation: 1354
public boolean checkAccess(String username,String password){
return password.equals(users.get(username));
}
Upvotes: 3
Reputation: 5569
Retrieve the user's password from the map and check it against the supplied password.
boolean valid = password.equals(users.get(username));
Upvotes: 2