Bom dia amigos,
Criei um applet que faz a comunicação com o servlet, consigo enviar e receber informações básicas.
Como uma String por exemplo, até esse ponto tudo lindo e maravilhoso.
Mas seria do meu interesse enviar do meu servlet um ArrayList para o Applet, mas ai que a coisa para de funcionar.
Há alguma restrição quanto a isso? Ou será que é porque eu tenho um ArrayList de Bean…
Eu precisei implementar o Serializable no meu Bean para poder compilar os códigos.
Segue os códigos para ajudar, e também para quem quiser fazer a comunicação applet -> servlet e vice-versa.
Coisa que dá uma baita trabalho pra fazer.
Abraço e obrigado pela ajuda.
Servlet
public void servico(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
// ObjectOutputStream out = new ObjectOutputStream(res.getOutputStream());
// out.writeObject(teste2);
String usuario = " ";
String senha = " ";
ObjectInputStream in = new ObjectInputStream(req.getInputStream());
try {
usuario = (String) in.readObject();
senha = (String) in.readObject();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(usuario);
System.out.println(senha);
try {
jogBean = jogDAO.getJogadores(usuario, senha);
} catch (JogadorDAOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try
{
//Tipo de conteúdo
res.setContentType("application/x-java-serialized-object");
//Envia dados para a applet
ObjectOutputStream out = new ObjectOutputStream(res.getOutputStream());
out.writeObject(jogBean);
// String teste = "xxxxxxxxXXXXXXXXXXXXXxxx";
// out.writeObject(teste);
System.out.println("final");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Applet
public class RelatorioTreino17 extends JApplet
implements MouseListener, MouseMotionListener, Serializable {
private static final long serialVersionUID = 1L;
int mx, my; // the most recently recorded mouse coordinates
Image campo;
Button sendButton;
String usuario = " ";
String senha = " ";
String teste1 = " ";
String teste2 = " ";
ArrayList<JogadoresBean> jogBean = new ArrayList<JogadoresBean>();
public void init() {
addMouseListener( this );
addMouseMotionListener( this );
setSize(750, 400);
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c = new GridBagConstraints();
c.anchor = GridBagConstraints.LAST_LINE_END;
c.weightx = 1.0;
c.gridwidth = 1; //reset to the default
c.gridheight = 1;
c.weighty = 1.0;
sendButton = new Button("Send");
add(sendButton, c);
usuario = getParameter("usuario");
senha = getParameter("senha");
try {
ObterDadosServlet();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
private void ObterDadosServlet() throws MalformedURLException, IOException, ClassNotFoundException {
URLConnection con = getServletConnection();
// envia dados para o servlet
OutputStream outstream = con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(usuario);
oos.writeObject(senha);
oos.flush();
oos.close();
// recebe resultado do servlet
InputStream instr = con.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
// jogBean = (ArrayList<JogadoresBean>) inputFromServlet.readObject();
teste1 = (String) inputFromServlet.readObject();
inputFromServlet.close();
instr.close();
// teste1 = "Jesus";
}
public void mouseEntered( MouseEvent e ) { }
public void mouseExited( MouseEvent e ) { }
public void mouseClicked( MouseEvent e ) { }
public void mousePressed( MouseEvent e ) {
mx = e.getX();
my = e.getY();
e.consume();
}
public void mouseReleased( MouseEvent e ) {
}
public void mouseMoved( MouseEvent e ) {
mx = e.getX();
my = e.getY();
repaint();
e.consume();
}
public void mouseDragged( MouseEvent e ) {
// get the latest mouse position
int new_mx = e.getX();
int new_my = e.getY();
repaint();
e.consume();
}
public void paint( Graphics g ) {
Graphics2D g2d = (Graphics2D)g;
g.setColor(Color.white);
g.drawImage(campo, 0, 0, this);
FontRenderContext frc = ((Graphics2D) g).getFontRenderContext();
Font f = new Font("Arial",Font.BOLD,10);
System.out.println(teste1);
System.out.println(teste2);
System.out.println(senha);
System.out.println(usuario);
TextLayout tl = new TextLayout(teste1,f,frc);
g2d.setColor(Color.black);
tl.draw(g2d, 40, 40);
TextLayout tl1 = new TextLayout(teste2,f,frc);
g2d.setColor(Color.black);
tl1.draw(g2d, 40, 140);
TextLayout tl2 = new TextLayout(usuario,f,frc);
g2d.setColor(Color.black);
tl2.draw(g2d, 40, 240);
TextLayout tl3 = new TextLayout(senha,f,frc);
g2d.setColor(Color.black);
tl3.draw(g2d, 40, 340);
}
public void update(Graphics g) {
paint(g);
}
public boolean action (Event e, Object args) {
if (e.target == sendButton)
{
onSendData();
}
return true;
}
/**
* Get a connection to the servlet.
*/
private URLConnection getServletConnection()
throws MalformedURLException, IOException {
URL urlServlet = new URL(getCodeBase(), "RelatorioTreinoApplet");
URLConnection con = urlServlet.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty("Content-Type","application/x-java-serialized-object");
return con;
}
/**
* Send the inputField data to the servlet and show the result in the outputField.
*/
private void onSendData() {
try {
// get input data for sending
// send data to the servlet
URLConnection con = getServletConnection();
OutputStream outstream = con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
String teste = "Olá servlet";
oos.writeObject(teste);
oos.flush();
oos.close();
// receive result from servlet
InputStream instr = con.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
String result = (String) inputFromServlet.readObject();
inputFromServlet.close();
instr.close();
// show result
// outputField.setText(result);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
class JogadoresBean implements Serializable {
private static final long serialVersionUID = 3L;
public JogadoresBean () {
}
// código.....
}