944,098 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2368
  • Java RSS
Oct 4th, 2007
0

Applet and i/o

Expand Post »
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";
}
}

Similar Threads
Reputation Points: 14
Solved Threads: 1
Junior Poster in Training
himanjim is offline Offline
67 posts
since Jul 2006
Oct 4th, 2007
0

Re: Applet and i/o

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.)
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: plz suggest
Next Thread in Java Forum Timeline: counter on a button





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC