• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

JSP Password checking problem

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm currently doing a JSP Project for school, one of my pages is a form where users can log in, their usernames & passwords are stored in a DB.
Here's the code for my code for the form:
[LogIn.jsp]
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<HTML>
<HEAD><TITLE>Log Gebruiker in</TITLE>
<link rel="stylesheet" href="gip.css" type="text/css">
</HEAD>
<BODY>
<P>
<P> 
<P> 
<P><img src="images/wsbeheer.gif" width="101" height="11"><br>
<br>
<a href="toevoegen.jsp">Produkten toevoegen</a>
<FORM name="form1" method="post" action="LogInVerwerking.jsp">
<TABLE border=1 cellpadding=0 cellspacing=0>
<TR>
<TD>Gebruikersnaam: </TD>
<TD>
<INPUT type="text" name="GebruikerIn" maxlength="10">
</TD>
</TR>
<BR>
<TR>
<TD>Wachtwoord: </TD>
<TD>
<INPUT type="password" name="WachtwoordIn" maxlength="20">
</TD>
</TR>
<BR>
</TABLE>
<INPUT type="submit" name="Submit" value="Submit">
</FORM>
<P> 
</BODY>
</HTML>
The code that handles the form:
[LogInVerwerking.jsp]
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%
Connection con;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc dbc:Gebruikers";
con = DriverManager.getConnection(url);
Statement stmt = con.createStatement();
String user;
String password;

gebruiker=request.getParameter("GebruikerIn");
wachtwoord=request.getParameter("WachtwoordIn");


ResultSet rs = stmt.executeQuery("SELECT * FROM tblGebruikers WHERE USERNAME='"+user+"' AND PASSWORD='"+password+"'");

while(rs.next()){
String dbUser = rs.getString("USERNAME");
String dbPassword= rs.getString("PASSWORD");

boolean entrance;
entrance=false;


if ((user.equals(dbUser)) && (password.equals(dbPassword))){
entrance=true;
}
else{
entrance=false;
}



if (entrance==true){%>
<jsp:forward page="Entrance.jsp"/>
<%}
else{%>
<jsp:forward page="NoEntrance.jsp"/>
<%}
}
%>
Now, the problem is here;
When I put in the correct login & password it forwards me to the 'entrace.jsp' page, but when it's incorrect, instead of redirecting me to the other page it gives me a blank page.. When i switch the first 'entrance==true' to 'false' it redirects me to the 'NoEntrance.jsp' page.. So it doesn't even check if 'entrance' could be false.. (i think)
Your help is greatly appreciated,
Michael
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael
the problem is that you set and test your flag within the while loop and you only enter the while loop if there is a match to the user and password. Pull the entrance delcaration outside of the loop and then test it after the loop:
[LogInVerwerking.jsp]

Or, dont even use the flag and just use the forward tag inside the loop. The select statement already checks for a match so another test is redundant:

hope that helps
[ May 28, 2002: Message edited by: Dave Vick ]
 
Michael Michna
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,
Actually I tried that earlier too, but it didn't work.. saying that 'entrance' was not a known variable or something, but now i declared it outside the loop and together with what you pointed out it works fine
Thanks
 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic