Link to home
Create AccountLog in
Avatar of Zolf
ZolfFlag for United Arab Emirates

asked on

Converting Java application


Hello there,

I have developed a java swing application with jnlp/oracle 10g/tomcat with EE helps and i appreciate all your help.Now I want to log user actions when they add/edit/delete in the application. for being able to do this i suppose i need to register user when they login.which presently i have a simple swing JDailog box which checks the username/password and then allows the user to use the application.now how can i use session and save user actions in db for later use.
i mean what technologies do i need to add so my application can register user actions when using the application.
please help.

cheers
zolf
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Collect the user's actions on the client and then POST them to a servlet at the servlet at the end of the user's client session
Avatar of Zolf

ASKER


I do i do that.presently my application is totally java swing standalone application with jnlp.how do i use POST and servlet.please help me because i am new to this servlet thingy.i mean how do i convert my application to allow servlet

thanks
You might also consider using a webservice, but here's how you'd do a POST from your app

http://javaalmanac.com/egs/java.net/Post.html
Avatar of Zolf

ASKER


thanks for your comment.how do i post the username/password into the code you provided.in my application the user uses jdialog box to enter username password which in turn checks with the db. and also how do i maintain the user state in between

String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("value1", "UTF-8");
        data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2", "UTF-8");
Just imagine key1=username, key2=password. Use https for proper security

>>how do i maintain the user state in between

You say you're already doing that:

>>I do i do that.presently

Collect on the client and POST again finally
Avatar of Zolf

ASKER


no i dont have anything that is maintaining user state.i dont know how to do it in a swing application.please tell me how to do it.
You need listeners for everything you're interested in recording. Add a node to an XML DOM each time in the listener and then finally post the compressed serialized DOM to your web app
Avatar of Zolf

ASKER


