Here is my code for an applet in java it runs well but shows error unreported exception java.io.IOException; at the functions highlighted in green
I wanna ask the functions i've highlighted highlighted in green in code are supported in applets?
Thnx in advance

/*
* Java(TM) SE 6 version.
*/


import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.io.*;
import java.net.*;
public class chat extends JApplet implements ActionListener
{
private JTextField chatField;
private JTextField loginField;
private JPasswordField passwordField;
private JTextArea chatarea;
private JLabel status;
private static String userName;
private static String passWord;
private Connection con = null;
private Statement stmt;
private String query = "select * from users";
private Socket kkSocket = null;
private  PrintWriter out = null;
private   BufferedReader in = null;
private int auth=0;
public  void main(String[] args) throws IOException {
init();
}
public void init(){
//Execute a job on the event-dispatching thread:
//creating this applet's GUI.
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't successfully complete");
}
}


private void createGUI(){
status = new JLabel();
JPanel contentPane = new JPanel(new GridBagLayout());
contentPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.BLACK),
BorderFactory.createEmptyBorder(10,10,5,5)));
setContentPane(contentPane);
GridBagConstraints c = new GridBagConstraints();
GridBagConstraints d = new GridBagConstraints();
if(auth==0){
c.insets = new Insets(0,0,0,0);
d.insets = new Insets(0,50,0,0);
loginField = new JTextField(10);
c.weightx = 1.0;
c.ipady = 5;
JLabel label1 = new JLabel("USERNAME:");
label1.setLabelFor(loginField);
add(label1,d);
c.anchor = GridBagConstraints.LINE_START;
c.gridwidth = GridBagConstraints.REMAINDER;
add(loginField, c);
loginField.addActionListener(this);


c.insets = new Insets(10,0,0,0);
d.insets = new Insets(10,50,0,0);
passwordField = new JPasswordField(10);
JLabel label2 = new JLabel("PASSWORD:");
label2.setLabelFor(passwordField);
add(label2,d);
add(passwordField, c);
c.gridwidth = GridBagConstraints.REMAINDER;
passwordField.addActionListener(this);


c.insets = new Insets(20,100,0,0);
JButton login = new JButton("LOGIN");
c.fill = GridBagConstraints.NONE; //keep the button small
c.ipady = 0;
c.ipadx = 10;
add(login, c);
login.addActionListener(this);
c.insets = new Insets(20,50,0,0);
add(status,c);
}


else {
c.insets = new Insets(0,0,5,5);
chatField = new JTextField(40);
c.ipady = 10;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
add(chatField, c);



JButton send = new JButton("SEND");
c.ipady = 0;
c.gridwidth = GridBagConstraints.REMAINDER; //end row
c.anchor = GridBagConstraints.LINE_START; //stick to the text field                                                  //
c.fill = GridBagConstraints.NONE; //keep the button small
c.weightx = 0.0;
add(send, c);


chatarea = new JTextArea(5, 60);
chatarea.setEditable(false);
c.anchor = GridBagConstraints.CENTER; //reset to the default
c.fill = GridBagConstraints.BOTH; //make this big
c.weighty = 1.0;
add(new JScrollPane(chatarea), c);}


}
public void actionPerformed(ActionEvent event) throws IOException
{
if(auth==0){
userName = loginField.getText();
char[] input = passwordField.getPassword();
passWord = new String(input);
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=anchal2");


if(!con.isClosed())
stmt = con.createStatement();
else
status.setText("Error connecting to database");
ResultSet rs = stmt.executeQuery(query);
rs.first();
String name = rs.getString("name");
String pass = rs.getString("password");
if(userName.compareTo(name)==0 && passWord.compareTo(pass)==0){
status.setText("Logged in");
auth=1;
createGUI();
}
else
status.setText("Wrong usename or password");
} catch(Exception e) {
;} finally {
try {
if(con != null)
con.close();
} catch(SQLException e) {}
}
}
else
{
try {
kkSocket = new Socket("localhost", 4444);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
} catch (UnknownHostException e) {


} catch (IOException e) {


}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser=chatField.getText();;
while ((fromServer = in.readLine()) != null) {
chatarea.append(fromServer);
fromUser = stdIn.readLine();
if (fromUser != null)
out.println(fromUser);
}


out.close();
in.close();
stdIn.close();
kkSocket.close();


}
}



public String getAppletInfo() {
return "Chat client by Himanshu Sharma";
}
}

Dani AI

Generated

Two quick points that explain the compile error and the bigger applet constraints.

The compiler message "unreported exception java.io.IOException" means a checked I/O exception is being thrown inside an event handler but not handled. You cannot change the ActionListener.actionPerformed signature to add checked exceptions, so wrap any socket/file I/O in try/catch (or move it to a method that you call from the handler and handle exceptions there). The Java Language Specification explains how overriding and checked exceptions work: Overriding and checked exceptions (JLS).

On what applets are allowed to do: is right that applets are restricted, but the precise rule is that an unsigned (untrusted) applet may only open network connections back to the host it came from (the applet codebase). Arbitrary socket connections and local file access are blocked unless the applet is signed and granted permissions by the user or by a security policy. See the official applet security notes: Applet security restrictions (Oracle Java Tutorial). Also note that using System.in in an applet (console input) is not appropriate — use GUI fields for user input.

Two practical fixes:

  • For local testing convert the code to an application (run with full permissions) or sign the JAR and request all-permissions (or a suitable policy).
  • Better architecture: keep the database and multi-client socket logic on the server (servlet or dedicated server) and have the client use an HTTP/WebSocket or same-origin socket. This avoids granting broad permissions.

Also avoid blocking the Swing event-dispatch thread: perform socket reads/writes on a background thread and update the GUI via SwingUtilities.invokeLater (or SwingWorker) so the UI remains responsive.

No. You cannot directly access sockets from an applet. (At least not without changing the security policies on the machine running the applet.) Maybe if it is a signed applet, but I don't believe that even a signed applet is allowed access to sockets. (At least not without changing the security policies, again.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.