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; } }