cehj,my application is not web app yet.just for you to get an idea to what my application starts with.this is the first thing that happens when i run the application.now how to i start building from here
      
      public class Login extends JFrame
      {
            // Variables declaration
            private JLabel jLabel1;
            private JLabel jLabel2;
            private JTextField jTextField1;
            private JPasswordField jPasswordField1;
            private JButton jButton1;
            private JPanel contentPane;
            // End of variables declaration
            Statement st;
      
            static Connection connect = null;
            public static final String DEFAULT_PROPERTY_FILE = "db.properties";
            
            
            public Login()
            {
                  super();
                  initConnection();
                  create();
                  //this.setVisible(true);
            }
      
            
            private void create()
            {
                  jLabel1 = new JLabel();
                  jLabel2 = new JLabel();
                  jTextField1 = new JTextField();
                  jPasswordField1 = new JPasswordField();
                  jButton1 = new JButton();
                  contentPane = (JPanel)this.getContentPane();
      
                  //
                  // jLabel1
                  //
                  jLabel1.setHorizontalAlignment(SwingConstants.LEFT);
                  jLabel1.setForeground(new Color(0, 0, 255));
                  jLabel1.setText("User Name:");
                  //
                  // jLabel2
                  //
                  jLabel2.setHorizontalAlignment(SwingConstants.LEFT);
                  jLabel2.setForeground(new Color(0, 0, 255));
                  jLabel2.setText("Password:");
                  //
                  // jTextField1
                  //
                  jTextField1.setForeground(new Color(0, 0, 255));
                  jTextField1.setSelectedTextColor(new Color(0, 0, 255));
                  jTextField1.setToolTipText("Enter your User Name");
                  jTextField1.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e)
                        {
                              jTextField1_actionPerformed(e);
                        }
      
                  });
                  //
                  // jPasswordField1
                  //
                  jPasswordField1.setForeground(new Color(0, 0, 255));
                  jPasswordField1.setToolTipText("Enter your Password");
                  jPasswordField1.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e)
                        {
                              jPasswordField1_actionPerformed(e);
                        }
      
                  });
                  //
                  // jButton1
                  //
                  jButton1.setBackground(new Color(204, 204, 204));
                  jButton1.setForeground(new Color(0, 0, 255));
                  jButton1.setText("Login");
                  jButton1.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e)
                        {
                              jButton1_actionPerformed(e);
                        }
      
                  });
                  //
                  // contentPane
                  //
                  contentPane.setLayout(null);
                  contentPane.setBorder(BorderFactory.createEtchedBorder());
                  contentPane.setBackground(new Color(204, 204, 204));
                  addComponent(contentPane, jLabel1, 5,10,106,18);
                  addComponent(contentPane, jLabel2, 5,47,97,18);
                  addComponent(contentPane, jTextField1, 110,10,183,22);
                  addComponent(contentPane, jPasswordField1, 110,45,183,22);
                  addComponent(contentPane, jButton1, 150,75,83,28);
                  //
                  // login
                  //
                  this.setTitle("Login To Members Area");
                  this.setLocation(new Point(76, 182));
                  this.setSize(new Dimension(335, 141));
                  this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                  this.setResizable(false);
            }
      
            /** Add Component Without a Layout Manager (Absolute Positioning) */
            private void addComponent(Container container,Component c,int x,int y,int width,int height)
            {
                  c.setBounds(x,y,width,height);
                  container.add(c);
            }
      
            
            private void jTextField1_actionPerformed(ActionEvent e)
            {
                  
      
            }
      
            private void jPasswordField1_actionPerformed(ActionEvent e)
            {
                  
            }
            
            private boolean checkUser(Connection connect,String username,String password)
            {      
                   boolean flag = false;
                   int counter = 0;
                try
                {
                      st = connect.createStatement();
                       String query = "SELECT * FROM users WHERE username = '"+username+"' AND passw = '"+password+"'";
                       ResultSet rs = st.executeQuery(query);
                 //rs.next();
               
                              // Start Display the new record
                              int total =0;
                              
                              System.out.println(query);
                              while (rs.next())
                          {
                                
                                    total = rs.getInt("user_id");
                                    counter++;
                            }
                              System.out.println(total+"  "+counter);
                              if(counter > 0)
                              {
                                    System.out.println(total+"  "+counter);
                                    flag = true;
                              }
                              else
                              {
                                    flag = false;
                              }
                              
                }
                catch (SQLException sqle)
                {
                      System.err.println("Can't get city collection from database : " + sqle.getMessage());
                }
                return flag;
          }
            
            private void jButton1_actionPerformed(ActionEvent e)
            {
                  System.out.println("\njButton1_actionPerformed(ActionEvent e) called.");
                  String username = new String(jTextField1.getText());
                  //String password = new String(jPasswordField1.getText());
                  String password = new String(jPasswordField1.getPassword());

                  if(username.equals("") || password.equals("")) // If password and username is empty > Do this >>>
                  {
                        jButton1.setEnabled(false);
                        JLabel errorFields = new JLabel("<HTML><FONT COLOR = Blue>You must enter a username and password to login.</FONT></HTML>");      
                        JOptionPane.showMessageDialog(null,errorFields);
                        jTextField1.setText("");
                        jPasswordField1.setText("");
                        jButton1.setEnabled(true);
                        this.setVisible(true);
                  }
                  else
                  {
                        System.out.println("INSIDE ELSE");
                        if(checkUser(getDBConnection(),username,password))
                        {
                              System.out.println("INSIDE IF");
                              dispose();
                              javax.swing.SwingUtilities.invokeLater(new Runnable()
                                          {
                                                public void run()
                                                {
                                                
                                                      PictMenuFrameDemo.createAndShowGUI();
                                                }
                                          });
                                          
                                    
                        }
                        else
                        {
                              System.out.println("INSIDE else");
                              jButton1.setEnabled(false);
                              jTextField1.setText("");
                              jPasswordField1.setText("");
                              jButton1.setEnabled(true);
                        }
                        
                  }
               }
 
            public static Connection getDBConnection()
            {
                  return connect;
            }
            
                        
            /**
             * Create the GUI and show it.  For thread safety,
             * this method should be invoked from the
             * event-dispatching thread.
             */
            protected static void createAndShowGUI()
            {
                  JFrame.setDefaultLookAndFeelDecorated(false);
                  
                  //Create and set up the window.
                  Login frame = new Login();
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  
                  //Display the window.
                  frame.setVisible(true);
                  
            }
            
            public static void main(String[] args)
            {
                  javax.swing.SwingUtilities.invokeLater(new Runnable()
                              {
                                    public void run()
                                    {
                                          createAndShowGUI();
                                    }
                              });
            }
      
      
      
      }
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Zolf

ASKER


>>such that the server is doing the authentication, not the client

