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

  • SEARCH
  • Home
  • 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 3242444
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:18:58+00:00 2026-05-17T18:18:58+00:00

I’m building a solution where a servlet on tomcat6 handles input from another machine

  • 0

I’m building a solution where a servlet on tomcat6 handles input from another machine by providing a jsp web page. I’d also like the same servlet to communicate with an applet on the same machine to show some results and perform a few tasks.

I’m able to connect from the applet to the servlet using a URLConnection but when receving a message from the servlet I get a “invalid stream header” exception message from the applet.

Any ideas?

edit: Error message:

java.io.StreamCorruptedException: invalid stream header: 0A0A0A3C
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at applet.viewApplet.onSendData(viewApplet.java:126)

Applet code:

 private URLConnection getServletConnection()
        throws MalformedURLException, IOException {

    // Open the servlet connection
    URL urlServlet = new URL("http://localhost:8080/Servlet");
    connection = urlServlet.openConnection();

    // Config
    connection.setDoInput(true);
    connection.setDoOutput(true);

    connection.setUseCaches (false);
    connection.setDefaultUseCaches (false);

    connection.setRequestProperty(
            "Content-Type",
            "application/x-java-serialized-object");

    return connection;
}

private void onSendData() {
    try {
        // get input data for sending
        String input = "Applet string heading for servlet";

        // send data to the servlet
        connection = getServletConnection();
        OutputStream outstream = connection.getOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(outstream);
        oos.writeObject(input);
        oos.flush();
        oos.close()

        // receive result from servlet
        InputStream instr = connection.getInputStream();
        ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
        String result = (String) inputFromServlet.readObject();
        inputFromServlet.close();
        instr.close();

        // show result
        textField.setText(result);

    } catch (java.net.MalformedURLException mue) {
        textField.setText("Invalid serlvetUrl, error: " + mue.getMessage());
    } catch (java.io.IOException ioe) {
        textField.setText("Couldn't open a URLConnection, error: " + ioe.getMessage());
    } catch (Exception e) {
        textField.setText("Exception caught, error: " + e.getMessage());
    }
}

Servlet code:

public class Servlet extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=ISO-8859-1");
    PrintWriter out = response.getWriter();
    String defect = request.getParameter("defect").toString();

    try {
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet at " + request.getContextPath() + "</h1>");
        out.println("<p>Defect: " + defect + "</p>");
        out.println("</body>");
        out.println("</html>");
    } finally {
        out.close();
    }
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {
        response.setContentType("application/x-java-serialized-object");

        InputStream in = request.getInputStream();
        ObjectInputStream inputFromApplet = new ObjectInputStream(in);

        String servletText = "Text from Servlet";

        // echo it to the applet
        OutputStream outstr = response.getOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(outstr);
        oos.writeObject(servletText);
        oos.flush();
        oos.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

    processRequest(request, response);
}
  • 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-17T18:18:59+00:00Added an answer on May 17, 2026 at 6:18 pm

    That’s because you’re attempting to deserialize the textbased response body of the servlet using ObjectInputStream while it isn’t serialized at all and you should be using a Reader for this.

    Long story short: How to fire and handle HTTP requests using URLConnection?.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.