Applet and i/o

Reply

Join Date: Jul 2006
Posts: 67
Reputation: himanjim is an unknown quantity at this point 
Solved Threads: 1
himanjim himanjim is offline Offline
Junior Poster in Training

Applet and i/o

 
0
  #1
Oct 4th, 2007
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";
}
}

Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,357
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 252
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Applet and i/o

 
0
  #2
Oct 4th, 2007
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC