Hi,
This is my second try to solve this problem. My first try was here but perhaps my explanation of my problem was insufficient, my problem was that the applet received the exception:
java.io.StreamCorruptedException: invalid stream header: 0A0A0A3C at
java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783) at
java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
sorry if I sound like a broken record :)
I'm trying to communicate between an Applet and a Servlet on the same machine, I've created the servlet in Netbeans by creating a New project - Java Web - Web Application and Choosing Glassfish Server 3 as server. It does create an index.jsp but I don't really need a web page interface.
I run the servlet from NetBeans (pressing f6) and it deploys and opens the servlet's index.jsp in my browser. I then run the applet (from a different project in Netbeans) and try to connect. I still receive the good ol' "invalid stream header" so I'm guessing the fault lies within something I've done in Netbeans.
I pasted some code I assume is working (old code but haven't found any more recent full examples) The code is blatantly stolen from Link
So in the end, what i'd like do is to send a two dimensional Object array from the servlet to the applet when the applet requests the array to be sent. The code examples is just to show the Invalid stream header I'm receiving.
I think/guess the applet is receving a textbased response from the server but I want the response to be a serialized-object (Just a String in the code example), it will be an Object[ ][ ] later, if I ever get a clue.
Thanks for your patience, gurus. :)
Applet code (feel free to ignore init() with all the layout code):
package se.iot.recallapplet;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class RecallApplet extends Applet {
private TextField inputField = new TextField();
private TextField outputField = new TextField();
private TextArea exceptionArea = new TextArea();
public void init() {
// set new layout
setLayout(new GridBagLayout());
// add title
Label title = new Label("Echo Applet", Label.CENTER);
title.setFont(new Font("SansSerif", Font.BOLD, 14));
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.weightx = 1.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(5, 5, 5, 5);
add(title, c);
// add input label, field and send button
c = new GridBagConstraints();
c.anchor = GridBagConstraints.EAST;
add(new Label("Input:", Label.RIGHT), c);
c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
add(inputField, c);
Button sendButton = new Button("Send");
c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
add(sendButton, c);
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onSendData();
}
});
// add output label and non-editable field
c = new GridBagConstraints();
c.anchor = GridBagConstraints.EAST;
add(new Label("Output:", Label.RIGHT), c);
c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
add(outputField, c);
outputField.setEditable(false);
// add exception label and non-editable textarea
c = new GridBagConstraints();
c.anchor = GridBagConstraints.EAST;
add(new Label("Exception:", Label.RIGHT), c);
c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
add(exceptionArea, c);
exceptionArea.setEditable(false);
}
/**
* Get a connection to the servlet.
*/
private URLConnection getServletConnection()
throws MalformedURLException, IOException {
// Connection zum Servlet ˆffnen
URL urlServlet = new URL("http://localhost:8080/Event_Servlet/");
URLConnection con = urlServlet.openConnection();
// konfigurieren
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
String input = inputField.getText();
// send data to the servlet
URLConnection con = getServletConnection();
OutputStream outstream = con.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstream);
oos.writeObject(input);
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();
exceptionArea.setText(ex.toString());
}
}
}
Servlet code:
package se.iot.eventservlet;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class Event_Servlet extends HttpServlet {
public void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
try {
response.setContentType("application/x-java-serialized-object");
// read a String-object from applet
// instead of a String-object, you can transmit any object, which
// is known to the servlet and to the applet
InputStream in = request.getInputStream();
ObjectInputStream inputFromApplet = new ObjectInputStream(in);
String echo = (String) inputFromApplet.readObject();
// echo it to the applet
OutputStream outstr = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstr);
oos.writeObject(echo);
oos.flush();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
stackTrace:
java.io.StreamCorruptedException: invalid stream header: 0A0A0A3C
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at se.iot.recallapplet.RecallApplet.onSendData(RecallApplet.java:114)
at se.iot.recallapplet.RecallApplet.access$000(RecallApplet.java:12)
at se.iot.recallapplet.RecallApplet$1.actionPerformed(RecallApplet.java:48)
at java.awt.Button.processActionEvent(Button.java:392)
at java.awt.Button.processEvent(Button.java:360)
at java.awt.Component.dispatchEventImpl(Component.java:4714)
at java.awt.Component.dispatchEvent(Component.java:4544)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:635)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)