ActionPerformed method not working when applet is loaded in browser window.

Advertisement
Hey there guys. I need urgent help from anybody who has experience in deploying websites whose code is in java.
I am having two problems as mentioned below...
first, I have made a simple login screen using java swing and JApplet. there is a single button to login. the action performed for this button accesses a private method to check the username and password which are there in atext file. the applet is working perfectly in appletviewer but when i load the applet in a Internet Explorer window using HTML's Applet tag, the button is giving no response at all even when i enter the correct username and password.
I guess it is either not calling the private function that is checking the username and password from the tes=xt file or it can not access the file. Please help as soon as possible as this is related to my college project.
I am attaching the code herewith. Suggestions to improve the coding are also welcome.
the second problem is that while writing my second program for generating a form which registers a user the html is not at all loading the applet into the browser and also if im trying to access a file to write all the details into the console is showing numerous amount of error after i press the button which i can't not understand. the only thing i can understand is that it is related to file access permissions. If anybody could put some light on the working of worker threads and thread safe activities of SwingUtilities.invokeandWait method it would be really appreciable.
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.plaf.*;
<applet code = "UserLogin" width = 300 height = 150>
</applet>
public class UserLogin extends JApplet implements ActionListener, KeyListener {
     private JLabel lTitle;
     private JLabel lUsername, lPassword;
     private JTextField tUsername;
     private JPasswordField tPassword;
     private JButton bLogin;
     private JLabel lLinkRegister, lLinkForgot;
     private JLabel lEmpty = new JLabel(" ", JLabel.CENTER);
     private JPanel panel1, panel2;
     public void init() {
          try {
               UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
               SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                         LoginGUI();
          catch(Exception e) {
               e.printStackTrace();
     public void start() {
          setVisible(true);
     public void stop() {
          setVisible(false);
     private void LoginGUI() {
          super.setSize(300, 150);
          super.setBackground(Color.white);
          lTitle = new JLabel("<HTML><BODY><FONT FACE = \"COURIER NEW\" SIZE = 6 COLOR = BLUE>Login</FONT></BODY></HTML>", JLabel.CENTER);
          lUsername = new JLabel("Username : ", JLabel.CENTER);
          lPassword = new JLabel("Password : ", JLabel.CENTER);
          tUsername = new JTextField(15);
          tPassword = new JPasswordField(15);
          bLogin = new JButton("LOGIN");
//          bLogin.setEnabled(false);
          bLogin.addActionListener(this);
          bLogin.addKeyListener(this);
          panel2 = new JPanel();
          GridBagLayout gbag = new GridBagLayout();
          GridBagConstraints gbc = new GridBagConstraints();
          panel2.setLayout(gbag);
          panel2.addKeyListener(this);
          gbc.anchor = GridBagConstraints.CENTER;
          panel2.setMinimumSize(new Dimension(300, 200));
          panel2.setMaximumSize(panel2.getMinimumSize());
          panel2.setPreferredSize(panel2.getMinimumSize());
          gbc.gridx = 1;
          gbc.gridy = 1;
          gbag.setConstraints(lUsername,gbc);
          panel2.add(lUsername);
          gbc.gridx = 2;
          gbc.gridy = 1;
          gbag.setConstraints(tUsername,gbc);
          panel2.add(tUsername);
          gbc.gridx = 1;
          gbc.gridy = 2;
          gbag.setConstraints(lPassword,gbc);
          panel2.add(lPassword);
          gbc.gridx = 2;
          gbc.gridy = 2;
          gbag.setConstraints(tPassword,gbc);
          panel2.add(tPassword);
          gbc.gridx = 2;
          gbc.gridy = 3;
          gbag.setConstraints(lEmpty,gbc);
          panel2.add(lEmpty);
          gbc.gridx = 2;
          gbc.gridy = 4;
          gbag.setConstraints(bLogin,gbc);
          panel2.add(bLogin);
          panel1 = new JPanel(new BorderLayout());
          panel1.add(lTitle, BorderLayout.NORTH);
          panel1.add(panel2, BorderLayout.CENTER);
          add(panel1);
          setVisible(true);
     public void keyReleased(KeyEvent ke) {}
     public void keyTyped(KeyEvent ke) {}
     public void keyPressed(KeyEvent ke) {
          if(ke.getKeyCode() == KeyEvent.VK_ENTER){
               String username = tUsername.getText();
               String password = new String(tPassword.getPassword());
               if(username.length() == 0 || password.length() == 0) {
                    JOptionPane.showMessageDialog(new JFrame(),"You must enter a username and password to login", "Error", JOptionPane.ERROR_MESSAGE);
               else {
                    boolean flag = checkUsernamePassword(username, password);
                    if(flag)
                         JOptionPane.showMessageDialog(new JFrame(),"Username and Password Accepted", "Access Granted", JOptionPane.INFORMATION_MESSAGE);
                    else
                         JOptionPane.showMessageDialog(new JFrame(),"Username or password Incorrect", "Access Denied", JOptionPane.INFORMATION_MESSAGE);
     public void actionPerformed(ActionEvent ae) {
          String gotCommand = ae.getActionCommand();
          if(gotCommand.equals("LOGIN")) {
               String username = tUsername.getText();
               String password = new String(tPassword.getPassword());
               if(username.length() == 0 || password.length() == 0) {
                    JOptionPane.showMessageDialog(new JFrame(),"You must enter a username and password to login", "Error", JOptionPane.ERROR_MESSAGE);
               else {
                    boolean flag = checkUsernamePassword(username, password);
                    if(flag)
                         JOptionPane.showMessageDialog(new JFrame(),"Username and Password Accepted", "Access Granted", JOptionPane.INFORMATION_MESSAGE);
                    else
                         JOptionPane.showMessageDialog(new JFrame(),"Username or password Incorrect", "Access Denied", JOptionPane.INFORMATION_MESSAGE);
     private boolean checkUsernamePassword(String username, String password) {
          String user = null, pswd = null;
          try {
               FileInputStream fin = new FileInputStream("@data\\userpass.txt");
               DataInputStream din = new DataInputStream(fin);
               BufferedReader brin = new BufferedReader(new InputStreamReader(din));
               user = (String) brin.readLine();
               pswd = (String) brin.readLine();
          catch(IOException ioe) {
               ioe.printStackTrace();
          if(username.equals(user) && password.equals(pswd))
               return true;
          else
               return false;
}PLEASE HELP ME GUYS......
Advertisement

Replay

RockAsh wrote:
Hey Andrew, first of all sorry for that shout, it was un-intentional as i am new to posting topics on forums and didn't new that this kind of writing is meant as shouting. Cool.
Secondly thank you for taking interest in my concern.No worries.
Thirdly, as i mentioned before, I am reading i file for checking of username and password. the file is named as "userpass.txt" and is saved in the directory named "@data" which is kept in the same directory in which my class file resides.OK - server-side. That makes some sense, and makes things easier. The problem with your current code is that the applet will be looking for that directory on the end user's local file system. Of course the file does not exist there, so the applet will fail unless the the end user is using the same machine as the server is coming from.
To get access to a resource on the server - the place the applet lives - requires an URL. In applets, URLs are relatively easy to form. It might be something along the lines of
URL urlToPswrd = new URL(getCodeBase(), "@data/userpass.txt");
InputStream is = urlToPswrd.openStream();
DataInputStream din = new DataInputStream(is);
So the problem is that it is reading the file and showing the specific output dialog box when i run it through appletviewer.. Huhh. What version of the SDK are you using? More recent applet viewers should report security exceptions if the File exists.
..but the same is not happening when i launch the applet in my browser window using the code as written belowHave you discovered how to open the Java Console in the browser yet? It is important.
Also the answer to your second question
Also, the entire approach to storing/restoring the password is potentially wrong. For instance, where is it supposed to be stored, on the server, or on the client?is that, as of now it is just my college project so all the data files and the username and password wiles will be stored on my laptop only i.e. on the client only. no server involved.OK, but understand that an applet ultimately does not make much sense unless deployed through a server. And the entire server/client distinction becomes very important, since that code would be searching for a non-existent file on the computer of the end user.