wiki:java/Jetty

Version 3 (modified by yuna, 54 years ago) (diff)

--

Jettyメモ

Tomcatと双璧を成すOSSのWebコンテナとしてJettyがありますが、Jetty 7.0からEclipse Foundationからリリースされるようになりました。これに従い、Jettyのパッケージもorg.eclipse.jettyパッケージに変更になっています。本稿では、Jetty7.0以降で利用できるTipsを紹介します。

データソースの設定

Jettyのデータソースの設定はTomcatと全く異なる上に分かりやすい説明が少なく結構はまります。ここでは、Jettyにおけるデータソースの設定方法を紹介します。まず、データソースを設定する上で知っておくべき基本的なディレクトリ構成を示します。ここでは、webappsディレクトリの下のtestdsアプリケーションでデータソースを利用できるように設定します。

JETTY_HOME/
   +contexts/
   | +testds.xml(※1)
   +lib/
   | +ext/(※2)
   |   +commons-dbcp-1.4.jar
   |   +commons-ppol-1.5.4.jar
   |   +postgresql-8.3-605.jdbc4.jar
   |
   +webapps/
     +testds/(※3)
       +WEB-INF/
          +web.xml 
  • ※1: アプリケーションコンテキストの設定の中でデータソースに関する設定を記述します。
  • ※2: データソースを利用するためのコネクションプールやJDBCドライバのjarファイルを置きます。
  • ※3: web.xmlにresource-ref要素の設定を行います(これはTomcatなどの他のAPサーバと同じです)

コンテキストの設定

contextsディレクトリの下にコンテキストを設定したXMLファイル(ここではtestds.xml)を作成します。次のような感じで記述します。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.eclipse.org/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <!-- http://hosname/xxxxでアクセスできるパスの設定 -->
  <Set name="contextPath">/testds</Set>

  <!-- webアプリケーションを配備したディレクトリ -->
  <Set name="resourceBase"><SystemProperty name="jetty.home" default="."/>/webapps/testds</Set>

  <!-- warファイルを利用する場合は、resourceBaseの代わりにwarを利用
  <Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/testds.war</Set>
  <Set name="extractWAR">true</Set>
  <Set name="copyWebDir">true</Set>
  -->

  <Set name="defaultsDescriptor"><SystemProperty name="jetty.home" default="."/>/etc/webdefault.xml</Set>
  <Set name="ConfigurationClasses">
    <Array type="java.lang.String">
      <Item>org.eclipse.jetty.webapp.WebInfConfiguration</Item>
      <Item>org.eclipse.jetty.webapp.WebXmlConfiguration</Item>
      <Item>org.eclipse.jetty.plus.webapp.EnvConfiguration</Item>
      <Item>org.eclipse.jetty.plus.webapp.Configuration</Item>
      <Item>org.eclipse.jetty.webapp.JettyWebXmlConfiguration</Item>
    </Array>
  </Set>

  <!-- データソースの設定(Commons DBCP,PostgreSQLを利用) -->
  <New class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg>jdbc/myds</Arg>
    <Arg>
      <New class="org.apache.commons.dbcp.BasicDataSource">
           <Set name="driverClassName">org.postgresql.Driver</Set>
           <Set name="url">jdbc:postgresql://localhost:5432/tomcat</Set>
           <Set name="username">postgres</Set>
           <Set name="password">postgres</Set>
      </New>
    </Arg>
  </New>

</Configure>

JDBCドライバとコネクションプールのjarファイルのコピー

lib/extディレクトリにdbcpに必要なファイルとJDBCドライバのjarファイルをコピーします。

web.xmlにデータソースの設定を記述 ==

webapps/<webアプリケーション>/WEB-INF/web.xmlにJNDIリソースの設定を行います。

<resource-ref>
   <description>postgreSQL Datasource example</description>
   <res-ref-name>jdbc/myds</res-ref-name>
   <res-type>javax.sql.DataSource</res-type>
   <res-auth>Container</res-auth>
</resource-ref>

データソースの利用

サーブレット内のアプリケーションで次のようにしてデータソースを取得します。

  InitialContext ic = new InitialContext();
  Context context = (Context)ic.lookup("java:comp/env");
  DataSource ds = (DataSource)context.lookup("jdbc/myds");
  Connection con = ds.getConnection();

参考URL