URL provider sample

 

+

Search Tips   |   Advanced Search

 

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...

...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...

...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...

...and URL...

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.

Next