建立及配置 Web 2.5、Web 3.0 和 Web 3.1 的資源參照

您可以使用部署描述子,來建立及配置 Web 2.5、Web 3.0 和 Web 3.1 專案的資源參照。

關於這項作業

您必須先使用 Web 2.5、Web 3.0 和 Web 3.1 來建立 Web 專案,才能建立及配置資源參照。請在專案建立精靈中,選取產生部署描述子。

程序

  1. 展開您的 Web 專案,並選取 WebContent > WEB-INF > web.xml。
  2. 用滑鼠右鍵按一下 web.xml,並選取開啟工具 > Web 應用程式部署描述子編輯器。
  3. 在「Web 應用程式」欄位中,選取新增,並選取資源參照,然後按一下確定:
    新增資源參照
  4. 在詳細資料區段中,提供資源參照的詳細資料:
    1. 在名稱欄位中,提供資源參照的名稱。
    2. 在類型欄位中,提供資源參照的類型。
    3. 在鑑別欄位中,選取應用程式或儲存器,以用來鑑別資源參照。
    4. 在共用範圍欄位中,針對您資源參照的共用範圍,選取可共用或不能共用。
    5. 在說明欄位中,輸入此資源參照的說明。
  5. 如果要檢視 web.xml 原始碼,請選取原始檔:
    <?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. 如果要編輯資源參照,可在部署描述子頁面的「設計」或「程式碼」視圖中進行變更。
  7. 如果要透過 JNDI 名稱 jdbc/SAMPLE,將這項資源參照連結至伺服器上的資料來源,並使用 JAAS 鑑別別名 USER_AUTH,請編輯 ibm-web-bnd.xml 檔,並新增下列定義:
    1. 如果 ibm-web-bnd.xml 檔不存在,請用滑鼠右鍵按一下該專案,然後選取 Java EE 工具 > 產生 WebSphere 連結部署描述子,來建立該檔案。
    2. 用滑鼠右鍵按一下 ibm-web-bnd.xml 檔,並選取開啟工具 > Web 連結編輯器。
    3. 新增資源參照:
      1. 在「設計」視圖中,按一下新增。
      2. 在「新增項目」視窗中,選取資源參照。按一下確定。
      3. 在名稱欄位中,輸入資源參照的名稱,例如:SAMPLE。
      4. 在連結名稱欄位中,輸入資源參照的名稱,例如:jdbc/SAMPLE。
    4. 在「設計」視圖中,強調顯示您的「資源參照」,並按一下新增 > 鑑別別名。
    5. 在「詳細資料」區段的名稱欄位中,提供「鑑別別名」的名稱(例如,使用稱為 USER_AUTH 的 JAAS 鑑別別名)。
      新增 JNDI 連結
    6. 儲存檔案。
    7. 如果要檢視 ibm-web-bnd.xml 原始碼,請選取原始檔:
      <?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>
Icon that indicates the type of topic Task topic
Timestamp icon Last updated: last_date

File name: tcreateconfigresref.html