Como passar uma classe serializada para uma Servlet

3 respostas
A
Não estou consequindo passar uma classe serializada para uma servlet, o que estou fazendo de errado alguem pode me ajudar

//Applet

public void sendContato(){

Contato  data = null;                    // Temporary storage for the data.

URL servletURL = null;                   // The URL to the servlet.

URLConnection servletConnection = null;  // The connection to the servlet.

ObjectOutputStream out =null;

data =new Contato();

data.setCodigo(99);

data.setNome(Tst 99);

data.setFone(9999);

try{

servletURL = new URL(<a href="http://localhost:8080/appletservelt/servletdbcontato?send=true">http://localhost:8080/appletservelt/servletdbcontato?send=true</a>”);

servletConnection = servletURL.openConnection();

servletConnection.setDoOutput(true);

out = new ObjectOutputStream(servletConnection.getOutputStream());

out.writeObject(data);

}
catch( MalformedURLException ee ){
          System.err.println( "MalformedURLException caught!" );
          System.err.println( ee.getMessage() );
          ee.printStackTrace( System.err );
        }
        catch( IOException eee )
        {
          System.err.println( "IOException caught!" );
          System.err.println( eee.getMessage() );
          eee.printStackTrace( System.err );
        }
        /*
        finally{
            try{
              out.close();
            }
            catch( IOException eeeee ){
              System.err.println( "IOException caught!" );
              System.err.println( eeeee.getMessage() );
              eeeee.printStackTrace( System.err );
            }
         }*/
      }
//Servlet

response.setContentType(CONTENT_TYPE);

PrintWriter out = response.getWriter();

out.println("<html>");

out.println("<head><title>Servlet1</title></head>");

out.println("");

Contato  data = null;                 // Temporary storage for the data.

ObjectInputStream dbStream = null;       // The stream from the servlet.

try{

dbStream = new ObjectInputStream(request.getInputStream());

data = (Contato) dbStream.readObject();

}

catch( MalformedURLException ee )

{

System.err.println( MalformedURLException caught! );

System.err.println( ee.getMessage() );

ee.printStackTrace( System.err );

}

catch( IOException eee ){

System.err.println( IOException caught! );

System.err.println( eee.getMessage() );

eee.printStackTrace( System.err );

}

catch( ClassNotFoundException eeee ){

System.err.println( ClassNotFoundException caught! );

System.err.println( eeee.getMessage() );

eeee.printStackTrace( System.err );

}

finally{

try{

dbStream.close();

}

catch( IOException eeeee ){

System.err.println( IOException caught! );

System.err.println( eeeee.getMessage() );

eeeee.printStackTrace( System.err );

}

}
if(data != null){
    out.println(data.getCodigo());
    out.println(data.getNome());
    out.println(data.getFone());
  }
  else
    System.out.print("DataSetData is null, no object read.");
    out.println("</body>&lt;/html&gt;");

3 Respostas

foia

qual a msg de erro !?

francisco.rodrigues

Algumas perguntas/respostas:
no Applet
a classe Contato é Serializable?
a sua URL está errada, vc precisa colocar a URI de invoker do Servlet

servletURL = new URL(“http://localhost:8080/appletservelt/servlet/servletdbcontato?send=true”);

e mapear no seu web.xml. Retirei o sinal de maior e menor para vc poder ver o comando, favor colocar qdo copiar para o seu web.xml
<servlet-mapping>
<servlet-name>invoker</servlet-name>
url-pattern>/servlet/*</url-pattern
</servlet-mapping>

Espero ter ajudado.

Francisco

A

Quanto a classe e serializada sim o mapeamento esta correto

Criado 25 de junho de 2004
Ultima resposta 28 de jun. de 2004
Respostas 3
Participantes 3