Add and Modify XML

Create a new context.xml file, and modify the existing web.xml file.

Add context.xml

We provide the XML code that sets the configuration of the PUBS database. You must add the XML to your project.

  1. In the Project Explorer, expand JSPBookDemo > WebContent.
  2. Right-click META-INF; then select New > Other.
  3. Expand XML; then select XML File.
  4. Click Next.
  5. In the File name field, type context.xml; then click Finish.

    This opens the empty context.xml file in the file editor.

  6. Copy the following XML code and paste it into the context.xml file:
    <?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>

    Replacing SQLServerUserName and SQLServerPassword respectively with the user name and password required for SQL Server authentication

  7. Click File > Save to save the file.

Modify web.xml

The web.xml file requires modification to accommodate the SQL Server database.

  1. In the Project Explorer expand JSPBookDemo > WebContent > WEB-INF.
  2. Double-click web.xml to open it in the file editor.
  3. Replace the entire contents of the file with the following:
    <?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. Click File > Save to save your changes.