创建和配置 Web 2.5 和 Web 3.0 的资源引用

您可以使用部署描述符来创建和配置 Web 2.5 和 Web 3.0 项目的资源引用。

关于此任务

需要先使用 Web 2.5 和 Web 3.0 创建一个 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. 要使用名为 USER_AUTH 的 JAAS 认证别名通过 JNDI 名称 jdbc/SAMPLE 将此资源引用绑定至服务器上的某个数据源,请编辑 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>
指示主题类型的图标 任务主题
信息中心的条款和条件 | 反馈

时间戳记图标 最近一次更新时间: 2014 年 4 月 17 日

文件名:tcreateconfigresref.html