when i run the code below in netbeans or eclipse it works, but when i put the applet in the browser it doesn't work. Please help me !
the applet for registration:
package newpackage;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.plaf.basic.BasicBorders;
public class registracija extends JApplet{
JTextField ime,prezime,embg,e_poshta,den,godina,korisnickoIme;
Server server;
Klient klient;
JPasswordField lozinka;
String meseci[]={"-Izberi-","Januari","Fevruari","Mart","April","Maj","Juni","Juli","Avgust","Septemvri","Oktomvri","Noemvri","Dekemvri"};
JRadioButton m,zh;
JButton zavrshi;
JLabel jl,jl1,jl2,jl3,jl4,jl5,jl6,jl7,jl8,jl9;
JComboBox jcb;
@Override
public void init(){
setSize(350, 370);
server=null;
klient=null;
setLayout(new GridLayout(12, 1));
lozinka=new JPasswordField();
JPanel p1=new JPanel(new FlowLayout(FlowLayout.LEFT)),p2=new JPanel(new FlowLayout(FlowLayout.LEFT)),p3=new JPanel(new FlowLayout(FlowLayout.LEFT)),p4=new JPanel(new FlowLayout(FlowLayout.LEFT)),p5=new JPanel(new FlowLayout(FlowLayout.RIGHT)),p7=new JPanel(new FlowLayout(FlowLayout.LEFT)),p8=new JPanel(new FlowLayout(FlowLayout.LEFT)),p9=new JPanel(new FlowLayout(FlowLayout.RIGHT)),p6=new JPanel(new FlowLayout());
jl1=new JLabel();jl2=new JLabel();jl3=new JLabel();jl4=new JLabel();jl5=new JLabel();jl6=new JLabel();jl7=new JLabel();jl8=new JLabel();
jl1.setForeground(Color.red);jl3.setForeground(Color.red);jl5.setForeground(Color.red);jl7.setForeground(Color.red);jl8.setForeground(Color.red);
jl2.setForeground(Color.red);jl4.setForeground(Color.red);jl6.setForeground(Color.red);
p1.add(new JLabel("Ime: "));
ime=new JTextField();
ime.setBounds(5, 5, 100, 100);
ime.setPreferredSize(new Dimension(110,20));p1.add(ime);p1.add(jl1);
add(p1);
p2.add(new JLabel("Prezme: "));
prezime=new JTextField();p2.add(prezime);p2.add(jl2);add(p2);
prezime.setBounds(5, 5, 100, 100);
prezime.setPreferredSize(new Dimension(110,20));
p3.add(new JLabel("EMBG: "));
embg=new JTextField();p3.add(embg);p3.add(jl3);add(p3);
embg.setBounds(5, 5, 100, 100);
embg.setPreferredSize(new Dimension(110,20));
p4.add(new JLabel("E-Poshta: "));
e_poshta=new JTextField();p4.add(e_poshta);p4.add(jl4);add(p4);
e_poshta.setBounds(5, 5, 100, 100);
e_poshta.setPreferredSize(new Dimension(110,20));
korisnickoIme=new JTextField();
p7.add(new JLabel(" Korisnicko ime:"));p7.add(korisnickoIme);p7.add(jl5);add(p7);
korisnickoIme.setBounds(5, 5, 100, 100);
korisnickoIme.setPreferredSize(new Dimension(110,20));
p8.add(new JLabel(" Lozinka: "));p8.add(lozinka);p8.add(jl6);
lozinka.setBounds(5, 5, 100, 100);
lozinka.setPreferredSize(new Dimension(110,20));add(p8);
JPanel panela=new JPanel(new FlowLayout(FlowLayout.LEFT));
add(new JLabel("Pol"));
m=new JRadioButton("Mashki");zh=new JRadioButton("Zhenski");
panela.add(m);panela.add(zh);
add(panela);
add(new JLabel("Den Na Ragjanje"));
JPanel panela1=new JPanel(new GridLayout(1,3));
jcb=new JComboBox(meseci);
den=new JTextField("den");godina=new JTextField("godina");
den.setFont(new Font("Serif", Font.ITALIC, 14));
godina.setFont(new Font("Serif", Font.ITALIC, 14));
den.addMouseListener(new mousenter(den));
godina.addMouseListener(new mousenter(godina));
panela1.add(jcb);panela1.add(den);panela1.add(godina);
add(panela1);
jl=new JLabel();p6.add(jl);
jl.setForeground(Color.red);
add(p6);
zavrshi=new JButton("Zavrshi");
add(p5);
p5.add(zavrshi);
zavrshi.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent event) {
if(ime.getText().isEmpty()){
jl.setText("Vnesi go Imeto");
jl1.setText("*");
}else if(prezime.getText().isEmpty()){
jl.setText("Vnesi go Prezimeto");
jl2.setText("*");
}else if(embg.getText().isEmpty()){
jl.setText("Vnesi embg");
jl3.setText("*");
}else if(e_poshta.getText().isEmpty()){
jl.setText("Vnesi e_poshta");
jl4.setText("*");
}else if(korisnickoIme.getText().isEmpty()){
jl.setText("Vnesi Korisnicko Ime");
jl5.setText("*");
}else if(lozinka.getText().isEmpty()){
jl.setText("Vnesi ja Lozinkata");
jl6.setText("*");
}else if(!(m.isSelected() || zh.isSelected())){
jl.setText("Izberi Pol");
}else if(jcb.getSelectedIndex()==0 || den.getText().equals("den") || godina.getText().equals("godina")){
jl.setText("Ispolni go datumot na radjanje");
}else{
jl.setText("");jl1.setText("");jl2.setText("");jl3.setText("");jl4.setText("");jl5.setText("");jl6.setText("");jl7.setText("");jl8.setText("");
Korisnici korisnik=new Korisnici(ime.getText(), prezime.getText(), embg.getText(), e_poshta.getText(), korisnickoIme.getText(), lozinka.getText(), true, Integer.parseInt(den.getText()), (String)jcb.getSelectedItem(), Integer.parseInt(godina.getText()));
if(server==null){
server=new Server(jl);
server.start();
klient=new Klient(korisnik,jl);
klient.start();
}else{
try {
klient.setk(korisnik);
} catch (IOException ex) {
Logger.getLogger(registracija.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
});
}
}
class mousenter implements MouseListener{
JTextField jtf;
public mousenter(JTextField jtf) {
this.jtf = jtf;
}
@Override
public void mouseClicked(MouseEvent me) {
}
@Override
public void mousePressed(MouseEvent me) {
jtf.setText("");jtf.setFont(new Font("Serif", Font.BOLD, 14));
}
@Override
public void mouseReleased(MouseEvent me) {
}
@Override
public void mouseEntered(MouseEvent me) {
}
@Override
public void mouseExited(MouseEvent me) {
}
}
the server class:
package newpackage;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Server extends Thread{
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
String whath;JLabel jl1;
public Server(JLabel jl) {
jl1=jl;
// jl1.setText("da raboti");
}
private void waitForConnection() throws IOException {
connection = server.accept();
// jl1.setText("connected");
}
private void setupStreams() throws IOException {
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
}
private void proveriIregistriraj() throws IOException, ClassNotFoundException {
// Korisnici k=(Korisnici) input.readObject();
// System.out.println("proveriIregistri.:"+ k.ime);
Korisnici korisnik=null;
try {
korisnik = (Korisnici) input.readObject();
// System.out.println("hello ");
if(korisnik!=null){
ArrayList<Korisnici> al = new ArrayList<Korisnici>();
try {
FileInputStream fileIn =
new FileInputStream("korisnici.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
Korisnici kl = null;
int count = 0;
try {
while (true) {
count++;
try {
Korisnici obj = (Korisnici) in.readObject();
al.add(obj);
} catch (ClassNotFoundException e) {
System.out.println("can't read obj #" + count + ": " + e);
}
}
} catch (EOFException e) {
} catch (IOException e) {
System.out.println("A");
} finally {
in.close();
}
} catch (Exception e) {
}
boolean flag=true;
for(Korisnici kk : al){
if(kk.korisnickoIme.equals(korisnik.korisnickoIme)){
flag=false;break;
}
}
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("korisnici.ser"));
for (Korisnici k : al) {
out.writeObject(k);
}
if(flag){
out.writeObject(korisnik);
output.writeObject("OK");
}
else{
output.writeObject("WRONG");
}
} catch (Exception e) {
}
}
} catch (ClassNotFoundException classNotFoundException) {
}
}
public void closeCrap() {
try {
output.close();
input.close();
connection.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
@Override
public void run() {
try {
server = new ServerSocket(9999, 100);
// jl1.setText("server created");
while (true) {
try {
waitForConnection();
setupStreams();
try {
proveriIregistriraj();
} catch (ClassNotFoundException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (EOFException eofException) {
} finally {
//closeCrap();
}
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
the client class:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package newpackage;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import javax.swing.JLabel;
public class Klient extends Thread{
private ObjectOutputStream output;
private ObjectInputStream input;
private Socket connection;
Korisnici korisnici;
JLabel jl;
public Klient(Korisnici korisnici,JLabel jl) {
this.korisnici=korisnici;
this.jl=jl;
// jl.setText("do tuka bilo");
}
public void startRunning(){
}
public void setk(Korisnici k) throws IOException{
korisnici=k;
run();
}
private void connectToServer() throws IOException{
connection = new Socket("localhost", 9999);
}
//set up streams to send and receive messages
private void setupStreams() throws IOException{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
}
//while chatting with server
private void prati() throws IOException{
// output.writeObject(korisnici);
// jl.setText("prati");
try{
// jl.setText("before");
output.writeObject(korisnici);
// jl.setText("after");
output.flush();
}catch(IOException ioException){
// jl.setText("in catch");
}
String s=null;
try{
s=(String) input.readObject();
if(s!=null && s.equals("WRONG") )
jl.setText("Korisnickoto ime veqe postoi");
}catch(ClassNotFoundException classNotfoundException){
}
}
public void closeCrap(){
try{
output.close();
input.close();
connection.close();
}catch(IOException ioException){
ioException.printStackTrace();
}
}
@Override
public void run() {
try{
jl.setText("try to connect");
connectToServer();
jl.setText("connected client");
setupStreams();
prati();
}catch(EOFException eofException){
}catch(IOException ioException){
ioException.printStackTrace();
}finally{
// closeCrap();
}
}
}
the user class:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package newpackage;
import java.io.Serializable;
public class Korisnici implements Serializable{
String ime,prezime,embg,e_poshta,korisnickoIme,lozinka,mesecR;
boolean mashkiRod;
int denR,godinaR;
public Korisnici(String ime, String prezime, String embg, String e_poshta, String korisnickoIme, String lozinka, boolean mashkiRod, int denR, String mesecR, int godinaR) {
this.ime = ime;
this.prezime = prezime;
this.embg = embg;
this.e_poshta = e_poshta;
this.korisnickoIme = korisnickoIme;
this.lozinka = lozinka;
this.mashkiRod = mashkiRod;
this.denR = denR;
this.mesecR = mesecR;
this.godinaR = godinaR;
}
public String getIme() {
return ime;
}
public String getPrezime() {
return prezime;
}
public String getEmbg() {
return embg;
}
public String getE_poshta() {
return e_poshta;
}
public String getKorisnickoIme() {
return korisnickoIme;
}
public String getLozinka() {
return lozinka;
}
public boolean isMashkiRod() {
return mashkiRod;
}
public int getDenR() {
return denR;
}
public String getMesecR() {
return mesecR;
}
public int getGodinaR() {
return godinaR;
}
}
.................................................... .................................................... .................................................... .....................................................
catch (Exception e) { ..
tocatch (Exception e) { e.printStackTrace(); // very informative! ..
I think I know why it is failing, but making that change should give you some clue.. 2) In future, don't post over 500 lines of code for such a problem! For better help sooner, post an SSCCE. – Andrew Thompson 2 days ago