Tomcat - JNDI 配置
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 配置的更多相关文章
- Springmvc +JNDI 在Tomcat下 配置数据源(转)
一. 简介 jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务 ...
- Tomcat JNDI + spring配置
http://hi.baidu.com/lzpsky/item/f9a727ba823257eb4ec7fd27 一.简介 JNDI : Java Naming and Directory Inter ...
- tomcat下jndi配置
jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和对象联系起来,使得我们可以用 ...
- JNDI和在tomcat中配置DBCP连接池 元数据的使用 DBUtils框架的使用 多表操作
1 JNDI和在tomcat中配置DBCP连接池 JNDI(Java Naming and Directory Interface),Java命名和目录接口,它对应于J2SE中的javax.namin ...
- Linux - tomcat -jndi数据源配置
Linux - tomcat -jndi数据源配置 tomcat/conf/context .xml 文件中修改如下 <Resource name="/jdbc/--" au ...
- tomcat中配置servlet.xml的JNDI或JDBC连接数据库【原】
tomcat中配置servlet.xml的JNDI或JDBC连接数据库 一. JNDI 1. tomcat环境 找到X:\xxx\......\apache-tomcat-6.0.39\conf\se ...
- tomcat JNDI Resource 配置
最近公司的项目慢慢开始向Maven项目迁移, 部分配置文件公共组也帮我们做了些改动,其中在spring的applicationContext.xml中看到了数据连接bean存在两个,一个是jndi 一 ...
- tomcat下配置jndi数据源c3p0
Tomcat下通过JNDI配置数据源,使用c3p0连接池 首先在打开tomcat找到在conf文件下,找到server.xml 在server.xml文件中找到标签 在下面添加如下配置 <Res ...
- Tomcat下配置JNDI的三种方式
最近在整理项目上的配置文件,正好看到了数据源配置,想着配置方式有多种,便趁热打铁,记录下常规的Tomcat配置数据源的方式 1.单个工程配置 找到Tomcat下的server.xml文件,在Conte ...
随机推荐
- IPhone 设备状态、闪光灯状态
//判断闪光灯状态,修改默认的"CameraFlashOff" 按钮图片.转由 TGCameraFlash.m 控制图标切换 AVCaptureDevice *device ...
- Bootstrap迁移系列 - Navbar
在V2.3.2版本中一个标准的导航栏模版如下: <div class="navbar"> <div class="navbar-inner"& ...
- iOS-default.png启动图片
我在xcode5下写的代码,我下载了iOS6的模拟器,我用iOS6和iOS7的模拟器切换运行,有的时候可以运行有的时候不可以运行,报错: 2013-11-17 16:49:04.049 sim[474 ...
- android之多媒体篇(三)
录像 Android提供了2种方案去录像. 方案一: 最简单的方式就是使用Intents去启动App来帮助你完成.这个方案使你能够指定输出的位置和视频的质量.这方案通常是最好的方法,应该可以用在多种情 ...
- iOS开发——设备篇Swift篇&判断设备类型
判断设备类型 1,分割视图控制器(UISplitViewController) 在iPhone应用中,使用导航控制器由上一层界面进入下一层界面. 但iPad屏幕较大,通常使用SplitViewCo ...
- 更新mac自带的python
mac lion自带的python是2.7版本的,有点儿低,要使用一些应用的时候还需要3.3以上的,这时可以用以下的方法更新mac自带的python. 先去python的官网下载最新的python,并 ...
- LeetCode: Palindrome Partition
LeetCode: Palindrome Partition Given a string s, partition s such that every substring of the partit ...
- js new date详解
创建一个日期对象: var objDate=new Date([arguments list]); var ini_date=new Date(2014,7,0); //是代表7月最后一天 ,也就是2 ...
- c++之函数原型
函数原型: [作用域] [函数连接规范] 返回值类型 [函数调用规范] 函数名 (类型1[形参1]...); 函数定义: [函数连接规范] 返回值类型 [函数调用规范] 函数名 (形参列表) {... ...
- python(5)-正则表达式
数量词的贪婪模式与非贪婪模式 正则表达式通常用于在文本中查找匹配的字符串.Python里数量词默认是贪婪的(在少数语言里也可能是默认非贪婪),总是尝试匹配尽可能多的字符:非贪婪的则相反,总是尝试匹配尽 ...