URL provider sample
Example 6-3 provides a code sample making use of the URL provider and URL resources. Note that the Web module resource reference, myHttpUrl, is bound to the URL resource JNDI name...
url/MotdUrl
...during application assembly or deployment.
javax.naming.InitialContext ctx = new javax.naming.InitialContext(); javax.naming.Context env = (javax.naming.Context) ctx.lookup("java:comp/env"); java.net.URL url = (java.net.URL) env.lookup("myHttpUrl"); java.io.InputStream ins = url.openStream(); int c; while ((c = ins.read()) != -1) { out.write(c); }In this case, we inserted the Example 6-3 code into a JSP, added the JSP to a Web module, added a URL resource reference to the Web module, and then deployed the Web module. Then we checked that the contents of the file specified in the MotdUrl URL resource...
file:///d:/url/motd.txt
...were included in the JSP's output.
Similarly, a stock quote custom URL provider could be accessed. The Web module resource reference, myQuoteUrl, is bound to a URL resource with JNDI name...
url/QuoteUrl
...and URL...
quote://IBM
The custom URL provider will access an online stock quote for IBM.
javax.naming.InitialContext ctx = new javax.naming.InitialContext(); javax.naming.Context env = (javax.naming.Context) ctx.lookup("java:comp/env"); java.net.URL url = (java.net.URL) env.lookup("myQuoteUrl"); out.println("The stock price is "+url.getContent());Each appserver's name space is initialized on startup. This means appservers must be restarted to load a modified resource property, such as a URL string.