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. AVCaptureDevice的几个属性

    AVCaptureDevice.h,主要用来获取iphone一些关于相机设备的属性. AVCaptureDevice.h,必须要引入AVFoundation.framework包. 1. 前置和后置摄 ...

  2. 转载“启动\关闭Oracle数据库的多种方法”--来自百度#Oracle

    启动\关闭Oracle数据库的多种方法 启动和关闭oracle有很多种方法. 这里只给出3种方法: l         Sql*plus l         OEM控制台 l         Wind ...

  3. TCP四种定时器--学习笔记

    TCP使用四种定时器: 重传定时器(Retransmission Timer).坚持定时器(Persistent Timer).保活定时器(Keeplive Timer).时间等待定时器(Time_W ...

  4. andriod开发,简单的封装网络请求并监听返回.

    一.为什么封装 因为android 4.0 以后的发送网络请求必须要放到异步线程中,而异步线程必须跟handle合作才能更新主线程中的UI,所以建议用一个类继承handler来异步处理网络请求. 二. ...

  5. C# WebService的简单和复杂参数类型和结果的JSON格式

    Jquery作为一款优秀的JS框架,简单易用的特性就不必说了.在实际的开发过程中,使用JQ的AJAX函数调用WebService 的接口实现AJAX的功能也成了一种比较普遍的技术手段了.WebServ ...

  6. xcode7.3 iTunes Store operation failed问题

    升级了7.3,真心的不好用啊,bug一堆,写个代码,引入的类根本找不到,必须要command+b 重新编译一遍,现在连提交appstore都有问题. 果断用了 Application Loader上传 ...

  7. IDHttp的基本用法(转)

    一.IDHTTP的基本用法 IDHttp和WebBrowser一样,都可以实现抓取远端网页的功能,但是http方式更快.更节约资源,缺点是需要手动维护cook,连接等 IDHttp的创建,需要引入ID ...

  8. Android应用开发多语言drawable目录

    Android程序多语言的支持是就该Value目录不同国家的区分,前面文档有介绍相相应的国家名称Value怎样写 例如以下说下多语言图片的替换也是就该图片目录目录drawable目录例如以下: Bac ...

  9. ZOJ 1151 Word Reversal反转单词 (string字符串处理)

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=151 For each list of words, output a l ...

  10. C# 手动读写app config 的源码

    public class ConfigOperator { public string strFileName; public string configName; public string con ...