web.xml之资源管理对象配置 resource-env-ref
来源:互联网 发布:淘宝上的崖柏是真的吗 编辑:程序博客网 时间:2024/05/09 10:32
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;/** * @author louisliao *我的第一个Servlet */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 blocke.printStackTrace();}finally{result.close();stmt.close();connection.close();}} catch (Exception e) {// TODO: handle exception}}}
/** * */package com.myapp.domain;/** * @author louisliao * */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之资源管理对象配置 resource-env-ref
- web.xml之资源管理对象配置 resource-env-ref
- web.xml 之 resource-ref
- resource-ref和resource-env-ref
- Difference between resource-ref & resource-env-ref
- The Difference between <resource-ref> and <resource-env-ref>,the title should be long...
- web.xml的一些配置信息详解 resorce-ref
- applicationContext.xml 配置属性和对象ref指向
- mapped-name/lookup/jndi-name and any ResourceProvider could’t process resource-env-ref named
- springmvc/mybaits的web.xml配置:Could not open ServletContext resource [/WEB-INF/springmvc-servlet.xml]
- web.xml之Servlet配置
- web.xml之配置Struts
- SSH配置之web.xml
- SSH配置之web.xml .
- springmvc之web.xml配置
- SSH配置之web.xml .
- SSH配置之web.xml
- SSH配置之web.xml
- .net中反序列化json对象方法(使用Dynamic)
- C#泛型集合揽胜
- 线性链表基本操作实现(C/C++)
- MyEclipse使用SVN插件报Failed to load JavaHL Library.错误
- Java基本数据类型
- web.xml之资源管理对象配置 resource-env-ref
- c# treeView 绑定集合
- sleep study
- 最长公共子序列(LCS)问题(非连续子序列)的两种解法
- POJ-2983 用Bellman-Ford求解差分约束..
- 教你如何拍好人像摄影
- jquery淡入淡出效果
- 最长公共子序列(LCS)问题
- 直接访问键盘控制芯片获取键盘记录