• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Invalid user error message

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic