`

JNDI

 
阅读更多

新建web应用

配置JNDI

server.xm l

 <Context docBase="JNDITest" path="/JNDITest" reloadable="true" source="org.eclipse.jst.jee.server:JNDITest">
                             <!-- Configure the name and data type of a resource made available to the application (equivalent to the inclusion of a <resource-ref> element in the web application deployment descriptor).
                             configure Tomcat's resource factory
                            
                              <Resource name="jdbc/oa"
                                        auth="Container"
                                        type="javax.sql.DataSource"
                                        username="root"
                                        password="root"
                                        driverClassName="com.mysql.jdbc.Driver"
                                        url="jdbc:mysql://127.0.0.1:3306/oa"
                                        maxActive="8"
                                        maxIdle="4"/>
              -->
              
              
                <!-- Tomcat 6 includes a series of standard resource factories that can provide services to your web applications
                Of the standard resource factories, only the "JDBC Data Source" and "User Transaction" factories are mandated to be available on other platforms, and then they are required only if the platform implements the Java2 Enterprise Edition (J2EE) specs. All other standard resource factories, plus custom resource factories that you write yourself, are specific to Tomcat and cannot be assumed to be available on other containers.
                       
                        在server.xml中配置可以,由于server.xml在启动时加载,Tomcat6可配置在context.xml文件中,Tomcat
                        6会定时去查看该文件,判断时间撮,重新加载context.xml文件

                        <Resource name="bean/MyBeanFactory" auth="Container"
                                        type="com.morning.learn.j2ee.jndi.MyBean"
                                        factory="org.apache.naming.factory.BeanFactory"
                                        bar="23"/>
                 -->
      </Context>
     
      </Host>

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>JNDITest</display-name>
 
  <!-- 资源引用,通常是资源(如JDBC DataSource,JavaMail Session)的对象工厂,或者在Tomcat6中配置的定制对象工厂
          custom object factories configured into Tomcat 6
   -->

   <!-- a new variation of resource-ref added in Servlet 2.4 that is simpler to configure for resources that do not require authentication information -->
   <resource-env-ref>
      <description>Object factory for MyBean instances</description>
      <resource-env-ref-name>bean/MyBeanFactory</resource-env-ref-name>
      <resource-env-ref-type>com.morning.learn.j2ee.jndi.MyBean</resource-env-ref-type>
  </resource-env-ref>
 
  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/oa</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
 
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

context.xml

<?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 reloadable="true">

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
   
    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->
   
    <!-- 全局资源定义 -->
    <Resource name="bean/MyBeanFactory" auth="Container"
                                    type="com.morning.learn.j2ee.jndi.MyBean"
                                    factory="org.apache.naming.factory.BeanFactory"
                                    bar="888"/>
                                   
     <Resource name="jdbc/oa"
                                        auth="Container"
                                        type="javax.sql.DataSource"
                                        username="root"
                                        password="root"
                                        driverClassName="com.mysql.jdbc.Driver"
                                        url="jdbc:mysql://127.0.0.1:3306/oa"
                                        maxActive="8"
                                        maxIdle="4"/>
</Context>

 

jndiTest.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="javax.naming.*" %>
<%@ page import="com.morning.learn.j2ee.jndi.*"%>
<%@ page import="javax.sql.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Cache-Control" content="no-store"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="0"/>
<title>Insert title here</title>
</head>
<body>
<%-- 编写需要使用JNDI资源的代码 --%>
<%
//通过JNDI默认的命名空间初始化JNDI上下文
Context initctx = new InitialContext();
Context envCtx = (Context) initctx.lookup("java:comp/env");

//通过JNDI服务查找JNDI树上的资源
DataSource ds= (DataSource) envCtx.lookup("jdbc/oa");
System.out.println(ds.getConnection());

MyBean bean= (MyBean) envCtx.lookup("bean/MyBeanFactory");
System.out.println(" bar: " + bean.getBar());

%>

ccadfs

</body>
</html>

分享到:
评论

相关推荐

    jndi-1_2_1.zip_jndi_jndi-1.2.1.jar

    标题中的"jndi-1_2_1.zip_jndi_jndi-1.2.1.jar"表明这是一个关于JNDI的版本1.2.1的开源软件包,其中包含了一个名为"jndi-1.2.1.jar"的JAR文件。这个JAR文件包含了JNDI库的所有实现,使得开发者可以在他们的Java项目...

    jndi-tool JNDI服务利用工具

    "jndi-tool"是一个专门针对JNDI服务的利用工具,它可以被用来测试和利用JNDI相关的安全漏洞。此工具特别关注RMI和LDAP服务,这两种服务是JNDI常用的两种协议。RMI允许Java对象在不同的Java虚拟机之间进行远程调用,...

    jndi-JNDI-Injection-Exploit

    java asm jndi_JNDI-Injection-Exploit,用于log4j2漏洞验证 可执行程序为jar包,在命令行中运行以下命令: $ java -jar JNDI-Injection-Exploit-1.0-SNAPSHOT-all.jar [-C] [command] [-A] [address] 其中: -C ...

    Tomcat中JNDI原理

    ### Tomcat中JNDI原理详解 #### 一、引言 Java Naming and Directory Interface (JNDI) 是Java平台中用于访问命名和目录服务的API。它为开发者提供了访问各种命名和目录服务(如DNS、LDAP等)的统一接口。在Tomcat...

    jndi所依赖的jar包

    Java Naming and Directory Interface (JNDI) 是Java平台中用于访问命名和目录服务的API,它为各种不同的命名和目录服务提供了统一的接口。在Java应用程序中,JNDI允许开发者查找和绑定对象,这些对象可以是数据源、...

    JNDI-Injection-Exploit-1.0-SNAPSHOT-all

    《深入解析JNDI注入攻击与防御》 Java Naming and Directory Interface (JNDI) 是Java平台中用于访问各种命名和目录服务的API,它提供了一种统一的方式来查找和访问不同类型的网络资源,如数据库、对象、服务器等。...

    jboss配置MySql的JNDI

    在IT领域,特别是Java应用服务器环境下,JBoss与MySQL数据库的集成是常见需求之一,而JNDI(Java Naming and Directory Interface)在此过程中扮演着关键角色。本文将深入解析如何在JBoss中配置MySQL的JNDI数据源,...

    spring获取weblogic jndi数据源的两种方式

    ### Spring 获取 WebLogic JNDI 数据源的两种方式 在Spring框架中,通过JNDI(Java Naming and Directory Interface)可以方便地访问WebLogic服务器中的数据源。这为应用程序提供了高度解耦的数据访问机制,使得...

    JNDIDemo 以及相关文档

    Java Naming and Directory Interface (JNDI) 是Java平台中用于访问命名和目录服务的一组API。JNDI允许Java应用程序查找和操作各种类型的资源,如数据源、对象服务、邮件服务器等,而无需知道这些资源的具体实现细节...

    用JNDI绑定DataSource

    在Java应用中,JNDI(Java Naming and Directory Interface)是一种标准的接口,它允许应用程序查找和使用资源,如数据源(DataSource),而无需直接在代码中硬编码这些资源的位置或配置信息。JNDI的核心理念是将...

    JNDI-Inject-Exploit

    # JNDI-Inject-Exploit ## 免责声明 本工具仅面向**合法授权的企业安全测试**,如您需测试本工具的可用性请自行搭建靶机环境,在使用本工具进行检测时,您应确保该行为符合当地的法律法规,并且已经取得了足够的...

    JNDI示例整合SSH

    **标题:“JNDI示例整合SSH”** 在IT领域,`JNDI`(Java Naming and Directory Interface)是Java平台提供的一种标准接口,用于访问命名和目录服务。它允许应用程序查找和操作不同类型的网络资源,如数据源、EJB等...

    JNDI-Injection-Exploit-1.0-SNAPSHOT-all.zip

    Java Naming and Directory Interface (JNDI) 是Java平台中用于访问命名和目录服务的API,它允许程序查找和操作各种命名和目录服务,如 Lightweight Directory Access Protocol (LDAP)、Java Database Connectivity ...

    JNDI简单应用示例

    Java Naming and Directory Interface (JNDI) 是Java平台中用于访问命名和目录服务的一组API。它提供了一种标准的方法来查找和绑定数据,这些数据可以是对象引用、配置信息或者其他资源。JNDI通常与Java应用程序...

    JNDI学习文档.doc

    **Java Naming and Directory Interface (JNDI) 是Java平台中用于访问命名和目录服务的一组API。它提供了一个统一的接口来查找、访问和管理分布式系统中的资源,如数据库连接池、邮件服务器、对象服务等。JNDI的核心...

    JNDI连接数据库配置

    ### JNDI连接数据库配置详解 #### 一、引言 在Java开发中,JNDI(Java Naming and Directory Interface)是一种广泛使用的API,它允许Java应用程序查找并定位远程对象和服务,包括数据库连接等资源。本文将详细...

    com.sun.jndi.ldap.jar

    JNDI是一个接口,提供了一种标准的方式来访问命名和目录服务,而LDAP则是一种网络协议,常用于存储和查询用户账户、组信息、配置数据等结构化信息。 JNDI的主要作用是将Java应用程序与各种不同的命名和目录服务连接...

    jndi数据库查询例子

    **JNDI(Java Naming and Directory Interface)数据库查询例子** JNDI是Java平台中用于查找和绑定资源的接口,它允许程序通过名称查找数据源、对象服务等。在Java应用中,特别是企业级Java应用中,JNDI被广泛用于...

Global site tag (gtag.js) - Google Analytics