创建和配置 Web 2.5、Web 3.0 及 Web 3.1 的资源引用
您可以使用部署描述符来创建和配置 Web 2.5、Web 3.0 及 Web 3.1 项目的资源引用。
关于此任务
过程
- 展开 Web 项目,然后选择 WebContent > WEB-INF > web.xml。
- 右键单击 web.xml 并选择打开方式 > Web 应用程序部署描述符编辑器。
- 在 Web 应用程序字段中,选择添加并选择资源引用,然后单击确定:

- 在详细信息部分中,提供资源引用的详细信息:
- 在名称字段中,提供资源引用的名称。
- 在类型字段中,提供资源引用的类型。
- 在认证字段中,对资源引用的认证选择应用程序或容器。
- 在共享作用域字段中,对资源引用的共享作用域选择可共享或不可共享。
- 在描述字段中,输入此资源引用的描述。
- 要查看 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> - 要编辑资源引用,您可以在部署描述符页面的“设计”视图或“源代码”视图中进行更改。
- 要使用名为 USER_AUTH 的 JAAS 认证别名通过 JNDI 名称 jdbc/SAMPLE 将此资源引用绑定至服务器上的某个数据源,请编辑 ibm-web-bnd.xml 文件并添加以下定义:
- 如果 ibm-web-bnd.xml 文件不存在,请通过右键单击该项目,然后选择 Java EE 工具 > 生成 WebSphere 绑定部署描述符来创建该文件。
- 右键单击 ibm-web-bnd.xml 文件并选择打开方式 > Web 绑定编辑器。
- 添加资源引用:
- 在“设计”视图中,单击添加。
- 在“添加项目”窗口中,选择资源引用。 单击确定。
- 在名称字段中,输入资源引用的名称,例如:SAMPLE。
- 在绑定名称字段中,输入资源引用的名称,例如:jdbc/SAMPLE。
- 在“设计”视图中,突出显示您的资源引用,然后单击添加 > 认证别名。
- 在“详细信息”部分的名称字段中,为认证别名提供一个名称(例如,使用名为 USER_AUTH 的 JAAS 认证别名)。

- 保存文件。
- 要查看 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>
父主题: 创建 Web 模块