Fitzwilliam的专栏

Losers always whine about their best; Winners go home and fuck the prom queen.

[示例代码]通用JavaBean资源配置(Generic JavaBean Resources)

我的环境(Eclipse3.0)如下图所示:

 

代码如下:

1.Mybean:


package com.mycompany;


public class MyBean {
private String foo="default foo";

/**
 * @return Returns the foo.
 */
public String getFoo() {
    return foo;
}
/**
 * @param foo The foo to set.
 */
public void setFoo(String foo) {
    this.foo = foo;
}

private int bar=0;

/**
 * @return Returns the bar.
 */
public int getBar() {
    return bar;
}
/**
 * @param bar The bar to set.
 */
public void setBar(int bar) {
    this.bar = bar;
}
}

 

2. web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<resource-env-ref>
 <description>
  this is a test for Tom config
 </description>
 <resource-env-ref-name>
  bean/MyBeanFactory
 </resource-env-ref-name>
 <resource-env-ref-type>
  com.mycompany.MyBean
 </resource-env-ref-type>
</resource-env-ref>
</web-app>

 

3. tom.xml (这个名字和发布的应用的名字相同)

<?xml version='1.0' encoding='utf-8'?>
<Context docBase="/tom" path="/tom" reloadable="true" workDir="/tom/work">
  <Resource name="bean/MyBeanFactory" type="com.mycompany.MyBean" auth="container"/>
  <ResourceParams name="bean/MyBeanFactory">
  <parameter>
  <name>factory</name>
  <value>org.apache.naming.factory.BeanFactory</value>
  </parameter>
  <parameter>
  <name>bar</name>
  <value>23</value>
  </parameter>
  </ResourceParams> 
  </Context>

4.beanTest.jsp

<%@ page language="java" pageEncoding="GB2312" %>
<%@page import="com.mycompany.MyBean"%>
<%@page import="javax.naming.*" %>
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>Test Tom Config</title>
</head>
<body bgcolor="#FFFFFF">

<%
 try {
            Context initctx=new InitialContext();
            Context envctx=(Context) initctx.lookup("java:comp/env");
            MyBean bean=(MyBean) envctx.lookup("bean/MyBeanFactory");
            System.out.println("Foo = "+bean.getFoo()+"; Bar = "+bean.getBar());
%>
<table width="787" border="0" cellpadding="0" cellspacing="0">
  <!--DWLayoutTable-->
  <tr>
    <td width="787" height="62" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
        <!--DWLayoutTable-->
        <tr>
          <td width="787" height="62"><h1><div align="center">Generic JavaBean Resources Test</div></h1></td>
        </tr>
        <tr>
          <td width="787" height="62"><h3><div align="right">---Fitzwilliam</div></h3></td>
        </tr>
      </table></td>
  </tr>
  <tr>
    <td height="159"><table width="75%" border="0">
        <tr>
          <td width="44%"><div align="right">Foo:</div> </td>
          <td width="56%"><%= bean.getFoo()%></td>
        </tr>
        <tr>
          <td><div align="right">Bar:</div></td>
          <td><%= bean.getBar()%></td>
        </tr>
      </table></td>
  </tr>
</table>
<%
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
%>

</body>
</html>

阅读更多
个人分类: 个人文集
上一篇Tomcat5 中JNDI Resources配置 ------ 通用JavaBean资源配置(Generic JavaBean Resources)
下一篇Tomcat5 中JNDI Resources配置 ------ JDBC 数据源配置(JDBC Data Sources)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