Using the same BufferedReader across different servlets

I tried to implement several servlets such that it allows a HTTP mode of transmitting data to a socket based server.

In the very first servlet, I made it such that a connection with the server will be established in that particular file. At the same time, I would also like the BufferedReader created in the first servlet to be used in other servlets so that I will be able to maintain the same connection with the server.

This is what I have in the first servlet:

try

{

socket =new Socket(ipName, 12111);

out =new PrintWriter(socket.getOutputStream(),true);

in =new BufferedReader(new InputStreamReader(socket.getInputStream()));

}

catch (UnknownHostException e)

{

System.err.println("Don't know about host: " + ipName);

System.exit(1);

}

catch (IOException e)

{

System.err.println("Couldn't get I/O for " +"the connection to: " + ipName);

System.exit(1);

}

I tried to use "in" in another servlet to receive data from the server but that didn't work. Note that I have already declared "out" and "in" as static.

This is what I have in the second servlet:

while (keepPolling)

{

System.out.println("Polling...");

xmlResponse = Auth.in.readLine();

if(xmlResponse !=null)

{

System.out.println("Reply: " + xmlResponse);

keepPolling =false;

}

else

{

keepPolling =true;

}

}

Problem: The code does not go past xmlResponse = Auth.in.readLine();.

Anybody able to help?

Thanks!

[2505 byte] By [zactoraa] at [2007-10-2 20:11:00]
# 1

If by "not getting past" you mean the code is hanging: the readLine() method will keep waiting (blocking) until it has read an entire response line (ended with a newline). As long as you are not getting it from the host you are connecting with, it will block indefinately.

So my guess is the host is not sending anything back to you, or it is not including any newlines.

gimbal2a at 2007-7-13 22:51:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Hey thanks for that. I solved the problem. The server was not returning any new lines.
zactoraa at 2007-7-13 22:51:32 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...