cehj what did you mean by this.you mean the client is directly connecting to the db for authentication or the client connects to the server when then cnnects to the db for authentication

did you see my login class??
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
>>or the client connects to the server when then cnnects to the db for authentication

I assume you mean

or the client connects to the server WHICH then cnnects to the db for authentication

if so, yes

>>did you see my login class??

Yes, it's doing client side authentication. You need server side
the session table would include the session id (an auto increment field could be used here), the username and any other details you want to store about the session such as logon and logoff times.
the user actions table would include the session id and details of the user action.
Avatar of Zolf

ASKER

sorry for the typing error.
>>client side authentication. You need server side
could you please tell me the difference and which one am i currently using from looking at my Login code.

can you give me an code example or url where it is using server side authentication

please bear with me for asking these question
>>could you please tell me the difference

Client-side: authentication done on the client
Server-side: authentication done on the server

>> and which one am i currently using from looking at my Login code.

Client-side

>>can you give me an code example or url where it is using server side authentication

Any web app on the Web doing authentication does it server side
> could you please tell me the difference and which one am i currently using from looking at my Login code.

its just whether the authentication is done on the client or server isde, from the application perspective its really itrelevant

> can you give me an code example or url where it is using server side authentication

it would be exactly the same as what you already have.
>>t would be exactly the same as what you already have.

It wouldn't (shouldn't) actually, since the container (Tomcat) would do the authentication transparently, both to the user AND the programmer. Read up on 'Tomcat realms', as i mentioned
Avatar of Zolf

ASKER



thanks for clearing my doubts. i will change my code later for server authentication once i finish with this problem of mine.
now from my Login class how can i start listening to user actions when a user logs into the application and starts adding/editing/deleting data from the applcation.only one user can log into the application to do edit.so i need to know or i should be able to ckheck which user edited what.
Avatar of Zolf

ASKER


>>Read up on 'Tomcat realms'
thanks for referring to this.i just went through it briefly and this is what is server authentication.i will handle this later
>>and this is what is server authentication.

It's one form of user authentication

>>i will handle this later

Yes, you can do that
Avatar of Zolf

ASKER


instead of having this Login class.i mean instead of using swing componets to to create a login form to authenticate, can i use a html form and then after authenicating the user open my java application.is this possible.
It will be better to let your app do the authentication
Avatar of Zolf

ASKER


did you mean to use login class rather then form.html
Yes
Avatar of Zolf

ASKER


ok so far so good.now the next stage is to be able to save user actions.how should i begin with is thing.
Avatar of Zolf

ASKER


please help
Imagine you were using println on them. Then think about making xml nodes
Avatar of Zolf

ASKER


can you refer to me some url or code were i can get an idea to go forward because i have not worked with xml dom
> ok so far so good.now the next stage is to be able to save user actions.how should i begin with is thing.

already outlined how in my earlier comment :)
Collect the actions on the client. This will eliminate unnecessary network access
does nothing of the sort, in fact it is a very standard way of handling this type of thing.
Avatar of Zolf

ASKER


>>already outlined how in my earlier comment :)
you mentioned about the table structure.but not how do i implement to store user name and his/her action

>>Collect the actions on the client. This will eliminate unnecessary network access

cehj,how.i have no clue what do i use or how do i begin.

please give me some starting boost to begin with my login class
> but not how do i implement to store user name and his/her action

they're just db insterts.
When a user logs in insert a record into the session atble
when a user action occurs that u want to log, insert a record into the user action table.
nothing complicated required :)
Avatar of Zolf

ASKER


thanks for your comment.so first when the user logs into the application.
please check the step and correct me if i am wrong

1. save the user name,date,time in the session table
2. after some time he/she will do some editing then i have to store that particular action in the session table again.

another question how do i capture the time in my java application.
Avatar of Zolf

ASKER


what do i search for in google for these type of java application
> 2. after some time he/she will do some editing then i have to store that particular action in the session table again.

no you would store use action in a seperate table
Avatar of Zolf

ASKER


objects can you please tell me what the table structure will look like
Avatar of Zolf

ASKER


sorry,ignore my previous comment.you had already told me about it.
>>the session table would include the session id (an auto increment field could be used here), the username and any >>other details you want to store about the session such as logon and logoff times.
>>the user actions table would include the session id and details of the user action.

but how do i capture the time/date is there a java method??
Date now = new Date();
:-)