XML の追加と変更

新しい context.xml ファイルを作成し、既存の web.xml ファイルを変更します。

context.xml の追加

PUBS データベースの構成を設定する XML コードを用意してあります。その XML をプロジェクトに追加する必要があります。

  1. プロジェクト エクスプローラーで、[JSPBookDemo > WebContent] を展開します。
  2. [META-INF] を右クリックし、[New > Other] を選択します。
  3. [XML] を展開し、[XML File] を選択します。
  4. [Next] をクリックします。
  5. [Folder name] フィールドにcontext.xmlと入力し、[完了] をクリックします。

    これにより、空の context.xml ファイルがファイル エディターに表示されます。

  6. 次の XML コードをコピーして、context.xml ファイルに貼り付けます。
    <?xml version="1.0" encoding="UTF-8"?>
    <Context>
    
    <!-- maxActive: Maximum number of database connections in pool. Make sure you
             configure your mysqld max_connections large enough to handle
             all of your db connections. Set to -1 for no limit.
             -->
           <!-- maxIdle: Maximum number of idle database connections to retain in pool.
             Set to -1 for no limit.  See also the DBCP documentation on this
             and the minEvictableIdleTimeMillis configuration parameter.
             -->
           <!-- maxWait: Maximum time to wait for a database connection to 
             become available in ms, in this example 10 seconds. 
             An Exception is thrown if this timeout is exceeded.  
             Set to -1 to wait indefinitely.
             -->
           <!-- username and password: username and password for the connection -->
           <!-- driverClassName: Class name for the JDBC driver -->
           <!-- url: The JDBC connection url for connecting to the database -->
    
           <Resource name="PUBS" auth="Container" type="javax.sql.DataSource"
                   maxActive="10" maxIdle="10" maxWait="10000"
                   username="SQLServerUserName" password="SQLServerPassword"
                   driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
                   url="jdbc:sqlserver://localhost;database=PUBS;"
           />       
    </Context>

    SQLServerUserNameSQLServerPassword を、SQL Server 認証に必要なユーザー名とパスワードに置き換えます。

  7. [File > Save] をクリックしてファイルを保存します。

web.xml の変更

web.xml ファイルを変更して、SQL Server データベースに適応させる必要があります。

  1. プロジェクト エクスプローラーで、[JSPBookDemo > WebContent > WEB-INF] を展開します。
  2. [web.xml] をダブルクリックしてファイル エディターで開きます。
  3. ファイルのすべての内容を次のコードに置き換えます。
    <?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"
             version="2.5">
      <display-name>JSPBookDemo</display-name>
      <servlet>
        <servlet-name>BookServlet</servlet-name>
        <servlet-class>com.microfocus.book.BookServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>BookServlet</servlet-name>
        <url-pattern>/view</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
        <welcome-file>view</welcome-file>
      </welcome-file-list>
      <resource-ref>
          <description>DB Connection</description>
          <res-ref-name>PUBS</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Container</res-auth>
      </resource-ref>
    </web-app>
  4. [File > Save] をクリックして変更内容を保存します。