Forums Register Login

Returning an object or null

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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;
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
Paddy spent all of his days in the O'Furniture back yard with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1250 times.
Similar Threads
SSO Auto create user problem
JAAS client problem in mastering EJB
Authentication and Authorization Problems with IIS 6 and Jrun 4
NEWBIE: from a servlet, how do i access a class reference?
Integration Question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 00:32:13.