Reputation: 199
I have created a simple login application using JSP form and displaying HomePage through a Servlet.I have also created a HashMap inside servlet which stores a couple of username and passwords.When a user enters username and password it is compared with those present inside the map and appropriate message is displayed.But the problem here is the HashMap is initialized after user hits the Login button i.e,when servlet program starts up.I want to initialize the map before hand and when ever user logs in the comparison with map should take place,instead of initialising map after Logging in.Kindly help me how I can go about it. Here is what I have done so far
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="UserAuthentication">
<center>
USER NAME: <input type="text" name="username">
<br>
<br>
PASSWORD: <input type="password" name="password">
<br>
<br>
<input type="submit" value="LOGIN">
</center>
</form>
</body>
</html>
UserAuthentication.java
public class UserAuthentication extends HttpServlet{
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException{
String username = request.getParameter("username");
String password = request.getParameter("password");
PrintWriter out = response.getWriter();
Map<String,String>users = new HashMap<String,String>();
users.put("Sheetal", "sss");
users.put("Raj","rrr");
users.put("Anjali", "aaa");
users.put("Bhavya","bbb");
if(users.containsKey(username) && users.containsValue(password))
{
if(password.equals(users.get(username)))
{
out.println("<html>"+
"<body>"+
"Login Successful" + "<br>" +
"Welcome " + username + "<br>" +
"</body>" +
"</html>");
}
}
else
{
out.println("<html>"+
"<body>"+
"Login Unsuccessful" + "<br>" +
"Try again" + "<br>" +
"</body>" +
"</html>");
}
}
}
TIA
Upvotes: 0
Views: 2878
Reputation: 1925
The best way in your case is to create a singleton class called users, which would be like below.
public class Users{
private static Map<String,String> users = new HashMap<>();
private users(){
users.put("Sheetal", "sss");
users.put("Raj", "rrr");
users.put("Anjali", "aaa");
users.put("Bhavya", "bbb");
}
public static Users singleInstance = new Users();
public boolean validate(String userName,String password)
{
boolean result = false;
if(condition)
result = true;
return result;
}
and in your UserAuthentication.java when you want to do the validation,
if(Users.singleInstance.validate(userName,password))
Upvotes: 0
Reputation: 16041
Make the hashmap a static in the class (I also assumed its final):
private static final Map<String,String> users = new HashMap<>();
And populate it in the static initializer block:
static {
users.put("Sheetal", "sss");
users.put("Raj", "rrr");
users.put("Anjali", "aaa");
users.put("Bhavya", "bbb");
}
This way, the HashMap
will be populated, when the class is registered.
If you did not know about initializers, please read this article.
Upvotes: 2