Use the Resource References section to add, remove, and view the web application's resource references.
To access a data source, a JMS connection factory, a JavaMail session, or a URL link from a web application, you must declare a resource reference in the application's web application deployment descriptor. The resource reference specifies a JNDI name, the object type, and the kind of authentication used when the resource is accessed. To connect the resource reference to the resource, you must map the JNDI name of the former to the latter. This mapping is done in the server-specific deployment descriptor file, such as the sun-web.xml file for the Glassfish application server.
You use the resource reference in your code to to extract the resource you need. For example, a resource reference defined in the web.xml file is used to extract data from a data source as follows:
public BookDBAO () throws Exception { try { Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); DataSource ds = (DataSource) envCtx.lookup("jdbc/BookDB"); con = ds.getConnection(); System.out.println("Created connection to database."); } catch (Exception ex) { System.out.println("Couldn't create connection." + ex.getMessage()); throw new Exception("Couldn't open connection to database: " + ex.getMessage()); }
To declare a resource reference, you must register it in the web.xml file. When you do this, you provide information needed by the server to use the resource reference.
You open the web.xml Visual Editor from the Projects window by expanding the Web Pages node, then the WEB-INF node, and then double-clicking the web.xml file. Click References at the top of the editor to open the References section, then click the Resource References header to open the Resource References section.
Click Add to define a resource reference element with the following properties: