Using global object to save user information

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Yene
    New Member
    • Oct 2009
    • 13

    Using global object to save user information

    Am trying to use my user object to hold one value after the user has logged in my website. when loging in the user object id is set using user.setid("id" );

    Once this is done. I want to use user.getid(); in a differernt method to update user record.

    I cant get the user object to hold the same value between the two methods

    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package user;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Random;
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.WebService;
    
    /**
     *
     * @author Yeneneh Mulatu
     */
    @WebService()
    public class userWebService {
    [B][U]private User user= new User ();[/U][/B]
    private Random pwd = new Random();
    private ResultSet rs;
    private conn mydb = new conn("root","");
     
        /**
         * Web service operation
         */
        @WebMethod(operationName = "login")
        public String login(@WebParam(name = "email")
        String email, @WebParam(name = "password")
        String password) {
            String login=null;
            try{
            
            mydb.open("class_Alert");
             rs = mydb.get("SELECT id,email,password FROM user WHERE email='"+email+"'and password="+password+" ");
            //TODO write your implementation code here:
            if(rs!=null){
               [B][U] user.setID(rs.getString("id"));[/U][/B]
                user.setUserlname(email);
                login=user.getID();
            }
            else login=null;
            }
            catch ( SQLException sqle){
                   sqle.getMessage();
                   
               }
    
            return login;
        }
     
        /**
         * Web service operation
         */
        @WebMethod(operationName = "admin")
        public Object admin(int select) {
    
           
            mydb.open("class_Alert");
            rs=mydb.get("SELECT * FROM user WHERE id="+[U][B]user.getID()[/B][/U]);
             try{
                // while (rs.next()){
                    user.setID(rs.getString("id"));
                    user.setUsername(rs.getString("fname"));
                    user.setUserlname(rs.getString("lname"));
                    user.setUserphone(rs.getString("phone"));
                    user.setUseremail(rs.getString("email"));
                    user.setRole(rs.getString("role"));
                   
                // }
            
             } catch ( SQLException sqle){
                  
             }
            switch (select) {
                case 1:  return user.getID();
                case 2:  return user.getUsername();
                case 3:  return user.getUserlname();
                case 4:  return user.getUserphone();
                case 5:  return user.getUseremail();
                case 6:  return user.getRole();
            }
            //TODO write your implementation code here:
           return user.toString();
        }
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    If your web service requires user information in order to work properly then your "website" that is using (consuming) the web service must provide the user information to the web service.

    So, a user logs into your website.

    You store their user information somewhere accessible to you for the duration of the time that they are logged in....like Session or something.

    When the user does something that requires the web service, you provide the information required by the web service in order for it to work.

    -Frinny

    Comment

    • Yene
      New Member
      • Oct 2009
      • 13

      #3
      Thank you for your help

      Comment

      Working...