This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
In an applet I want to make an endless repetitive URLConnection to a servlet.
This means I plan to use a while(true) loop and open the connection within it.
Now my knowledge/understanding ends.
Can I establish the connection outside of the loop once and make my
permanent reads from the servlet within the loop - without ever closing the connection ?
Does it somehow harm the net ( traffic ) when I make the URLConnection within
the infinite loop ?
In an example of a chat program which I studied the authors made the URLConnection within an infinite loop - this even within a separate thread.
Time isn't Money, Money is Time.
The man who works looses his time ( spanish proverb )
Wolfgang Tintemann wrote:does the while loop be interrupted when the read command to the servlet is called ?
I don't think so. Not for any of the usual meanings of the word "interrupted" anyway. First the statement before the "read command" is executed. Then the connection to the server is made. Then (assuming no exceptions were thrown) the statement after that is executed. Is there some reason why you would describe the step in the middle as "interrupting" the while loop? Because I find hard to guess what was the reason you asked the question that way.
what i want to know is whether the while loop
stops when the read is executed and waits
for the response from the servlet ?
Or : are there done dozens, hundreds or millions
of URLConnections in the loop ?
I wonder how http deals with that.
Well, of course. Just like it stops and waits while you add one to a variable if that's what your code is supposed to do. I don't see why this is such a mystery: it's just Java code. First one statement is executed and then the next statement is executed.
Perhaps you could post some code which illustrates this extremely strange question?
Here I have some code from a chat program example.
A snippet from the applet :
Here is the infinite loop i am puzzled about.
My question is about line 14 : here the readLine() reads from the servlet.
DOES THE PROGRAM WAIT HERE FOR THE ANSWER FROM THE SERVLET ???
Or is perhaps some exception thrown and then the loop goes on over and over again ?
Here are the methods needed in the above snippet :
Well, yes. Either line 14 ends normally (at which time it will have done what the documentation says the method does) or it throws an exception. But again, this is true of all Java code.
You seem to think the fact that the "readLine" method is accessing data from somewhere else makes a difference. It doesn't.
Or perhaps you're confused about what the readLine method does? It reads a line (terminated by some line-ending character or characters) from the input stream. So your code (exceptions aside) connects to the servlet and reads the first line of whatever it returns as its response. And it does that repeatedly.