File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes invoking servlet using URLConnection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "invoking servlet using URLConnection" Watch "invoking servlet using URLConnection" New topic

invoking servlet using URLConnection

prasad hagargi
Ranch Hand

Joined: Oct 21, 2002
Posts: 36
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
20:57:48,371 ERROR [STDERR] at 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();
InputStream is = connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String response = in.readLine();System.out.println(response);
response = in.readLine();

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

Joined: Jan 07, 2003
Posts: 38
the section url.openConnection(); already opens the connection.
URLConnection connection = url.openConnection();
so, you do not have to:
Hope this helps
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
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.

prasad hagargi
Ranch Hand

Joined: Oct 21, 2002
Posts: 36
thanks u were right i was openning the connection before setting the doinput and dooutput fields
I agree. Here's the link:
subject: invoking servlet using URLConnection
It's not a secret anymore!