| | |
Applet and i/o
![]() |
•
•
Join Date: Jul 2006
Posts: 67
Reputation:
Solved Threads: 1
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";
}
}
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";
}
}
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.)
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
Similar Threads
- Java Applet viewer blinks everytime the init method is used. (Java)
- Java Game Applet Too Small (Java)
- Problem with Yahoo chat applet. (Windows NT / 2000 / XP)
- help!! My applet can't run... Urgent!! (Java)
- where to get the java applet? (Web Browsers)
- applet crashed (Windows NT / 2000 / XP)
- Applet or Application? (Java)
Other Threads in the Java Forum
- Previous Thread: plz suggest
- Next Thread: counter on a button
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application arguments array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class client code color component count database derby design eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui homework html ide if_statement image integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia keyword linux list macintosh map method methods midlethttpconnection mobile netbeans newbie nullpointerexception object open-source os problem producer program programming project projectideas property read recursion reference replaysolutions ria scanner search server set size sms sort sourcelabs splash sql sqlite stop string swing threads transforms tree ui unicode validation windows






