修改配置
Tomcat\conf\context.xml
<Context>
<Environment name="tjndi" value="hello JNDI" type="java.lang.String" />
</Context>
操作
输出配置中的数据
<%
InitialContext ctx = new InitialContext();
Object resObj = ctx.lookup("java:comp/env/tjndi");
String resStr = (String) resObj;
out.print("结果为:" + resStr);
%>
注意事项
刚刚我们明明配的是对的
但是多次都出错
最终不改代码
重启了服务后
就正常了
有时候就是这个样子
大家需要注意这个现象
不要因为报错
就把明明是对的操作,又改成错误的了
jndi的更名
<Environment name="pyhui" value="hello222 JNDI baby222" type="java.lang.String" />
配置中
name的值是获取的名称
注意对应关系
项目中的jndi
上面配的是整个tomcat的总配置
影响太大了
我们要给自己的项目来一个单独的配置
项目中新建目录
拷备配置到项目中
代码如下
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>
<!-- Default set of monitored resources. If one of these changes, the -->
<!-- web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<Environment name="javahui" value="look at here" type="java.lang.String" />
</Context>
小结
可以给自己的项目单独的配置jndi的数据的
需要在web目录下新建
meta-inf目录
在下面新建context.xml配置(可拷备总配置过来)
编写需要的代码即可
当前的这个文件仅对当前的应用是有效的