关闭

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

810人阅读 评论(0) 收藏 举报

我的环境(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>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:175840次
    • 积分:2920
    • 等级:
    • 排名:第12590名
    • 原创:96篇
    • 转载:19篇
    • 译文:12篇
    • 评论:3条