Tomcat 5 servletJSP container JNDI resource instructions (1)

zhaozj2021-02-16  60

Tomcat 5 Servlet / JSP Container JNDI Resource Instructions (1) This time I want to do JNDI applications, I have gone a lot of curve T_T I hope everyone can take a look at the standard document first. For everyone to see it, here Translated some important text, please correct :)

Introduction introduction

Tomcat5 provides an instance of JNDI InitialContext implemented for web applications to run on it, and its style and other J2EE application servers are consistent. His registration is in the $ catalina_home / conf / server.xml file, please refer to the following elements for the deployment file (/Web-inf/web.xml):

- Environment Enter, single value parameters, is used to specify how the application is operated. - Resource Reference, typical application is a factory that points to an object, such as a JDBC data source, a JavaMail Session, or a factory that is configured to Tomcat 5. - Resource Environment Reference, this is a newly added variable in servlet2.4. It is more simply configured with resources that do not need to verify information. The primary context configuration is to be deployed as a web application, which can be accessed by the components of the web application (read only). All configuration registrations and resources will be placed under Java: Comp / ENV, which is part of JNDI namespace, and a typical access resource (in this example, it is an access to the JDBC data source) method :

// Obtain our environment naming contextContext initCtx = new InitialContext (); Context envCtx = (Context) initCtx.lookup ( "java: comp / env"); // Look up our data sourceDataSource ds = (DataSource) envCtx.lookup ( " JDBC / Employeedb "); // allocate and use a connection from the poolconnection conn = ds.getConnection (); ... Use this connection to access the database ... conn.close ();

See the following Specifications for more information about programming APIs for JNDI, and for the features supported by Java2 Enterprise Edition (J2EE) servers, which Tomcat emulates for the services that it provides: see the following APIs For more information about the JNDI, tomcat provided The servers specified by J2EE are as follows:

Java Naming and Directory Interface (Incline Separately for Prior Jdk Versions) J2EE Platform Specification (in particular, see chapter 5 on naming)

Configuring JNDI Resources Configuring JNDI Resources

Each available JDNI resource is defined in $ catalina_home / conf / server.xml, and the elements used are as follows:

- Configure the name and value for the environment, which will be exposed to the web application through JNDI (equivalent to ) in the Web Planner. - For resources available Configuration name and data type (equivalent to included in the Web Planner). - Configuring a resource factory for Java classes, such as the property value of JavaBeans. - in the global JDNI context Add a resource link. All of these elements are placed within the element or element. In addition, all the elements included in all departments (/Web-inf/web.xml) Configured only when the element in conf / server.xml is allowed to be overloaded, the content consistent with the content is overloaded. Tomcat Standard Resource Factories

Tomcat5 provides several standard resource factories for our web applications, but it does not give you greater flexibility by modifying the application departure (in $ catalina_home / conf / server.xml). The following section Applied standard resource factories.

If you would like more about establishing, installing, configuring, using a custom resource factory, see Adding Custom Resource Factories.

Note: For standard resource plants, only "JDBC Data Source" and "User Transaction" can be used on other platforms, and they just need J2EE in the platform. All other standard resource factories, as well as your custom factory, can only be used in Tomcat, and cannot be accepted by other platforms.

General JavaBean resources

0. Introduction

This resource factory is used to establish a Java class that meets the standard JavaBeans naming specification. It has a zero parameter constructor, and some properti setters similar to setfoo () named. This plant will establish an instance of a bean every time when Lookup () is used.

Examples using this factory are as follows:

1. Create a JavaBean class to establish a class that a resource factory can implement. This example, it is recommended that you use the com.mycompany.mybean class, it should like this:

package com.mycompany; public class MyBean {private String foo = "Default Foo"; public String getFoo () {return (this.foo);} public void setFoo (String foo) {this.foo = foo;} private int bar = 0; public int getBar () {return (this.bar);} public void setbar (int bar) {this.bar = bar;}}

2. Define the resources you need, modify your application departure device (/Web-inf/web.xml) to honor the JNDI name, the easiest way is to use the element, like this:

Object Factory for MyBean Instances. bean / myBeanfactory com.mycompany.mybean Note: Element rules that the DTD defined by the application departure must be followed. See Servlet Specification for details

3. Write applications that apply this resource. A typical application of this resource is as follows:

Context ITCTX = New InitialContext (); context envctx = (context) INITCTX.LOOKUP ("Java: Comp / ENV"); MyBean Bean = (MyBean) ENVCTX.LOOKUP ("bean / mybeanfactory"); Writer.println ("foo) = " Bean.getfoo () ", bar = " bean.getbar ());

4. Configure the resource factory of Tomcat

Configuring Resource Factory To add an element in $ catalina_home / conf / server.xml file, add it in context elements (or default context elements).

... Factory org.apache.naming.factory.beanfactory bar 23 ...

Note that the resource name must comply with the deployment rules. In this example, the setbar function must be defined in the class.

JDBC Data Sources JDBC Data Source

0. Introduce Many web applications require access to the database through the JDBC to support the functions required to apply. To achieve this, the J2EE server requires a data source implementation (ie, a JDBC connection pool). Tomcat5 is very good to implement this requirement, so you can run well on the database-based application on other J2EE servers.

JDBC related information, please refer to the following:

Http://java.sun.com/products/jdbc/ - JDBC's hometown. http://java.sun.com/j2se/1.3/docs/guide/jdbc/spec2/jdbc2.1.frame.html - JDBC 2.1 API Description. Http://java.sun.com/products/jdbc/jdbc20.stdext.pdf - JDBC 2.0 standard extension API (including javax.sql.datasource API). This package is called "JDBC optional package" http://java.sun.com/j2ee/download.html - J2EE Platform Description (All JDBC features that must be implemented). 1. Install the JDBC driver To use JDBC data sources, JNDI resource factory requires you to Suitable JDBC drivers are placed in Tomcat built-in classes and applications. It is easy to implement the method to put the drive in a $ catalina_home / common / lib directory :). 2. Define the resource below to modify your web application departure device (/Web-inf/web.xml), where you will find The JNDI name is defined before the resource. The agreement is this: All these names are placed under the JDBC sub-context (the root of the standard context is Java: Comp / ENV), a typical web.xml content is as follows: Resource reference to a factory for java.sql.Connection instances that may be used for talking to a particular database is configured in the server.xml file. jdbc / EmployeeDB javax.sql.datasource container

Note: Element rules that must be followed by the DTD defined by the Planner. See Servlet Specification 3. Write a typical application for writing this resource. Reference is as follows:

Context initCtx = new InitialContext (); Context envCtx = (Context) initCtx.lookup ( "java: comp / env"); DataSource ds = (DataSource) envCtx.lookup ( "jdbc / EmployeeDB"); Connection conn = ds.getConnection (); ... USE THIS Connection To Access The Database ... conn.close ();

Note The name used by the application is consistent with the application departener, it must also be consistent with the resource factory defined in $ catalina_home / conf / server.xml, as follows: 4. Configure Tomcat resource factory configuration factories, Add an element in $ catalina_home / conf / server.xml, placed in the context of this web application (or put it in the defaultcontext element surrounded by or element)

... Username dbusername password dbpassword driverclassname Org.hsql.jdbcdriver URL jdbc: hypersonicsql: Database Maxactive 8 maxidle 4 ... Note Resource Name (here is JDBC / EMPLOYEEDB) must comply with the Web Application Platform. This example uses the HypersonicsQL database JDBC driver. Please modify the corresponding driver name and connect the URL to comply with the database you use. Tomcat Standard Data Source Configuration The properties needed by the factory are as follows:

DriverClassName - JDBC used database driver class full name. MaxActive - The maximum active connection provided in the same time is available at the same time. Maxidle - Connecting pool keeps the maximum number of connections in idle moments. MaxWait - When an abnormality occurs, the database waits for the maximum millisecond (when there is no connection available). Password - Connect the password of the database. URL - Connect to the driver URL. DRIVERNAME is also allowed to be later compatible.) User - Database User Name. ValidationQuery - This is a SQL statement that will be called by the factory to ensure that the database is available.

转载请注明原文地址:https://www.9cbs.com/read-17244.html

New Post(0)