Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

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.net.ProtocolException: Cannot write output after reading input.

843841Jul 4 2004 — edited Jul 4 2004
ive written an applet which communicates over the dopost() method and gets an aswear from the servlet. this works fine only if i do it one time. if i sourround this communication with a loop i get an exception:

java.net.ProtocolException: Cannot write output after reading input.


the source
import java.applet.* ;
import java.io.* ;
import java.net.* ;
public class HelloApplet extends Applet
{
	private static final String servlet = "http://localhost:8080/TomcatTest/hello";
	
	public void init()
	{
		System.out.println("init...");
		
		
			URLConnection con = null;
			
			try
			{
				con = getConnection(servlet);
			}
			catch (IOException e1)
			{
				System.err.println("Fehler beim Verbinden mit servlet\n");
				e1.printStackTrace();
			}
	
			
			for(int i=0; i<10; i++)
			{			
				try
				{
					sendObject( con , this.toString() );
				}
				catch (IOException e2)
				{
					System.err.println("Fehler beim senden...");
					e2.printStackTrace();
				}
			
				String s=null;
				try
				{
					s = (String) receiveObject(con);
				}
				catch (Exception e3)
				{
					e3.printStackTrace();
				}
				
				System.out.println(s);
				
			}
		
	}
	
	private static Object receiveObject( URLConnection con ) throws Exception
	{
		ObjectInputStream in = new ObjectInputStream(
		con.getInputStream() );
		Object obj = in.readObject();
		in.close();
		return obj ;
	}
	
	private URLConnection getConnection( String servlet ) throws IOException
	{
		URL u = new URL( servlet );
		URLConnection con = u.openConnection();
		con.setDoInput( true );
		con.setDoOutput( true );
		con.setUseCaches( false );
		con.setRequestProperty("Content-type","application/octet-stream");
		con.setAllowUserInteraction( false );
		return con ;
	}
	
	
	private void sendObject( URLConnection con , Object obj ) throws IOException
	{
		ObjectOutputStream out = new ObjectOutputStream( con.getOutputStream() );
		if ( obj != null )
		{
			out.writeObject( obj );
		}
		out.close();
	}
	
	
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 1 2004
Added on Jul 4 2004
3 comments
2,603 views