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();
}
}