| Author |
Jsp Login Page Problem
|
malik ge
Ranch Hand
Joined: May 13, 2011
Posts: 69
|
|
Hello.
I am trying to make a login page using jsp and servlet.
The user name and password is verified from database.
The problem is that the first userName and userPassword in database is verified correctly,
but when I try to enter the other userName and userPassword it did not login.
The code of index.jsp
ControllerServlet
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56163
|
|
You're doing it poorly and inefficiently.
What you are doing:
Fetching all users and comparing credentials.
What you should be doing:
Fetch the count of records where the username and password match the credentials. You should get 0 or 1.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16482
|
|
Bear Bibeault wrote:What you are doing:
Fetching all users and comparing credentials.
Actually malik is redirecting to "Good" or "Bad" based on the first user fetched, which is why the bad and inefficient method doesn't even do what it's supposed to do. (Only the first user in the table ever gets to log in.) But yes, just fixing that code isn't the thing to do. The query returning a simple yes or no is much better.
|
 |
malik ge
Ranch Hand
Joined: May 13, 2011
Posts: 69
|
|
Thanks for the reply.
I have corrected this problem by using this:
and it worked fine.
Now I want to know how to do it in an efficient manner, by using "mvc". So I don't have to write the code of connecting with the database again and again.
Thanks.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56163
|
|
Moving this code to the model would be a good step.
Also, if you'd like to be more efficient and don't need the row data (if all you care about is whether the record exists or not) select count(*) will be more performant than select *. But if you need the data, select * is fine.
|
 |
 |
|
|
subject: Jsp Login Page Problem
|
|
|