Creating and configuring resource references for Web 2.5, Web 3.0, Web 3.1, and Web 4.0

You can create and configure resource references for Web 2.5, Web 3.0, Web 3.1, and Web 4.0 projects using the deployment descriptor.

About this task

You need to create a web project using Web 2.5, Web 3.0, Web 3.1, and Web 4.0 before you can create and configure resources references. Select Generate deployment descriptor in the project creation wizard.

Procedure

  1. Expand your web project, and select WebContent > WEB-INF > web.xml.
  2. Right-click web.xml and select Open with > Web Application Deployment Descriptor Editor.
  3. In the Web application field, select Add and select Resource Reference and click OK:
    Add resource reference
  4. In the Details section, provide the details for your resource reference:
    1. In the Name field, provide a name for your resource reference.
    2. In the Type field, provide the type of resource reference.
    3. In the Authentication field, select either Application or Container for the authentication of your resource reference.
    4. In the Sharing Scope field, select either Shareable or Unshareable for the sharing scope of your resource reference.
    5. In the Description field, type a description of this resource reference.
  5. To view the web.xml source code, select Source:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">	
    <servlet>
    		<description>
    		</description>
    		<display-name>
    		TestServlet</display-name>
    		<servlet-name>TestServlet</servlet-name>
    		<servlet-class>test.TestServlet</servlet-class>
    	</servlet>
    	<servlet-mapping>
    		<servlet-name>TestServlet</servlet-name>
    		<url-pattern>
    		/TestServlet</url-pattern>
    	</servlet-mapping>
    	<welcome-file-list>
    		<welcome-file>index.html</welcome-file>
    		<welcome-file>index.htm</welcome-file>
    		<welcome-file>index.jsp</welcome-file>
    		<welcome-file>default.html</welcome-file>
    		<welcome-file>default.htm</welcome-file>
    		<welcome-file>default.jsp</welcome-file>
    	</welcome-file-list>
    	<resource-ref>
    		<res-ref-name>SAMPLE</res-ref-name>
    		<res-type>javax.sql.DataSource</res-type>
    		<res-auth>Container</res-auth>
    		<res-sharing-scope>Shareable</res-sharing-scope>
    	</resource-ref>
    </web-app>
  6. To edit your resource reference, you can make changes in either the Design or the Source view of the deployment descriptor page.
  7. To bind this resource reference to a data source on the server, with the JNDI name jdbc/SAMPLE using a JAAS authentication alias called USER_AUTH, edit the ibm-web-bnd.xml file and add the following definitions:
    1. If the ibm-web-bnd.xml file does not exist, create the file by right-clicking on the project and then selecting Java EE tools > Generate WebSphere Bindings Deployment Descriptor.
    2. Right-click the ibm-web-bnd.xml file and select Open with > Web Bindings Editor.
    3. Add a resource reference:
      1. In the Design view, click Add.
      2. In the Add Item window, select Resource Reference. Click OK.
      3. In the Name field, type the name of your resource reference, for example: SAMPLE.
      4. In the Binding Name field, type the name of your resource reference, for example: jdbc/SAMPLE.
    4. In the Design view, highlight your Resource Reference, and click Add > Authentication Alias.
    5. In the Details section, in the Name field, provide a name for your Authentication Alias (for example, use the JAAS authentication alias called USER_AUTH).
      Add JNDI binding
    6. Save the file.
    7. To view the ibm-web-bnd.xml source code, select Source:
      <?xml version="1.0" encoding="UTF-8"?>
      <web-bnd 
      	xmlns="http://websphere.ibm.com/xml/ns/javaee"
      	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      	xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-web-bnd_1_0.xsd"
      	version="1.0">
      
      	<virtual-host name="default_host" />
      
      	<resource-ref name="SAMPLE" binding-name="jdbc/SAMPLE">
      		<authentication-alias name="USER_AUTH" />
      	</resource-ref>
      </web-bnd>