Two Laptop Bag*
The moose likes JSP and the fly likes Invalid user error message Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Invalid user error message" Watch "Invalid user error message" New topic
Author

Invalid user error message

shuzo monsoon
Ranch Hand

Joined: Feb 11, 2004
Posts: 54
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?
jyothsna kumari
Ranch Hand

Joined: Jul 21, 2003
Posts: 108
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

Joined: Feb 11, 2004
Posts: 54
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

Joined: Feb 11, 2004
Posts: 54
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Invalid user error message