Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

invoking servlet using URLConnection

 
prasad hagargi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there,
i am trying to invoke a servlet from one jboss to another servlet on another jboss,using URLConnection.
iam getting
java.lang.IllegalStateException: Already connected
20:57:48,371 ERROR [STDERR] at java.net.URLConnection.setDoInput(URLConnection.java:709)
20:57:48,371 ERROR [STDERR] at com.dpsl.dxdam.util.assettransfer.FTPController.run(Unknown Source)
where as if i run the same code on as standalone application,the servlet on the other jboss gets invoked,below is the code:

URL url = new URL(strPreingestServletUrl);
URLConnection connection = url.openConnection();
connection.connect();
connection.setDoOutput(true);connection.setUseCaches(false);connection.connect();
InputStream is = connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String response = in.readLine();System.out.println(response);
while(response!=null){
System.out.println(response);
response = in.readLine();
}
is.close();
in.close();

please explain why this happens,does it have anything to do with the policy file of catalina.
--thanks in advance--Prasad
 
Jaime Garcia
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the section url.openConnection(); already opens the connection.
URLConnection connection = url.openConnection();
so, you do not have to:
connection.connect();
Hope this helps
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that is entirely correct. The URLConnection.connect() method will open a connection only if there isn't one already established, otherwise the call will be ignored.
I think you are generating the IllegalStateException in the URLConnection.setDoInput(boolean) method. It looks like that you are calling setup parameters after the actual connection has already been established. This should be done prior to calling the URLConnection.connect() method.

Craig
 
prasad hagargi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks u were right i was openning the connection before setting the doinput and dooutput fields
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic