Jetty JNDI development practices

Source: Internet
Author: User

Jetty JNDI development practices

I. Introduction to JNDI

JNDI is Java Naming and Directory Interface. Java Naming and Directory Interface is a standard Java Naming System Interface provided by Oracle. It allows Java clients to discover or query data and Objects Based on their names. The JNDI specification provides a set of standard naming system-independent APIs. These APIs are built on the drivers related to the naming system and can be used to separate applications from actual data sources, therefore, no matter whether the application accesses LDAP, RMI, DNS, or other directory services. For example, data sources, email servers, and message queues can all be found through JNDI. The JNDI query policy abstracts applications from external resources and makes it easier to configure external resources.

This article describes how to use JNDI to query Jetty. We first register a JDBC data source, and then query and access the data source through JNDI. Then we can see how the JNDI resource is configured to an independent Jetty.

2. Development Environment
The tools used in this article are listed as follows:
1) JDK 8.0.25
2) Maven 3.3.3
3) Eclipse JEE 4.5.0 (mars)
4) Jetty 9.3.2
5) H2 Database Engine 1.4.188

Note that the H2 database can be run through a single JAR package without additional installation.
In addition, assume that we want to create a jcgexamle data table, which has two columns: ID column and ARTICLE_NAME column.

C: \ jtools \ h2 \ bin> java-cp h2-1.4.188.jar org. h2.tools. Server
TCP server running at tcp: // 192.168.1.103: 9092 (only local connections)
PG server running at pg: // 192.168.1.103: 5435 (only local connections)
Web Console server running at http: // 192.168.1.103: 8082 (only local connections)
This command immediately starts the server mode of the H2 database, automatically opens the browser, connects to the H2 Web console, and allows users to create and configure the H2 database.

Select the Chinese (simplified) language and enter
Jdbc url: jdbc: h2: tcp: // localhost /~ /Jcgdb
User name: sa
Click test connection to return the "test successful" message. In this case, the H2 database engine automatically creates the name jcgdb in the home directory of the file system. mv. db database file, then we can run the following script to create the data table jcgexamle and insert two rows of records:

Create cached table public. JCGEXAMPLE (
Id integer,
ARTICLE_NAME VARCHAR (255)
);
Insert into JCGEXAMPLE values (1, 'jetty JNDI Example ');
Insert into JCGEXAMPLE values (2, 'jetty JMX Example ');
Now, the database is ready.

3. Build a Demo project
1. Create a Maven project in the Eclipse Environment
Group Id: com. ch. snippets. enterprise
Artifact Id: jetty-jndi-demo
Packaging: war

2. Add dependency packages
Add the following dependency packages to the pom. xml configuration file:
Org. eclipse. jetty: jetty-server
Org. eclipse. jetty: jetty-webapp
Org. eclipse. jetty: jetty-plus
Org. eclipse. jetty: jetty-jndi
Com. h2database: h2

The pom. xml configuration file is as follows:

<Project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<ModelVersion> 4.0.0 </modelVersion>
<GroupId> com. ch. snippets. enterprise </groupId>
<ArtifactId> jetty-jndi-demo </artifactId>
<Version> 0.0.1-SNAPSHOT </version>
<Packaging> war </packaging>
<Dependencies>
<Dependency>
<GroupId> org. eclipse. jetty </groupId>
<ArtifactId> jetty-server </artifactId>
<Version> 9.3.2.v20150730 </version>
</Dependency>
<Dependency>
<GroupId> org. eclipse. jetty </groupId>
<ArtifactId> jetty-webapp </artifactId>
<Version> 9.3.2.v20150730 </version>
</Dependency>
<Dependency>
<GroupId> org. eclipse. jetty </groupId>
<ArtifactId> jetty-plus </artifactId>
<Version> 9.3.2.v20150730 </version>
</Dependency>
<Dependency>
<GroupId> org. eclipse. jetty </groupId>
<ArtifactId> jetty-jndi </artifactId>
<Version> 9.3.2.v20150730 </version>
</Dependency>

<Dependency>
<GroupId> com. h2database </groupId>
<ArtifactId> h2 </artifactId>
<Version> 1.4.188 </version>
</Dependency>
</Dependencies>
</Project>

Installation and configuration of Jetty Server

Use Jetty as an Embedded Server

Jetty source code analysis

Jetty installation learning and demonstration

Install Jetty in Eclipse

Install Jetty in Linux (RedHat 5.8)

Jetty details: click here
Jetty's: click here

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.