Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

simple java question

Options
  • 17-04-2008 8:31am
    #1
    Registered Users Posts: 18,272 ✭✭✭✭


    there is probably something really stupid I over looked here but with family issues at home and 6 assignments to hand in , I just cant concentrate on this.

    I'm using JDeveloper to build a website.

    I have a LoginSessionBean that holds a id for a session of the website, I have another bean that requires the id from the LoginSessionBean to use in a method, I was wondering whats the correct way to get the id from one bean to the other?

    thanks (sorry for it being a noob question) :o


Comments

  • Registered Users Posts: 1,821 ✭✭✭Skud


    i found this... might help : http://forum.java.sun.com/thread.jspa?threadID=740653&messageID=4248006

    if not post some code


  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    ok I tried to work the code from the sun site into my site but it wont work, this code is for my first bean, it lets a user log on and gets there username, password and dealer(which is an integer) from the database
    package view.beans;
    
    import java.io.FileInputStream;
    
    import java.io.IOException;
    
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Properties;
    import javax.faces.context.FacesContext;
    Import javax.servlet.http.HttpServletRequest;
    mport sd3004project.model.business.Users;
    import sd3004project.model.dao.PrimaryKeyGenerator;
    import sd3004project.model.dao.UserDao;
    
    import sd3004project.model.exceptions.DaoException;
    
    import view.utils.ScopeUtility;
    
    public class LoginSessionBean  {
        
        private String username;
        private String password;
        private int dealer;
       
            UserDao dao = null ;
            
        public LoginSessionBean() throws DaoException {
            dao = new UserDao() ;
            
        }
    
        public void setUserName(String userName) {
            this.username = userName;
        }
    
        public String getUserName() {
            return username;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getPassword() {
            return password;
        }
        
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getUsername() {
            return username;
        }
    
    
        public String verifyLoginButton() {
            Users user = null ;
            try
            {
                user = dao.findUserByCodeNamePassword(username, password) ;
            }
            catch(DaoException e) {
                HttpServletRequest request = ScopeUtility.getRequest() ;
                request.setAttribute("error", "Verify login " + e.getMessage() );
                return "error" ;
            }
            if (user == null) {
                userLoggedIn = false ;
                adminLoggedIn = false ;
                loginAttempt = true ;
                return null ;   // pack to same page
            }
            else
            {
                
                username = user.getUsername();
                password = user.getPassword();
                dealer = user.getDealer();
                
             
                if (dealer > 0)
                    adminLoggedIn = true ;
                else
                    userLoggedIn = true ;
            
                userId = user.getUserid() ;
                
                return "success" ;
            }
        }
    

    This is the second bean I want the dealer variable/attribue from the first bean to be able to be used in the second bean in the method findAllCarsByDealer(dealer);
    package view.beans;
    
    import java.util.ArrayList;
    import java.util.List;
    
    
    
    import sd3004project.model.business.Car;
    import sd3004project.model.dao.CarsDao;
    
    import sd3004project.model.exceptions.DaoException;
    
    import view.utils.ScopeUtility;
    
    public class AmendCar{
        private static List<Car> cars = null ;
        CarsDao dao = null ;
        int dealer = 0;
        
        
    
        public AmendCar() throws DaoException {
            dao = new CarsDao() ;
        }
    
        public void setCars(List<Car> cars) {
            this.cars = cars;
        }
    
        public List<Car> getCars() throws DaoException {
            
            if (cars == null)
                cars = dao.findAllCarsByDealer(dealer) ; 
                  
            return cars;/*
             if (cars == null)
                 cars = dao.findAllCarsByDealer(10) ; 
                   
             return cars;*/
        }
    

    so when a user logs on there dealer (this should be called dealerNumber) will be pulled from the database and saved for the session, so dealer 1 is logged on, I wanted that dealer number 1 to be able to be inserted into the method findAllCarsByDealer(dealer);


  • Moderators, Science, Health & Environment Moderators Posts: 10,079 Mod ✭✭✭✭marco_polo


    Where do you create the beans is it in a JSPpage?

    The easiest way would be to exposes a getDealer() method in the first class and add a second constructor in the Amend car class. as follows
    public class LoginSessionBean  {
    
        .......
    
        public String getDealerID() {
                return dealerID;
        }
    
    }
    
    
    public class AmendCar{
    
    ......
    
        public AmendCar(int dealer) throws DaoException {
            this.dealer=dealer;
            dao = new CarsDao() ;
        }
    }
    
    Use it in your code that calls the beans as follows
        LoginSessionBean lsb = new LoginSessionBean();
        lsb.setUsername();
        lsb.serPassword();
        lsb.verifyLoginButton();
    
        AmendCar=new AmendCar(lsb.getDealerID());
    


  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    The two beans are just java classes out on there own,

    Then I use the variables in the JSP's

    for example i use the cars list to display the cars in a data table
    <h:dataTable value="#{AmendCar.cars}" var="c" 
                                         styleClass="news_text" width="200">
    
    
    ............
    
    </h:dataTable>
    
    


    I'm not sure how I would implement your code, would I put it directly into the JSP?


  • Moderators, Science, Health & Environment Moderators Posts: 10,079 Mod ✭✭✭✭marco_polo


    Not programmed with JSPs for a while so I forget how to use alot of the bean tags etc, but for a quick and dirty hack you could throw some code in a scriptlet tag near the top of the page.

    Its not pretty but it is probably the quickest way since time is not your friend :)
    <&#37;
      LoginSessionBean lsb = new LoginSessionBean();
        lsb.setUsername();
        lsb.serPassword();
        lsb.verifyLoginButton();
    
        AmendCar=new AmendCar(lsb.getDealerID());
    %>
    


  • Advertisement
  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    thanks, I ended up using FacesContext to get the beans to pass data, I'll post the code up later


Advertisement