wood burning stoves 2.0*
The moose likes Servlets and the fly likes Returning an object or null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Returning an object or null" Watch "Returning an object or null" New topic
Author

Returning an object or null

Michael Cleary
Ranch Hand

Joined: Jul 29, 2003
Posts: 93
I'm taking a class in J2EE programming, and we are writing a servlet which reads in login information from a user (from an HTML form), and then is supposed to authenticate the user. The authentication is accomplished via a method in a separate "AccountDelegate" class. There is also a UserAccount class which creates user account objects. We are not connecting to a database, but just have several hardcoded user accounts stored in the AccountDelegate class (UserAccount objects). If the login information matches one of the hard coded user accounts we are supposed to return a UserAccount object. If it doesn't match we are supposed to return null (which indicates login failure). This is the part that is giving me problems. In the servlet, I call the authentication method as follows:
m_user = new UserAccount(AccountDelegate.authenticateUser(strUsername, hashPassword));
This works fine if the method authenticateUser returns a UserAccount object. The UserAccount class has a constructor which takes another UserAccount object. However, if authenticateUser returns null (i.e. because I have passed it an invalid username and/or password), it fails.
How should I call this method from the servlet such that I can deal with it returning either an object or null?
Thanks,
Mike


Mike<br />SCJP 1.4<br />----------------------------<br />mdcleary@earthlink.net<br />----------------------------<br />There are 10 types of people<br />in the world. Those that <br />understand binary, and those<br />that don't.
Phani Paladugu
Greenhorn

Joined: Jan 25, 2004
Posts: 2
Why can't you do the following

UserAccount ua = AccountDelegate.authenticateUser
(strUsername,hashPassword));
if(ua != null)
m_user = new UserAccount(ua);
else
m_user = null;
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
An alternative would be to have the UserAccount constructor throw an exception if it gets a null value or otherwise unsuitable input. I like to use IllegalArguementException for this purpose - naturally you should provide for catching the exception and returning null.
Bill
Michael Cleary
Ranch Hand

Joined: Jul 29, 2003
Posts: 93
Thanks! I will play around w/ these ideas for a bit - in between shoveling snow/ice! I need to get this part done so I can move on to the rest of the project (behind schedule as usual....)
Thanks again,
Mike
Michael Cleary
Ranch Hand

Joined: Jul 29, 2003
Posts: 93
In reference to the first response, how should "ua" be initialized?
Using this method, the result is always an invalid user. It appears that even if a valid UserAccount object is returned, ua still turns out to be null.
For example, as a check I inserted the line: String name = ua.getID() after the call to the authentication method. Now, even if I input a valid username and password, this line always generates a NullPointerException. So apparently, ua is not getting assigned the attributes of the returned object.
My UserAccount constructors are as follows:
public UserAccount()
{
}

public UserAccount(String userID, String userPassword, String userRole)
{
setID(userID);
setPassword(userPassword);
setRole(userRole);
}

public UserAccount(UserAccount account)
{
this(account.m_strID, account.m_strPassword, account.m_strRole);
}
Thanks,
Mike
Phani Paladugu
Greenhorn

Joined: Jan 25, 2004
Posts: 2
Then I guess you must be doing something incorrectly in the
AccountDelegate.authenticateUser(strUsername,hashPassword));
If you can send the code for AccountDelegate.authenticateUser method
this can be clarified.
Also the solution proposed by bill is a good a way resolving this.
Michael Cleary
Ranch Hand

Joined: Jul 29, 2003
Posts: 93
Here it is:
public static UserAccount authenticateUser(String id, String password) throws BusinessException
{
int count = 0;
for(int i = 0; i < 5; i++)
{
if(m_users[i].getID().equals(id))
{
m_currentUser = new UserAccount(m_users[i]);
break;
}
count++;
}//for
if(count < 5)
{
if(m_currentUser.getPassword().equals(password))
{
return m_currentUser;
}
else
{
return null;
}

else
{
return null;
}

}//end authenticateUser
Actually, I got it to work by adding another catch block in the servlet code:
try
{
m_user = new UserAccount(AccountDelegate.authenticateUser(strUsername, hashPassword));
}
catch(BusinessException badUser)
{
}
catch(NullPointerException badValue)
{
m_user = null;
}
The "BusinessException" is our own exception class which, quite frankly doesn't appear to do anything....
Thanks,
Mike
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
But, I think the BusinessException is along the lines of what Bill was referring to, For example in your

whenever you hava an invalid user i.e. returning a null value, you can throw a BussinessException("Invalid User") or something along those lines.
That way you want have to define another catch block such as NullPointerException. The BusinessException fits along the lines of what you are trying to accomplish. It is an exception created just for the application(authenticateUser) use, it is an anticipated error and a recoverable error.
I hope this makes sense.
Craig.
Michael Cleary
Ranch Hand

Joined: Jul 29, 2003
Posts: 93
So if I get an invalid password (or ID) in authenticateUser, I might do something like:
if(m_currentUser.getPassword().equals(password))
{
return m_currentUser;
}
else
{
throw new BusinessException("my message");
return null;
}
Or did I misunderstand?
Thanks,
Mike
Michael Cleary
Ranch Hand

Joined: Jul 29, 2003
Posts: 93
Thanks CJ! I did the following and it works like a charm!
In the method authenticateUser:
if(m_currentUser.getPassword().equals(password))
{
return m_currentUser;
}
else
{
throw new BusinessException("Invalid User Password");
}
I did the same for an invalid user ID.
Thanks again!
Mike
 
wood burning stoves
 
subject: Returning an object or null
 
Similar Threads
Authentication and Authorization Problems with IIS 6 and Jrun 4
NEWBIE: from a servlet, how do i access a class reference?
JAAS client problem in mastering EJB
Integration Question
SSO Auto create user problem