Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

java.io.StreamCorruptedException: invalid stream header

843807Apr 16 2008 — edited Apr 18 2008
Hi all...........

I am developing small application of an applet which sends string to Servlet and vice versa. but i am getting above exception.

I am herewith posting my code.
Can anyone tell me What could be wrong in my code.

Code For applet1.java
 

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class applet1 extends Applet implements ActionListener
{
		TextField text_send,text_receive;
		Button send;
		TextArea exception;
		Label label1,label2;
	

	public void init(){
		 
		setBackground(Color.gray);

		label1=new Label("Enter Your Message : ");
		add(label1);

		text_send = new TextField(20);
		add(text_send);

		send = new Button("Send");
		add(send);
		send.addActionListener(this);

		label2=new Label("You Have Entered : ");
		add(label2);

		text_receive=new TextField(20);
		add(text_receive);

		exception = new TextArea(20,30);
		add(exception);
		
	}

	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==send)
			{
							

					Onsend();
			}
	}
			// ------------------Get connection to the servlet through URLConnection Interface ------------------------------
				
private URLConnection getServletConnection() throws MalformedURLException, IOException 
	{
					
								
							URL getservlet = new URL("URL OF SERVER/Servlet.java");		
							URLConnection  con = getservlet.openConnection();

							//........................ Setting Internet caching parameters......................................

									con.setDoInput(true);
									con.setDoOutput(true);
									con.setUseCaches(false);
									con.setRequestProperty(
										"Content-Type",
										"application/x-java-serialized-object");

									return con;
	}			
	public void Onsend()
	{
							try
							{
								String input = text_send.getText();
								URLConnection con=getServletConnection();
								OutputStream out=con.getOutputStream();
								ObjectOutputStream oos = new ObjectOutputStream(out);
								oos.writeObject(input);
								oos.flush();
								oos.close();


								InputStream instr = con.getInputStream();
								ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
								String result = (String) inputFromServlet.readObject();
								inputFromServlet.close();
								instr.close();

								// show result
								text_receive.setText(result);
							}									
				
						
						catch(Exception ex)
						{
								ex.printStackTrace();
								exception.setText(ex.toString());
						}
	}

	
}


	
Code for Servlet.java
 import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class Servlet extends HttpServlet{

public void doPost(HttpServletRequest request, 
					HttpServletResponse response)throws ServletException,IOException{
	PrintWriter out = response.getWriter(); 
	try
	{
		response.setContentType("application/x-java-serialized-object");
		InputStream in=request.getInputStream();
		ObjectInputStream inputFromApplet=new ObjectInputStream(in);
		String echo = (String) inputFromApplet.readObject();	
		//out.println(echo);
		
		OutputStream outstr = response.getOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(outstr);
			oos.writeObject(echo);
			oos.flush();
			oos.close();
	}
	catch (Exception e)
	{
		e.printStackTrace();
	}


}


}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 16 2008
Added on Apr 16 2008
2 comments
159 views