1. Create Your JavaBean Class

Create the JavaBean class which will be instantiated each time that the resource factory is looked up. For this example, assume you create a class com.huey.hello.bean.HelloBean, which looks like this:

package com.huey.hello.bean;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter; @NoArgsConstructor
public class HelloBean {
@Getter @Setter
private String personName; public String sayHello() {
if (personName != null && personName.length() > 0) {
return "Hello, " + personName + "!";
}
return "Hello, world!";
}
}

2. Declare Your Resource Requirements

Next, modify your web application deployment descriptor (/WEB-INF/web.xml) to declare the JNDI name under which you will request new instances of this bean. The simplest approach is to use a <resource-env-ref> element, like this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>com.huey.hello.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/hello.html</url-pattern>
</servlet-mapping> <resource-env-ref>
<resource-env-ref-name>bean/HelloBeanFactory</resource-env-ref-name>
<resource-env-ref-type>com.huey.hello.bean.HelloBean</resource-env-ref-type>
</resource-env-ref>

</web-app>

3. Code Your Application's Use Of This Resource

A typical use of this resource environment reference might look like this:

package com.huey.hello.servlet;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.huey.hello.bean.HelloBean; public class HelloServlet extends HttpServlet { private static final long serialVersionUID = 2684083672082632268L; @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
HelloBean helloBean = (HelloBean) envCtx.lookup("bean/HelloBeanFactory"
);
resp.getWriter().println(helloBean.sayHello());

} catch (Exception e) {
e.printStackTrace();
}
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}

4. Configure Tomcat's Resource Factory

To configure Tomcat's resource factory, add an element like this to the <Context> element for this web application.

<Resource name="bean/HelloBeanFactory" auth="Container"
type="com.huey.hello.bean.HelloBean"
factory="org.apache.naming.factory.BeanFactory"
personName="huey"
/>

Note that the resource name (here, bean/HelloBeanFactory must match the value specified in the web application deployment descriptor. We are also initializing the value of the personName property, which will cause setPersonName("huey") to be called before the new bean is returned.

5. Verification

Send a http GET request to http://localhost:8080/hellotomcat/hello.html.

C:\Users\huey>curl http://localhost:8080/hellotomcat/hello.html
Hello, huey!

Tomcat - JNDI 配置的更多相关文章

  1. Springmvc +JNDI 在Tomcat下 配置数据源(转)

    一.             简介 jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务 ...

  2. Tomcat JNDI + spring配置

    http://hi.baidu.com/lzpsky/item/f9a727ba823257eb4ec7fd27 一.简介 JNDI : Java Naming and Directory Inter ...

  3. tomcat下jndi配置

    jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和对象联系起来,使得我们可以用 ...

  4. JNDI和在tomcat中配置DBCP连接池 元数据的使用 DBUtils框架的使用 多表操作

    1 JNDI和在tomcat中配置DBCP连接池 JNDI(Java Naming and Directory Interface),Java命名和目录接口,它对应于J2SE中的javax.namin ...

  5. Linux - tomcat -jndi数据源配置

    Linux - tomcat -jndi数据源配置 tomcat/conf/context .xml 文件中修改如下 <Resource name="/jdbc/--" au ...

  6. tomcat中配置servlet.xml的JNDI或JDBC连接数据库【原】

    tomcat中配置servlet.xml的JNDI或JDBC连接数据库 一. JNDI 1. tomcat环境 找到X:\xxx\......\apache-tomcat-6.0.39\conf\se ...

  7. tomcat JNDI Resource 配置

    最近公司的项目慢慢开始向Maven项目迁移, 部分配置文件公共组也帮我们做了些改动,其中在spring的applicationContext.xml中看到了数据连接bean存在两个,一个是jndi 一 ...

  8. tomcat下配置jndi数据源c3p0

    Tomcat下通过JNDI配置数据源,使用c3p0连接池 首先在打开tomcat找到在conf文件下,找到server.xml 在server.xml文件中找到标签 在下面添加如下配置 <Res ...

  9. Tomcat下配置JNDI的三种方式

    最近在整理项目上的配置文件,正好看到了数据源配置,想着配置方式有多种,便趁热打铁,记录下常规的Tomcat配置数据源的方式 1.单个工程配置 找到Tomcat下的server.xml文件,在Conte ...

随机推荐

  1. 从 ALAsset 中取出属性

    #pragma mark - 从相册数组中取出所有的 图片数据 -(NSMutableArray *)getImageFromAlbumArray:(NSArray *)albumArr { NSMu ...

  2. django控制admin的model显示列表

    class goods(models.Model):    name = models.CharField(max_length=300)    price = models.IntegerField ...

  3. SQLite数据库连接方式

    http://blog.csdn.net/ZF101201/archive/2010/05/26/5626365.aspx SQLite.NET Type:    .NET Framework Cla ...

  4. linux 一些遇到的问题

    亚马逊云服务器上的YUM源有些问题,是64位系统,安装GCC的时候要用 yum install gcc44 yum install gcc44-c++ 查看源的列表和禁用(开启)源 yum-confi ...

  5. MySQL 5.5 手册下载

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  6. node搜索codeforces 3A - Shortest path of the king

    发一下牢骚和主题无关: 搜索,最短路都可以     每日一道理 人生是洁白的画纸,我们每个人就是手握各色笔的画师:人生也是一条看不到尽头的长路,我们每个人则是人生道路的远足者:人生还像是一块神奇的土地 ...

  7. C# 使用xsd文件验证XML 格式是否正确

    C# 使用xsd文件验证XML 格式是否正确 核心示例代码: //创建xmlDocument XmlDocument doc = new XmlDocument(); //创建声明段 如<?xm ...

  8. 异步消息总线hornetq学习-03客户端连接hornet进行jms消息的收发-非jndi方式连接

    在上节中介绍了通过jndi方式连接到hornetq服务器上,有时候由于某些原因,我们不希望通过jndi方式连接,hornetq也支持这种方式进行 以第2章节的例子为模板,我们编写了另一个获取Conne ...

  9. iOS开发——图形编程OC篇&粘性动画以及果冻效果

    粘性动画以及果冻效果 在最近做个一个自定义PageControl——KYAnimatedPageControl中,我实现了CALayer的形变动画以及CALayer的弹性动画,效果先过目: 先做个提纲 ...

  10. quickstack is a tool to take call stack

    https://github.com/yoshinorim/quickstack quickstack is a tool to take call stack traces with minimal ...