Here's a possible design scenario:
Class: User Represents a user of the system. Has userId and possibly many other attributes
Class: DataAccessObject This is an abstract class. It's main feature is a getConnection() method which does a JNDI lookup on the data source and then uses it to get a
JDBC Connection object.
Class: UserDAO extends DataAccessObject
Has a getValidUser(
String userId, String password) method which throws InvalidUser and InvalidPassword exceptions and returns a User object. This method uses the superclass's getConnection method to get a JDBC connection. It then uses the connection to query the database to see if there is a user with the given userId and password. If there is, it returns the User object. If there isn't, it throws an exception.
Class: LoginForm extends ActionForm
The ActionForm used by the Login action. Has a userId and a password property that are populated by Struts
Class: LoginAction extends Action
In the execute() method, retrieve the userid and password from the LoginForm object passed in as a parameter. Instantiate a UserDAO object and call its getValidUser() method, passing the UserId and password as parameters. Do the above in a try/catch block and if exceptions are thrown, add new messages to the ActionMessages object and forward to the Login page. If no exceptions are thrown, put the User object in the HttpSession and forward to the welcome page.
In more complex interactions, you may want another layer between the Action class and the DAO known as a Business Delegate. For example, suppose your business has rules for how complex a password has to be. When a user wants to change a password, you would have a Business Delegate object such as RegistrationDelegate with a changePassword method. You would put the logic to check password complexity in this method, and then call the changePassword method on the UserDAO to persist the new password to the database.
If you don't know how to set up or use a connection pool, I'd suggest looking in the documentation for the Sun Application Server that you're using. It will tell you how to do it for that specific App Server.
This link may get you pointed in the right direction.
[ June 08, 2006: Message edited by: Merrill Higginson ]