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 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 shown in Example: Resource Reference
Example 18-3 Resource Reference
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.
In the Visual Editor, 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:
Element | Description |
---|---|
Resource Name |
Specifies the resource reference's name, such as myDataSource . |
Resource Type |
Specifies the variable's name for validation, such as java.lang.String . |
Authentification |
Specifies the source of the authentification credentials. |
Application |
Programmatic authentification, provided by the application. |
Container |
(Default) Provided by the caller who logged in to the server and created the component instance. |
Sharing Scope |
By default, connections to a resource manager are shareable across web components in an application that use the same resource in the same transaction context. |
Description |
Specifies a description, such as Resource Reference for My Data Source . |
Related Topics
Developing Applications with NetBeans IDE,
Developing Applications with NetBeans IDE,
Copyright © 2015, Oracle and/or its affiliates. All rights reserved. |