[示例代码]通用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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值