Hi Folks,

I finally have done developing a JSP query application. I need ur experts
advice on the following code I have written that hopefully I havent written
anything illegal(in terms of designing this thing) or if it is a poor/good
way of doing things in JSP environment.

Greatly appreciate any help..thanx in advance


Heres the code for my LoginBean(in the jsp page the scope of this bean is
application):

import javax.servlet.http.*;
import java.util.*;

public class LoginBean implements HttpSessionBindingListener{

        String username;
        String password;
        String dbInstance;
        String status = new String("Not Connected");

        static Hashtable h;

        /****** Bean Properties ******/
        set and get methods for username, pwd, dbInstance
        /****** End: Bean Properties ******/

        // param:  "session" Object from JSP Page
        public void processSession(HttpSession session) {

                String sessionID;
                DBManager dbm;
                //DBManager is a bean that manages all my database related
tasks
                // one per each user session

                if (h==null){
                        h = new Hashtable();
                }
                sessionID = session.getId();

                if (h.containsKey(sessionID)) {
                        System.out.println("Session Already
Created:"+sessionID);
                }
                else{
                        dbm = new DBManager();
                        //returns connected if successful else the exception
message
                        status = dbm.connect(username,password,dbInstance);

                        if (status.equalsIgnoreCase("Connected")){
                                h.put(sessionID,dbm);
                                System.out.println("Added:" + sessionID);
                                //I did this because I wanted the LoginBean
to be notified when the session
                                //gets invalidated
                                //Should I have created a seperate object
which implemented
                                //HttpSessionBindingListener and then do the
cleanup there
                                //instead of puttin my LoginBean in
session??????
                                session.putValue("login",this); // IS THIS
GOOD APPROACH???

                                session.putValue("loginID",username);
                        }
                        else
                         //display the exception
                }

        }

        public String getStatus(){
                return status;
        }

        public DBManager getDBManager(String sessionID){
                        return (DBManager)h.get(sessionID); //will put code
to handle if sessionID is not in the Hashtable
        }
        /*
        valueUnbound: Gets Invoked when the LoginBean is removed from the
session
                                        or the session gets invalidated
                                        Closes Database Connection
                                        and Removes the SessionID from the
Hashtable
        */

        public void valueUnbound(HttpSessionBindingEvent event){
                DBManager dbm;
                System.out.println("I just got UN-Bounded for
SessionID"+event.getSession().getId());
                //will put code to handle if sessionID is not in the
Hashtable
                dbm = getDBManager(event.getSession().getId()); //gets the
DB Connection for the particular session getting invalidated
                dbm.close();
                dbm=null;
                h.remove(event.getSession().getId());

        }
        protected void finalize() throws Throwable{
                        System.out.println("In Finalize of
LoginBean..."+this.toString());

        }
}

Later in my application I use the implicit "application" object  to get the
login bean and from login bean I get the DBManager Instance(for sessionID)
and do some tasks like perform a query to database and display result etc.
Is this whole approach a good idea  or a very bad one???? Need your expert
advice!!


Asim Imam
Application Developer
@AT&T Tax Systems Group

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
FAQs on JSP can be found at:
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html

Reply via email to