![]() ![]() 16 September 2001 ![]() |
![]() |
Into Java, Part 20
Last time we briefly discussed threads and made a little demo application.
This time we will continue using threads and at the same time implement the tiniest web browser ever seen. The
goal of this installment is to introduce networking. One of the main reasons Java ever surfaced was Internet use,
not just its networking capacity but that Java programs are small and portable. These days Java also has matured
to be very powerful for networking. |
||||||||||||
URL
A Uniform Resource Locator is better known under the acronym URL.
Java has a class java.net.URL that encapsulates a lot of mechanisms around the common URLs. A URL can be broken
down into: |
|
I do not recommend these kinds of hard coded ini files though,
they are hardly expanded and prone to other errors. Better are key-value pairs that can be parsed, e.g. two objects
in conjunction: String:"location" Point:[100,20], etc. Though, I recommend object files since they are
fast and they are easily used without unreadable and muddled code.
We continue with the doExit method that is called at exit, as seen
in the window adapter above. And we look at the tiny main method at the same time.
|
That was the outermost class. Now we will look into the BrowserPane
that is added to the frame's content pane.
|
Let us continue with the methods needed.
|
Since we implement the ActionListener interface, we have to add
the method actionPerformed. Two objects can send action events to this method, the url input field and the stop
button. The url input field enables the so far disabled stop button, clears the text area and creates the thread
object. That is it. When all that is done this object's thread will return and wait for user input, no matter
what the other thread does.
The stop button simply stops the other reading thread and disables
itself.
With this class done we continue with the threaded reader.
|
This class implements the Runnable interface and hence has to instantiate
its own thread. (This time we could have implemented this class to extend Thread as well, I chose this way only
because most of the time we extend other classes and add the thread to that.) We let the fresh object start itself.
Recall that the start method orders a JRE call to the method run.
|
Exactly as we are used to, we continue with reading new lines until
no more lines are read and the stream is finished. Each turn in the while loop we append the text to the tiny
browser's text area. As soon as the stream is read, we close the stream and we leave the run method causing this
thread to die naturally.
If the stop button was pressed, we see that the boolean continueLoading
is set to false and will break the while loop as well, the stream closes and the thread dies.
This way we construct a new threaded reader everytime we load a
new url. Of course we can do better with the system resources, but as threads are rather cheap to instantiate
and we must create a new URL object anyway, I do not consider that much to talk about. There are worse things
to avoid.
Have a nice time surfing.
Next time we will see a much more elegant web browser made much
easier than this one, but harder to understand. And we will dig into the URLConnection class. CU around.
Previous Article |
|
Next Article |