`

web.xml之资源管理对象配置 resource-env-ref

阅读更多

resource-env-ref元素来指定对管理对象的servlet引用的声明,该对象与servlet环境中的资源相关联

<resource-env-ref>
    <resource-env-ref-name>资源名</resource-env-ref-name>
    <resource-env-ref-type>查找资源时返回的资源类名</resource-env-ref-type>
</resource-env-ref>

<resource-env-ref-name>:资源的名称 相对于java:comp/env >>context

<resource-env-ref-type>:当web应用查找该资源的时候,返回的Java类名的全称

 

建立步骤:

一.先在meta-inf目录下新建一个Context.xml文件配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
<!--对于javax.sql.DataSource资源类型 Tomcat容器提供了默认factory org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory -->
<Resource name="jdbc/mssql" auth="Container"
          type="javax.sql.DataSource" driverClassName="net.sourceforge.jtds.jdbc.Driver"
          url="jdbc:jtds:sqlserver://127.0.0.1:1433;DatabaseName=spring3mvc"
          username="sa" password="wlyoa_)*#!" >
</Resource>
<Resource name="bean/MyBeanFactory" auth="Container"
          type="com.myapp.domain.MyBean"  factory="org.apache.naming.factory.BeanFactory" projectName="我的测试项目">
</Resource>
</Context>

二.在web.xml中添加资源引用

<!--定义外部资源管理对象 在Web容器目录的conf\Catalina\localhost\工程名.xml文件中(可见示例文件:WebContext.xml) -->
<resource-env-ref>
  <resource-env-ref-name>bean/MyBeanFactory</resource-env-ref-name>
  <resource-env-ref-type>com.myapp.domain.MyBean</resource-env-ref-type>
</resource-env-ref>
<resource-env-ref>
  <resource-env-ref-name>jdbc/mssql</resource-env-ref-name>
  <resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
</resource-env-ref>

三.在Servlet类中可通过如下方式查找或使用资源

package com.myapp.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import com.myapp.domain.MyBean;

/**
 * 
 */
public class MyFirstServlet extends HttpServlet {

	private String servletInitParam="";
	
	public MyFirstServlet() {
		// TODO Auto-generated constructor stub
	}

	public void init(){
		servletInitParam=this.getInitParameter("ServletInitParam");
	}
	public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
		
		testDataSourceJNDI();
		testBeanSourceJNDI();
		String parame1=getServletContext().getInitParameter("MyContextParam");
		String projectName=getServletContext().getInitParameter("ProjectName");
		System.out.println("上下文初始化参数1:"+parame1);
		System.out.println("上下文初始化参数2:"+projectName);
		PrintWriter writer=response.getWriter();
		writer.println("<html><head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>");
		writer.println(projectName);
		writer.println("</title></head>");
		writer.println("<body>");
		writer.println(servletInitParam+":"+servletInitParam+"<br/>");
		writer.println("上下文初始化参数1:"+parame1+"<br/>");
		writer.println("上下文初始化参数2:"+projectName+"<br/>");
		writer.println("</body></html>");
	}
	public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
		doGet(request, response);
	}
	public void destroy() {
		super.destroy();
		System.out.println("我是destroy()方法!用来进行销毁实例的工作");
	}
	/**
	 * Java-Bean资源读取测试
	 */
	private void testBeanSourceJNDI()
	{
		try {
			System.err.println("testBeanSourceJNDI");
			Context context=new InitialContext();
			Context envContext=(Context)context.lookup("java:/comp/env");
			MyBean mBean=(MyBean)envContext.lookup("bean/MyBeanFactory");
			System.out.println(mBean.getHello()+":"+mBean.getProjectName());
			
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	/**
	 * DataSource资源测试
	 */
	private void testDataSourceJNDI() 
	{
		try {
			
		System.err.println("DataSourceJNDI");
		Connection connection=null;
	    ResultSet result = null;
	    Statement stmt = null;
		Context context=new InitialContext();
		Context envContext=(Context)context.lookup("java:/comp/env");
		DataSource dataSource=(DataSource)envContext.lookup("jdbc/mssql");
		try {
			connection=dataSource.getConnection();
			stmt=connection.createStatement();
			result=stmt.executeQuery("select * from Author");
			while (result.next()) {
				System.out.println(result.getString("name"));
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			result.close();
			stmt.close();
			connection.close();
		}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}

 

package com.myapp.domain;

/**
 *
 */
public class MyBean {
private String projectName;
private String hello="欢迎使用";
public String getProjectName() {
	return projectName;
}
public void setProjectName(String projectName) {
	this.projectName = projectName;
}
public String getHello() {
	return hello;
}
public void setHello(String hello) {
	this.hello = hello;
}
}
分享到:
评论

相关推荐

    web.xml配置详解

    ELEMENT resource-env-ref (resource-env-ref-name, resource-env-ref-type) &gt;。 resource-ref 元素用于定义资源引用,&lt;!ELEMENT resource-ref (res-ref-name, res-type, res-auth) &gt;。security-constraint 元素...

    web.xml配置详解, web.xml web.xml 配置实例

    * resource-env-ref 元素:声明与资源相关的一个管理对象。 * resource-ref 元素:声明一个资源工厂使用的外部资源。 * security-constraint 元素:制定应该保护的 URL。它与 login-config 元素联合使用。 web.xml ...

    WEB-INF中的web.xml中之前增加以下配置:

    在web.xml文件中,添加了resource-ref配置:&lt;resource-ref&gt; &lt;description&gt;SQL Server Datasource&lt;/description&gt; &lt;res-ref-name&gt;jdbc/DBUtil&lt;/res-ref-name&gt; &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt; &lt;res-auth&gt;...

    Web.xml常用元素

    &lt;resource-env-ref-type&gt;javax.sql.DataSource&lt;/resource-env-ref-type&gt; &lt;/resource-env-ref&gt; ``` #### &lt;resource-ref&gt; - **作用**:声明一个外部资源的引用,通常用于数据库连接池或邮件会话。 - **示例**: ...

    J2EE中关于web.xml文件的配置

    15. `&lt;resource-env-ref&gt;`:resource-env-ref 元素用于声明与资源相关的一个管理对象。 16. `&lt;resource-ref&gt;`:resource-ref 元素用于声明一个资源工厂使用的外部资源。 17. `&lt;security-constraint&gt;`:security-...

    web.xml详细配置说明

    - **resource-env-ref**:声明与资源相关的管理对象。 - **resource-ref**:声明外部资源引用,如数据库连接。 - **security-constraint**:定义需要保护的URL,配合`login-config`进行安全控制。 - **login-...

    web.xml详细说明

    &lt;resource-env-ref-name&gt;jdbc/myDataSource&lt;/resource-env-ref-name&gt; &lt;/resource-env-ref&gt; ``` 15. **`&lt;resource-ref&gt;`** - **作用**:声明外部资源的引用。 - **示例**: ```xml &lt;resource-ref&gt; ...

    web.xml详解(txt)

    &lt;resource-env-ref-type&gt;javax.sql.DataSource&lt;/resource-env-ref-type&gt; &lt;/resource-env-ref&gt; ``` ##### 2.16 `&lt;resource-ref&gt;` - **定义**:引用一个外部资源。 - **用途**:如数据库连接池等。 - **属性**: ...

    java web项目 web.xml配置详解

    在Java Web开发中,`web.xml`文件是应用的核心配置文件,它定义了Servlet、过滤器、监听器等组件,并负责映射它们的行为。本文将深入解析`web.xml`配置,帮助开发者理解其背后的原理和用途。 一、web.xml文件概述 `...

    关于Web.xml配置说明

    【Web.xml配置说明】 在B/S(Browser/Server,浏览器/服务器)项目中,Web.xml文件扮演着核心角色,它是Web应用程序的部署描述符。它包含了一系列配置信息,用于指导服务器如何运行和管理Web应用。以下是对Web.xml...

    Web.xml配置详解精华

    - `resource-env-ref`: 声明与资源相关的管理对象。 - `resource-ref`: 声明外部资源。 - `security-constraint`: 指定需要保护的URL路径。 - `login-config`: 配置认证机制,用于用户登录验证。 - `security-role`:...

    web.xml的配置

    - **`resource-env-ref`**:声明与资源相关的管理对象,用于获取环境资源。 - **`resource-ref`**:声明外部资源,如数据库连接池,便于应用统一管理。 - **`security-constraint`**:定义需要保护的URL,配合`...

    一篇关于web.xml配置的详细说明

    - `&lt;resource-env-ref&gt;`: 声明与资源相关的管理对象。 - `&lt;resource-ref&gt;`: 声明外部资源工厂。 - `&lt;security-constraint&gt;`: 定义URL的访问权限。 - `&lt;login-config&gt;`: 配置身份验证机制和授权信息。 3. **具体...

    web.xml详解(web-app_2_3.dtd)

    &lt;resource-env-ref-type&gt;javax.sql.DataSource&lt;/resource-env-ref-type&gt; &lt;/resource-env-ref&gt; ``` #### 17. `resource-ref`元素 `resource-ref`元素用于引用外部资源,如数据库连接池或消息队列。DTD定义如下: ...

    Web.xml配置详解

    14. `resource-env-ref`和`resource-ref`元素分别声明资源环境引用和资源工厂引用,用于管理外部资源。 15. `security-constraint`和`login-config`元素协同工作,实现Web应用的安全控制,如访问限制和身份验证。 ...

    web.xml+详细解析四合一

    最后,web.xml文件也支持可扩展性,允许通过taglib元素声明自定义标签库的位置,以及通过resource-ref声明外部资源工厂。security-constraint和login-config则用于安全配置,如用户认证方式和保护特定资源。 了解和...

    web.xml元素详解

    `resource-env-ref`元素用于引用环境资源,如数据源、消息目的地等,以便在应用中方便地使用这些资源。 #### 17. Resource-Ref `resource-ref`元素用于引用应用外部的资源,如数据库连接池,确保资源的复用性和...

Global site tag (gtag.js) - Google Analytics