This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
I have been pulling my hair out for way too long on this one - 4 hours for something that's supposed to be "simple" Here goes-- I've written a login JSP page. It's supposed to say "Hi,Foobar" whenever the login name is _equal_ to the password but it only does this the first time I enter the page, not subsequent times. And it's supposed to just print "Hi, " below the form whenever they're not equal. It does this all the time though. What could be the problem? Here's the JSP with the form in it -- login.jsp -- <jsp:useBean id="abean" scope="session" class="LoginHandler" /> <jsp:setProperty name="abean" property="*" /> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body bgcolor="#FFFFFF" text="#000000"> <form name="logon" method="post"> <p>Login <input type="text" name="login"> </p> <p>Password <input type="password" name="pass"> </p> <p> <input type="submit" name="Submit" value="Submit"> </p> </form> <h1>Hello, <% if (abean.getAuthenticate()) out.println("Foobar"); %> </h1> </body> </html> Now here's the class 'LoginHandler': --LoginHandler.java-- public class LoginHandler { String login; String pass; boolean authenticate; public LoginHandler() { login = null; pass = null; } public void setLogin(String name ) { login = name; } public String getLogin() { return login; } public void setPassword(String password) { pass = password; } public String getPass() { return pass; } public boolean getAuthenticate() { return (login==pass); } }
Frank Carver
Sheriff
Joined: Jan 07, 1999
Posts: 6913
posted
0
"a m", The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please choose a new name which meets the requirements. Thanks.
Hello, There are two bugs in ur program . 1) u have set the property of password as setPass() and not setPassword() 2)U have to make use of equals method and not == in the authenticate method which returns the boolean value. Change these things i ur program and it will work fine.
------------------ Sandeep Jain
Try and Try Till u succeed<br /> <br />Sandeep Jain
a m
Greenhorn
Joined: Feb 25, 2001
Posts: 6
posted
0
Originally posted by Sandeep Jain: Hello, There are two bugs in ur program . 1) u have set the property of password as setPass() and not setPassword() 2)U have to make use of equals method and not == in the authenticate method which returns the boolean value. Change these things i ur program and it will work fine.
I changed these things but now at the line return (login.equals(pass)) It gives me a nullpointerexception?? why?
sandy gupta
Ranch Hand
Joined: Jan 30, 2001
Posts: 228
posted
0
Please go thru the code again and make the following changes.. change login==pass to login.equals(pass) and initialise the login and pass strings at the time of declaration... HTH Sahil
Adios
a m
Greenhorn
Joined: Feb 25, 2001
Posts: 6
posted
0
OK, I've initialized login and pass to a non-null value and it seems to work but I'm working to get a working login screen. I wanted to do it this way. Pls tell me if it's doable. In the JSP, before the login form, I would have : <% if (abean.getAuthenticate()) { // it's valid so redirect to some other page } else { out.println("Error.. Please reenter form data"); } %> The problem is -- say I initialize login and password to "" in the LoginHandler constructor, then the first time the user enters the page it will automatically redirect to some page. So I don't want the code to execute the first time, but all subsequent times but I don't see how this can be done. I would like this functionality with the least number of JSPs possible. Any suggestions? Thanks, Mohap.
a m
Greenhorn
Joined: Feb 25, 2001
Posts: 6
posted
0
OK, it works now that I've initialized the login/pass to "" in the construcotr. however I still haven't completed what I REALLY wanted to do I wanted to do it this way. Pls tell me if it's doable. In the JSP, before the login form, I would have : <% if (abean.getLogin()!="" && abean.getPass()!="") { if (abean.getAuthenticate()) { // it's valid so redirect to some page } else { out.println("Error.. Please reenter form data"); } } %> Almost everything works this way. When it first comes to the page I get the login/password prompt. When the two fields are not equaal the error message is shown. When the two fields are equal I get forwarded to the other file. The problem arises when the user clicks on submit with both fields empty, it still gets forwarded! why does this happen and how can i correct it? Thanks. Or if u have a better idea of doing his with a small number of JSPs then please let me know.
Frank Carver
Sheriff
Joined: Jan 07, 1999
Posts: 6913
posted
0
This is a basic java "gotcha". To compare objects (such as Strings) you should always use the "equals" method rather than the "==" operator.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.