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?
[SOLVED]Problem with getting parameter from html with applet
Moderator: General Moderators
[SOLVED]Problem with getting parameter from html with applet
Last edited by thallish on Fri Oct 21, 2005 10:03 am, edited 2 times in total.
Now it all works. ill post my code here.
This is my Java class
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) {
}
}
}