Page 1 of 1

[SOLVED]Problem with getting parameter from html with applet

Posted: Wed Oct 19, 2005 7:28 am
by thallish
Hi guys

I've been working my brain for some time now with this and i havn't found a solution to this problem.

What I'm trying to do is to make a applet that dynamically makes a lot of buttons according to how many different links the currrent user has in a table. I'm using a signed applet because I need to acces the local computer.

This is not a problem, but how can i tell who is the current user? The information is in a $_SESSION variable so I need to check that, but how can I do this with a java applet?

Any thoughts?

Posted: Wed Oct 19, 2005 7:33 am
by feyd
you don't. The applet is run by the client, the session data resides on the server. You should have the applet query the server for the information needed, or have the script pass the information to the applet..

Posted: Wed Oct 19, 2005 7:37 am
by thallish
ok feyd that's what i meant :lol:

Specifically where this session information resides

Is this something i should look at Apache for or do you have any hint's on how to accomplish it?

Posted: Wed Oct 19, 2005 7:42 am
by feyd
has very little to do with Apache as it doesn't know about the session information. It's all under PHP's control. You need a script.

Posted: Wed Oct 19, 2005 8:02 am
by thallish
ok now i know what i'm gonna do. i'm gonna pass the content of the session variable to the applet and see if that works out.

i'll reply as soon as i'm done (or done for)

Posted: Fri Oct 21, 2005 2:32 am
by thallish
Now it all works. ill post my code here.

This is my Java class

Code: Select all

public class RunApplet extends JApplet {

    FlowLayout flowLayout1 = new FlowLayout();
    static int numberOfButtons = 0;
    public int userId;

    //Get a parameter value
    //if null then return default value provided
    public String getParameter(String key, String def) {

       String inKey;
        try {
            inKey = getParameter(key);
        } catch (NullPointerException e) {
            inKey = def;
        }
        return inKey;
    }
    //Construct the applet
    public RunApplet() {
        
    }

    public void init(){
        try {
            userId = Integer.parseInt(getParameter("userId", "2"));
        } catch (NumberFormatException ex) {
            System.out.println("NumberFormatException caught");
            ex.printStackTrace();
        }
        
        try {
           jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    private void jbInit() throws Exception {

        try {
            String db = "jdbc:mysql://localhost/db1";
            String username = "user";
            String password = "pass";

            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            this.getContentPane().setLayout(flowLayout1);

            try {
                Connection con = DriverManager.getConnection(
                        db, username, password);
                Statement stmt = con.createStatement();

                String query;
                ResultSet rs;

                query = "SELECT * FROM user_defined_program WHERE userId='" +
                        userId + "'";

                rs = stmt.executeQuery(query);

                while (rs.next()) {

                    JButton jButton = new JButton();

                    jButton.setPreferredSize(new Dimension(100, 24));

                    String name = rs.getString("programName");
                    jButton.setText(name);

                    String programPath = rs.getString("programPath");
                    jButton.addActionListener(new
                                              RunApplet_jButton_actionAdapter(this,
                            programPath));

                    this.getContentPane().add(jButton, null);

                    numberOfButtons++;
                }

            } catch (SQLException e) {
                e.printStackTrace();
            }

            int height = numberOfButtons * 24;

            this.setPreferredSize(new Dimension(110, height));

        }

        catch (Exception e) {
            e.printStackTrace();
        }

    }
}


class RunApplet_jButton_actionAdapter implements ActionListener {

    private RunApplet adaptee;
    private String programPath;

    RunApplet_jButton_actionAdapter(RunApplet adaptee, String programPath) {
        this.adaptee = adaptee;
        this.programPath = programPath;
    }

    public void actionPerformed(ActionEvent e) {
        try {
            Runtime.getRuntime().exec(this.programPath);
        } catch (IOException ex) {

        }
    }
}