[SOLVED]Problem with getting parameter from html with applet

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

[SOLVED]Problem with getting parameter from html with applet

Post 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?
Last edited by thallish on Fri Oct 21, 2005 10:03 am, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

Post 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)
thallish
Forum Commoner
Posts: 60
Joined: Wed Mar 02, 2005 11:38 am
Location: Aalborg, Denmark

Post 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) {

        }
    }
}
Post Reply