• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Jsp Login Page Problem

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
  •  
    Marshal
    Posts: 26912
    82
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 69
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Sheriff
    Posts: 67595
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic