Zolf
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
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
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
http://javaalmanac.com/egs/java.net/Post.html
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"
    data += "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("value2"
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
>>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
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
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.getContentPan
     Â
                 //
                 // jLabel1
                 //
                 jLabel1.setHorizontalAlign
                 jLabel1.setForeground(new Color(0, 0, 255));
                 jLabel1.setText("User Name:");
                 //
                 // jLabel2
                 //
                 jLabel2.setHorizontalAlign
                 jLabel2.setForeground(new Color(0, 0, 255));
                 jLabel2.setText("Password:
                 //
                 // jTextField1
                 //
                 jTextField1.setForeground(
                 jTextField1.setSelectedTex
                 jTextField1.setToolTipText
                 jTextField1.addActionListe
                      public void actionPerformed(ActionEven
                      {
                            jTextField1_actionPerforme
                      }
     Â
                 });
                 //
                 // jPasswordField1
                 //
                 jPasswordField1.setForegro
                 jPasswordField1.setToolTip
                 jPasswordField1.addActionL
                      public void actionPerformed(ActionEven
                      {
                            jPasswordField1_actionPerf
                      }
     Â
                 });
                 //
                 // jButton1
                 //
                 jButton1.setBackground(new
                 jButton1.setForeground(new
                 jButton1.setText("Login");
                 jButton1.addActionListener
                      public void actionPerformed(ActionEven
                      {
                            jButton1_actionPerformed(e
                      }
     Â
                 });
                 //
                 // contentPane
                 //
                 contentPane.setLayout(null
                 contentPane.setBorder(Bord
                 contentPane.setBackground(
                 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.setDefaultCloseOperat
                 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,heig
                 container.add(c);
           }
     Â
          Â
           private void jTextField1_actionPerforme
           {
                Â
     Â
           }
     Â
           private void jPasswordField1_actionPerf
           {
                Â
           }
          Â
           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+"
                            if(counter > 0)
                            {
                                 System.out.println(total+"
                                 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(A
           {
                 System.out.println("\njBut
                 String username = new String(jTextField1.getText
                 //String password = new String(jPasswordField1.get
                 String password = new String(jPasswordField1.get
                 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.showMessageDia
                      jTextField1.setText("");
                      jPasswordField1.setText(""
                      jButton1.setEnabled(true);
                      this.setVisible(true);
                 }
                 else
                 {
                      System.out.println("INSIDE
                      if(checkUser(getDBConnecti
                      {
                            System.out.println("INSIDE
                            dispose();
                            javax.swing.SwingUtilities
                                       {
                                            public void run()
                                            {
                                           Â
                                                  PictMenuFrameDemo.createAn
                                            }
                                       });
                                      Â
                                Â
                      }
                      else
                      {
                            System.out.println("INSIDE
                            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.setDefaultLookAndFe
                Â
                 //Create and set up the window.
                 Login frame = new Login();
                 frame.setDefaultCloseOpera
                Â
                 //Display the window.
                 frame.setVisible(true);
                Â
           }
          Â
           public static void main(String[] args)
           {
                 javax.swing.SwingUtilities
                            {
                                 public void run()
                                 {
                                       createAndShowGUI();
                                 }
                            });
           }
     Â
     Â
     Â
      }
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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
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
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.
the user actions table would include the session id and details of the user action.
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
>>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
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.
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
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
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.
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
It's one form of user authentication
>>i will handle this later
Yes, you can do that
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
ASKER
did you mean to use login class rather then form.html
Yes
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.
ASKER
please help
Imagine you were using println on them. Then think about making xml nodes
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 :)
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.
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 :)
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 :)
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.
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
no you would store use action in a seperate table
ASKER
objects can you please tell me what the table structure will look like
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();
:-)