Changes between Version 1 and Version 2 of java/Jetty


Ignore:
Timestamp:
1970/01/01 09:21:13 (54 years ago)
Author:
yuna
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • java/Jetty

    v1 v2  
    33 
    44== データソースの設定 == 
     5Jettyのデータソースの設定はTomcatと全く異なる上に分かりやすい説明が少なく結構はまります。ここでは、Jettyにおけるデータソースの設定方法を紹介します。まず、データソースを設定する上で知っておくべき基本的なディレクトリ構成を示します。ここでは、webappsディレクトリの下のtestdsアプリケーションでデータソースを利用できるように設定します。 
     6{{{ 
     7JETTY_HOME/ 
     8   +contexts/ 
     9   | +testds.xml(※1) 
     10   +lib/ 
     11   | +ext/(※2) 
     12   |   +commons-dbcp-1.4.jar 
     13   |   +commons-ppol-1.5.4.jar 
     14   |   +postgresql-8.3-605.jdbc4.jar 
     15   | 
     16   +webapps/ 
     17     +testds/(※3) 
     18       +WEB-INF/ 
     19          +web.xml  
     20}}} 
     21 * ※1: アプリケーションコンテキストの設定の中でデータソースに関する設定を記述します。 
     22 * ※2: データソースを利用するためのコネクションプールやJDBCドライバのjarファイルを置きます。 
     23 * ※3: web.xmlにresource-ref要素の設定を行います(これはTomcatなどの他のAPサーバと同じです) 
     24 
     25=== コンテキストの設定 === 
     26contextsディレクトリの下にコンテキストを設定したXMLファイル(ここではtestds.xml)を作成します。次のような感じで記述します。 
     27{{{ 
     28<?xml version="1.0" encoding="UTF-8"?> 
     29<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.eclipse.org/configure.dtd"> 
     30<Configure class="org.eclipse.jetty.webapp.WebAppContext"> 
     31  <!-- http://hosname/xxxxでアクセスできるパスの設定 --> 
     32  <Set name="contextPath">/testds</Set> 
     33 
     34  <!-- webアプリケーションを配備したディレクトリ --> 
     35  <Set name="resourceBase"><SystemProperty name="jetty.home" default="."/>/webapps/testds</Set> 
     36 
     37  <!-- warファイルを利用する場合は、resourceBaseの代わりにwarを利用 
     38  <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/testds.war</Set> 
     39  <Set name="extractWAR">true</Set> 
     40  <Set name="copyWebDir">true</Set> 
     41  --> 
     42 
     43  <Set name="defaultsDescriptor"><SystemProperty name="jetty.home" default="."/>/etc/webdefault.xml</Set> 
     44  <Set name="ConfigurationClasses"> 
     45    <Array type="java.lang.String"> 
     46      <Item>org.eclipse.jetty.webapp.WebInfConfiguration</Item> 
     47      <Item>org.eclipse.jetty.webapp.WebXmlConfiguration</Item> 
     48      <Item>org.eclipse.jetty.plus.webapp.EnvConfiguration</Item> 
     49      <Item>org.eclipse.jetty.plus.webapp.Configuration</Item> 
     50      <Item>org.eclipse.jetty.webapp.JettyWebXmlConfiguration</Item> 
     51    </Array> 
     52  </Set> 
     53 
     54  <!-- データソースの設定(Commons DBCP,PostgreSQLを利用) --> 
     55  <New class="org.eclipse.jetty.plus.jndi.Resource"> 
     56    <Arg>jdbc/myds</Arg> 
     57    <Arg> 
     58      <New class="org.apache.commons.dbcp.BasicDataSource"> 
     59           <Set name="driverClassName">org.postgresql.Driver</Set> 
     60           <Set name="url">jdbc:postgresql://localhost:5432/tomcat</Set> 
     61           <Set name="username">postgres</Set> 
     62           <Set name="password">postgres</Set> 
     63      </New> 
     64    </Arg> 
     65  </New> 
     66 
     67</Configure> 
     68}}} 
     69=== JDBCドライバとコネクションプールのjarファイルのコピー === 
     70lib/extディレクトリにdbcpに必要なファイルとJDBCドライバのjarファイルをコピーします。 
     71 
     72=== web.xmlにデータソースの設定を記述 == 
     73webapps/<webアプリケーション>/WEB-INF/web.xmlにJNDIリソースの設定を行います。 
     74{{{ 
     75<resource-ref> 
     76   <description>postgreSQL Datasource example</description> 
     77   <res-ref-name>jdbc/myds</res-ref-name> 
     78   <res-type>javax.sql.DataSource</res-type> 
     79   <res-auth>Container</res-auth> 
     80</resource-ref> 
     81}}} 
     82=== データソースの利用 === 
     83サーブレット内のアプリケーションで次のようにしてデータソースを取得します。 
     84{{{ 
     85  InitialContext ic = new InitialContext(); 
     86  Context context = (Context)ic.lookup("java:comp/env"); 
     87  DataSource ds = (DataSource)context.lookup("jdbc/myds"); 
     88  Connection con = ds.getConnection(); 
     89}}}