MySQL Lists are EOL. Please join:

List:Commits« Previous MessageNext Message »
From:tony.bedford Date:July 6 2010 2:36pm
Subject:svn commit - mysqldoc@docsrva: r21585 - trunk/refman-common
View as plain text  
Author: tbedford
Date: 2010-07-06 16:36:23 +0200 (Tue, 06 Jul 2010)
New Revision: 21585

Log:
Notes on getting a JSP/JDBC application working on Glassfish with details of XML hackery.

Modified:
   trunk/refman-common/connector-j.xml


Modified: trunk/refman-common/connector-j.xml
===================================================================
--- trunk/refman-common/connector-j.xml	2010-07-06 13:30:18 UTC (rev 21584)
+++ trunk/refman-common/connector-j.xml	2010-07-06 14:36:23 UTC (rev 21585)
Changed blocks: 1, Lines Added: 219, Lines Deleted: 0; 7791 bytes

@@ -4734,6 +4734,225 @@
 
         </itemizedlist>
 
+        <section id="connector-j-usagenotes-glassfish-config-jsp">
+
+          <title>A Simple JSP Application with Glassfish, Connector/J and MySQL</title>
+
+          <para>
+            This section shows how to deploy a simple JSP application on
+            Glassfish, that connects to a MySQL database.
+          </para>
+
+          <para>
+            This example assumes you have already set up a suitable
+            Connection Pool and JDBC Resource, as explained in the
+            preceding sections. It is also assumed you have a sample
+            database installed, such as <literal>world</literal>.
+          </para>
+
+          <para>
+            The main application code, <filename>index.jsp</filename> is
+            presented here:
+          </para>
+
+<programlisting language="jsp">
+<![CDATA[
+<%@ page import="java.sql.*, javax.sql.*, java.io.*, javax.naming.*" %>
+<html>
+<head><title>Hello world from JSP</title></head>
+<body>
+<%
+  InitialContext ctx;
+  DataSource ds;
+  Connection conn;
+  Statement stmt;
+  ResultSet rs;
+
+  try {
+    ctx = new InitialContext();
+    ds = (DataSource) ctx.lookup("java:comp/env/jdbc/MySQLDataSource");
+    //ds = (DataSource) ctx.lookup("jdbc/MySQLDataSource");
+    conn = ds.getConnection();
+    stmt = conn.createStatement();
+    rs = stmt.executeQuery("SELECT * FROM Country");
+
+    while(rs.next()) {
+%>
+    <h3>Name: <%= rs.getString("Name") %></h3>
+    <h3>Population: <%= rs.getString("Population") %></h3>
+<%    
+    }
+  }
+  catch (SQLException se) {
+%>
+    <%= se.getMessage() %>
+<%      
+  }
+  catch (NamingException ne) {
+%>  
+    <%= ne.getMessage() %>
+<%
+  }
+%>
+</body>
+</html>
+]]>
+</programlisting>
+
+          <para>
+            In addition two XML files are required:
+            <filename>web.xml</filename>, and
+            <filename>sun-web.xml</filename>. There may be other files
+            present, such as classes and images. These files are
+            organized into the directory structure as follows:
+          </para>
+
+<programlisting>
+index.jsp
+WEB-INF
+   |
+   - web.xml
+   - sun-web.xml
+</programlisting>
+
+          <para>
+            The code for <filename>web.xml</filename> is:
+          </para>
+
+<programlisting language="xml">
+<![CDATA[
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
+  <display-name>HelloWebApp</display-name>  
+  <distributable/>
+  <resource-ref>
+    <res-ref-name>jdbc/MySQLDataSource</res-ref-name>
+    <res-type>javax.sql.DataSource</res-type>
+    <res-auth>Container</res-auth>
+    <res-sharing-scope>Shareable</res-sharing-scope>                
+  </resource-ref>
+</web-app>
+]]>
+</programlisting>
+
+          <para>
+            The code for <filename>sun-web.xml</filename> is:
+          </para>
+
+<programlisting language="xml">
+<![CDATA[
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 8.1 Servlet 2.4//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_2_4-1.dtd">
+<sun-web-app>
+  <context-root>HelloWebApp</context-root>
+  <resource-ref>
+    <res-ref-name>jdbc/MySQLDataSource</res-ref-name>
+    <jndi-name>jdbc/MySQLDataSource</jndi-name>  
+  </resource-ref> 
+</sun-web-app>
+]]>
+</programlisting>
+
+          <para>
+            These XML files illustrate a very important aspect of
+            running JDBC applications on Glassfish. On Glassfish it is
+            important to map the string specified for a JDBC resource to
+            its JNDI name, as set up in the Glassfish administration
+            console. In this example, the JNDI name for the JDBC
+            resource, as specified in the Glassfish Administration
+            console when creating the JDBC Resource, was
+            <literal>jdbc/MySQLDataSource</literal>. This must be mapped
+            to the name given in the application. In this example the
+            name specified in the application,
+            <literal>jdbc/MySQLDataSource</literal>, and the JNDI name,
+            happen to be the same, but this does not necessarily have to
+            be the case. Note that the XML element &lt;res-ref-name&gt;
+            is used to specify the name as used in the application
+            source code, and this is mapped to the JNDI name specified
+            using the &lt;jndi-name&gt; element, in the file
+            <filename>sun-web.xml</filename>. The resource also has to
+            be created in the <filename>web.xml</filename> file,
+            although the mapping of the resource to a JNDI name takes
+            place in the <filename>sun-web.xml</filename> file.
+          </para>
+
+          <para>
+            If you do not have this mapping set up correctly in the XML
+            files you will not be able to lookup the data source using a
+            JNDI lookup string such as:
+          </para>
+
+<programlisting language="jsp">
+ds = (DataSource) ctx.lookup("java:comp/env/jdbc/MySQLDataSource");
+</programlisting>
+
+          <para>
+            You will still be able to access the data source directly
+            using:
+          </para>
+
+<programlisting language="jsp">
+ds = (DataSource) ctx.lookup("jdbc/MySQLDataSource");
+</programlisting>
+
+          <para>
+            With the source files in place, in the correct directory
+            structure, you are ready to deploy the application:
+          </para>
+
+          <orderedlist>
+
+            <listitem>
+              <para>
+                In the navigation tree, navigate to
+                <guilabel>Applications</guilabel> - the
+                <guilabel>Applications</guilabel> frame will be
+                displayed. Click <guibutton>Deploy</guibutton>.
+              </para>
+            </listitem>
+
+            <listitem>
+              <para>
+                You can now deploy an application packaged into a single
+                WAR file from a remote client, or you can choose a
+                packaged file or directory that is locally accessible to
+                the server. If you are simply testing an application
+                locally you can simply ponit Glassfish at the directory
+                that contains your application, without needing to
+                package the application into a WAR file.
+              </para>
+            </listitem>
+
+            <listitem>
+              <para>
+                Now select the application type from the
+                <guilabel>Type</guilabel> drop-down listbox, which in
+                this example is <literal>Web application</literal>.
+              </para>
+            </listitem>
+
+            <listitem>
+              <para>
+                Click OK.
+              </para>
+            </listitem>
+
+          </orderedlist>
+
+          <para>
+            Now, when you navigate to the
+            <guilabel>Applications</guilabel> frame, you will have the
+            option to <guilabel>Launch</guilabel>,
+            <guilabel>Redeploy</guilabel>, or
+            <guilabel>Restart</guilabel> your application. You can test
+            your application by clicking <guilabel>Launch</guilabel>.
+            The application will connection to the MySQL database and
+            display the Name and Population of countries in the
+            <literal>Country</literal> table.
+          </para>
+
+        </section>
+
       </section>
 
 <!--


Thread
svn commit - mysqldoc@docsrva: r21585 - trunk/refman-commontony.bedford6 Jul