Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3307650
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:25:50+00:00 2026-05-17T21:25:50+00:00

This is my second try to solve this problem. My first try was here

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:25:51+00:00Added an answer on May 17, 2026 at 9:25 pm

    The problem was that the applet couldn’t connect to the servlet so the code in the servlet here can be ignored.

    I needed to config server.xml with this:

    <Context path="/servletName" docBase="servletName" debug="0" reloadable="true"
       crossContext="true">
       </Context>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a problem I run into every now and then, that I usually try
This is actually a problem that I've already solved, but I suspect that there
This is my first post here and my first try to explain a coding
How can I make this second struct work, when the first struct has a
This is my second question about clicking buttons in JavaScript but this one I'm
This is my second question on Bamboo ( My First One ). My understanding
this is the second game that I'm having issues with in the same area...
If this is the first string : ABCD if this is the second string:
I know I'm a newbie in Rails but this is the second day I'm
i've asked the first question about selects here , i thought, that when i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.