This code works, but it fails if I use my own objects, can you help me?
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Wednesday, February 27, 2002 06:40 AM

Hi all, with the same code that you can find at the end, I have been able to send String, Date, ... objects from applet to servlet and viceversa, but when I want define my own object and send and receive it, the code is not working. In the applet I have putted two TextFields. In the first I can write a word and when I press the button, it is sended to the servlet. The servlet constructs a new object that depends on the contents that it receives, and send this new object to tue applet, that is waiting for it. Then, the applet shows part of the content of it on the second TextField. I have added several lines that are useful for debugging proposals. With them I am superv   More>>

Hi all,

with the same code that you can find at the end, I have been able to send String, Date, ... objects from applet to servlet and
viceversa, but when I want define my own object and send and receive it, the code is
not working.

In the applet I have putted two TextFields. In the first I can write a word and when I press the
button, it is sended to the servlet. The servlet constructs a new object that depends on the contents that it
receives, and send this new object to tue applet, that is waiting for it. Then, the applet
shows part of the content of it on the second TextField.

I have added several lines that are useful for debugging proposals. With them I am supervising the applet work.
If the user input "Hello" on the first TextArea, the output on the second must be "HELLO", but if there is a problem, a
different message will be showed.

On the servlet I am writting to a log file to know what is happening.

To aboiv problems with the installation of mi class into the web server, I have defined my class two times: The first inside
the applet and the second inside the servlet. I have made tests putting the definition of my class outside them, as a public class,
but the result is still worse.

I am working with JavaWebServer.

Please, I would like somebody say to me why it is not working.

1. I input "Hello" on the first TextArea.

2. The output on the second TextArea is: "step 1 ok - step 2 ok - res is null - step 4 ok -"

3. The output on the log file from the servlet is: "log file: java.io.ObjectInputStream@1bfe2b Step 2 exception - step 3 ok -"

Thank You.

/***********************/

/* APPLET SOURCE CODE */

/***********************/

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

import java.net.URL;

import java.net.URLConnection;

import java.io.*;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;





public class AppletRep_mod3 extends Applet implements ActionListener

{

Button envButton;

TextField inField;

TextField outField;

String textoObtenido;





public void init()

{

setLayout (new FlowLayout());



envButton = new Button ("Send to the servlet");

inField = new TextField ("", 15);

outField = new TextField ("", 30);



add (inField);

add (outField);

add (envButton);

envButton.addActionListener (this);

}





public void paint (Graphics g)

{

outField.setText (textoObtenido);

}





public void actionPerformed (ActionEvent evt)

{

if (evt.getSource() == envButton)

contactar_con_servlet (inField.getText());



repaint();

}





private void contactar_con_servlet (String in_cad)

{

Object res = null;



try

{

String servletUrl="http://localhost:8080/servlet/ServletRep_mod3";



URL url=new URL(servletUrl);

URLConnection con=url.openConnection();

con.setDoInput(true);

con.setDoOutput(true);

con.setRequestProperty("Content-Type","java-internal/java.lang.Object");



// This is the object to send to the servlet.

MiObjAtS objAS = new MiObjAtS();

objAS.cad_inicial = in_cad;

objAS.long_cad = in_cad.length();



textoObtenido = "step 1 ok -";



// We send the object to the servlet.

ObjectOutputStream oos = new ObjectOutputStream(con.getOutputStream());

oos.writeObject(objAS);

oos.flush();

oos.close();



textoObtenido += " step 2 ok -";



// We read an object from the servlet.

ObjectInputStream ois = new ObjectInputStream(con.getInputStream());

res=ois.readObject();

ois.close();

}

catch(Exception ex)

{

ex.printStackTrace();

}



if (res == null)

{

textoObtenido += " res is null -";

repaint();

}

else

textoObtenido += " step 3 ok -";



// We are making a cast with the object.

MiObjStA objSA = (MiObjStA)res;



textoObtenido += " step 4 ok -";



// We are building the messag to show in the TextField.

Integer dat_int = new Integer (objSA.num_msg);

textoObtenido = objSA.cad_modificada;

}





private class MiObjAtS implements Serializable

{

String cad_inicial = null;

int long_cad;

}





private class MiObjStA implements Serializable

{

String cad_inicial = null;

String cad_modificada = null;

int num_msg;

}

}





/***********************/

/* SERVLET SOURCE CODE */

/***********************/

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.sql.Date;





public class ServletRep_mod3 extends HttpServlet

{

private final static String pag_error = "ServletRep_err.htm";





public void service (HttpServletRequest req, HttpServletResponse res)

throws IOException, ServletException

{

ObjectInputStream ooi = new ObjectInputStream(req.getInputStream());

Object obj = null;

MiObjAtS objAS = null;

MiObjStA objSA = null;

String str_log = "";



try

{

// We retrieve the object from the applet.

obj=ooi.readObject();

objAS = (MiObjAtS)obj;

ooi.close();

str_log += " step 1 ok -";

}

catch(Exception ex)

{

str_log += " Step 1 excepción -";

System.err.println ("Exception");

ex.printStackTrace (System.err);

}





if (ooi == null)

str_log = " ooi is null -";

else

str_log = " ooi=" + ooi.toString() + " ";





try

{

// This is the object to send to the applet.

objSA = new MiObjStA();

objSA.cad_inicial = objAS.cad_inicial;

objSA.cad_modificada = objAS.cad_inicial.toUpperCase();

objSA.num_msg = objAS.long_cad + 3000;



str_log += " step 2 ok -";

}

catch(Exception ex)

{

str_log += " Step 2 excepción -";

System.err.println ("Exception");

ex.printStackTrace (System.err);

}





try

{

// We send the object to the applet.

ObjectOutputStream oos = new ObjectOutputStream(res.getOutputStream());

oos.writeObject(objSA);

oos.flush();

oos.close();



str_log += "step 3 ok -";

}

catch(Exception ex)

{

str_log += " Step 3 excepción -";

System.err.println ("Exception");

ex.printStackTrace (System.err);

}





// We are writting to the log file.

byte b[] = ("Fichero de log: " + str_log).getBytes();

File file_log = new File ("log_servlet3.txt");

FileOutputStream canal_log = new FileOutputStream (file_log);

canal_log.write (b);

canal_log.close ();

}





private class MiObjAtS implements Serializable

{

String cad_inicial = null;

int long_cad;

}





private class MiObjStA implements Serializable

{

String cad_inicial = null;

String cad_modificada = null;

int num_msg;

}

}

   <<Less
About | Sitemap | Contact