Java Reference
In-Depth Information
Each of these accessor methods returns a string value that can be used for informa-
tional purposes or for constructing other URLs dynamically, as was done in the ex-
ample. If you take a look at the results from the solution to this recipe, you can see the
information that was obtained regarding the URL via the accessor methods listed in
Table 21-1 . Most of the accessors are self-explanatory. However, a couple of them
could use further explanation. The getFile() method returns the file name of the
URL. The file name is the same as the result of concatenating the value returned from
getPath() with the value returned from getQuery() . The getRef() method
may not be very straightforward. The reference component that is returned by calling
the getRef() method refers to the “fragment” that may be appended to the end of an
URL. For instance, a fragment is indicated using the pound character ( # ), followed by
a string that usually corresponds to a subsection on a particular web page. Given the
URL such as the following, recipe21_6 would be returned using the getRef()
method.
http://www.java8recipes.org/chapters/chapter21#recipe21_6
Although it's not always needed, the ability to parse an URL to obtain information
can come in very handy at times. Because the Java language has helper methods built
into the java.net.URL class, it makes gathering information pertaining to URLs a
piece of cake.
Summary
This chapter covered a few basic networking features of the Java language. In the re-
cent releases, there have been some nice new features added, such as the Socket Direct
Protocol. However, much of the java.net package has been unchanged for years,
and it is robust and easy to use. This chapter delved into using socket connections and
URLs and broadcasting messages via DatagramChannel . Chapter 22 contains a re-
cipe that covers another new feature of the java.net package, which is the new
URLPermission class.
Search WWH ::




Custom Search