HI everyine,
I trying to send some data from an applet to a servlet: Here is code for the applet:
try{
System.out.println("in the try for getData \n");
URL url = new URL("http://myip/app/servlet/getData");
System.out.println("the url connection to getData done \n");
URLConnection servletConnection2 =url.openConnection();
///////////////I get error java.lang.IllegalStateException:Already Connected at this point //////
servletConnection2.setDoInput(false);
servletConnection2.setDoOutput(true);
servletConnection2.setUseCaches (false);
servletConnection2.setDefaultUseCaches (false);
servletConnection2.connect();
servletConnection2.setRequestProperty("Content-Type","application/octet-stream");
ObjectOutputStream oos = new ObjectOutputStream(servletConnection2.getOutputStream());
oos.writeObject(userID);
oos.flush();
System.out.println("the userID was flushed");
oos.close();
}
catch(Exception e)
{
System.out.println("Error in sending data to getData Servlet \n" +e);
}
Here is what I have in my servlet:
public class getData extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username ="";
try
{
ObjectInputStream in = new ObjectInputStream(request.getInputStream());
username = (String)in.readObject();
System.out.println("the username received from ui is \n" +username);
in.close();
}
catch (IOException e)
{
System.out.println("error1" + e);
}
catch (ClassNotFoundException cnfe)
{ {System.out.println("error2");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
When I try to access this applet I keep getting an error :
java.lang.IllegalStateException: Already connected
I cannot understand why this could eb happening ..I have been stuck on this problem for quite some time now.....
Thanks,
G.