Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Invalid user error message

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written a jsp page which compares the username and password entered in a form with the username and password stored in the database. This works fine when the correct information is entered but when the data is incorrect no error message is displayed, the page is simply blank.
Here is the code :
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
if( (username!=null) && (password!=null) )
{
String findUser = "SELECT Username, Password FROM User WHERE username LIKE '"+ username +"' AND password LIKE '"+ password +"'";
String dsn = "mcaiysm3";
String dbURL = "jdbc dbc:" + dsn;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(dbURL,"","");

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(findUser);

while(rs.next()) {
String dbUser = rs.getString("username");
String dbPassword = rs.getString("password");
if( (dbUser.equalsIgnoreCase(request.getParameter("username"))) && (dbPassword.equalsIgnoreCase(request.getParameter("password"))) )
{ %> <input type="hidden" name="username" value="<%=dbUser%>">
<jsp:forward page="Welcome.jsp"/>
<%
}
else if (rs==null) {
out.print("Invalid login details");
%> <jsp:forward page="Login.jsp"/> <%
}
rs.close();
stmt.close();
con.close();
}
}
%>
I have tried using a try-catch, if-else to get an error message but nothing seems to work. Can anyone please help?
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
The problem here is, the loop 'while(rs.next())' executes only when there are records in the database which match the inputs.So the 'if' loop is working fine when correct data is entered.So when there are no records,control do not go to while loop and so whatever you are trying to print in 'else' loop is not executed.
So something like this,
while(rs.next()) {
String dbUser = rs.getString("username");
String dbPassword = rs.getString("password");
if( (dbUser.equalsIgnoreCase(request.getParameter("username"))) && (dbPassword.equalsIgnoreCase(request.getParameter("password"))) )
{ %> <input type="hidden" name="username" value="<%=dbUser%>">
<jsp:forward page="Welcome.jsp"/>
<%
}
}
if (rs==null) {
out.print("Invalid login details");
%> <jsp:forward page="Login.jsp"/> <%
}
rs.close();
stmt.close();
con.close();
Jyothsna
 
shuzo monsoon
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help but I've tried making those chnages but it still doesn't lead me back to the login page. Any other ideas?
 
shuzo monsoon
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's ok, I've managed to get it working, basically I realised that I was saying if the resultset was empty, then go to the error page, but this was wrong as details were always entere, they were just incorrect. So I had to use if (rs!=null). Thanks anyway.
 
Can you smell this for me? I think this tiny ad smells like blueberry pie!
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic