http://www.netbeans.org/kb/docs/web/mysql-webapp.html
Simple web applications can be designed using a two-tier architecture, in which the application communicates directly with a data source using the Java Database Connectivity API. A user's requests are sent to a database, and the results are sent directly back to the user. Two-tier architectures can be easily mapped to a client-server configuration, where a user's browser serves as the client, and a remote database reachable over the Internet corresponds to the server.
The most efficient way to implement communication between the server and database is to set up a database connection pool. Creating a new connection for each client request can be very time-consuming, especially for applications that continuously receive a large number of requests. To remedy this, numerous connections are created and maintained in a connection pool. Any incoming requests that require access to the application's data layer use an already-created connection from the pool. Likewise, when a request is completed, the connection is not closed down, but returned to the pool.
Setting up a JNDI Datasource
Both GlassFish and Tomcat contain Database Connection Pooling (DBCP) libraries that provide connection pooling functionality in a way that is transparent to you as a developer. In either case, you need to configure a JNDI Datasource for the server that creates an interface which your application can use for connection pooling. Depending on whether you are using GlassFish or Tomcat, do the following:
GlassFish
The IDE provides enhanced support for GlassFish, enabling you to specify resources using a wizard:
- In the Projects window, right-click the Server Resources node and choose New > Other. The New File wizard opens. Under Categories, select GlassFish. Under File Types, select JDBC Resource. Click Next.
- Under General Attributes, choose the Create New JDBC Connection Pool option, then in the JNDI Name text field, type in
jdbc/IFPWAFCAD
. Click Next. - Click Next again to skip Additional Properties, then in Step 4, type in
IfpwafcadPool
for JDBC Connection Pool Name. Make sure the Extract from Existing Connection option is selected, and choose jdbc:mysql://localhost:3306/MyNewDatabase
from the drop-down list. Click Next.
Note: The wizard detects any database connections that have been set up in the IDE. Therefore, you need to have already created a connection to the MyNewDatabase
database at this point. You can verify what connections have been created by opening the Services window (Ctrl-5; ⌘-5 on Mac) and looking for connection nodes (
) under the Databases category. - Accept any other default settings within the wizard, then click Finish.
By completing the wizard, you declared a new datasource and connection pool for the application. In the Projects window, open the newly created Server Resources > sun-resources.xml
file and note that, within the <resources>
tags, a jdbc resource and connection pool have been declared containing the values you previously specified.
To confirm that a new datasource and connection pool are indeed registered with GlassFish, you can deploy the project to the server, then locate the resources in the IDE's Services window:
- In the Projects window, right-click the IFPWAFCAD project node and choose Deploy (or Undeploy and Deploy). The server starts up if not already running, and the project is compiled and deployed to it.
- Open the Services window (Ctrl-5) and expand the Servers > GlassFish > Resources > JDBC > JDBC Resources and Connection Pools nodes. Note that the new datasource and connection pool are now displayed:
Referencing the Datasource from the Application
You need to reference the JNDI resource you just configured from the web application. To do so, you can create an entry in the application's deployment descriptor (web.xml
).
Deployment descriptors are XML-based text files that contain information describing how an application is to be deployed to a specific environment. For example, they are normally used to specify application context parameters and behavioral patterns, security settings, as well as mappings for servlets, filters and listeners.
Do the following to reference the JNDI Datasource in the application's deployment descriptor.
- In the Projects window, expand the Web Pages > WEB-INF subfolder and double-click
web.xml
. A graphical editor for the file displays in the Source Editor. - Click the References tab located along the top of the Source Editor. Expand the Resource References heading, then click Add. The Add Resource Reference dialog opens.
- For Resource Name, enter the resource name that you gave when configuring the JNDI Datasource for the server above (
jdbc/IFPWAFCAD
). The Description field is optional, but you can enter a human-readable description of the resource, e.g., Database for IFPWAFCAD application
. Note that the default resource type is javax.sql.DataSource
. Leave all fields that are provided by default and click OK. The new resource is added under the Resource References heading:
To verify that the resource is now added to the web.xml
file, click the XML tab located along the top of the Source Editor. Notice that the following <resource-ref
> tags are now included: <resource-ref>
<description>Database for IFPWAFCAD application</description>
<res-ref-name>jdbc/IFPWAFCAD</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
Adding the JSTL Library to the Project's Classpath
In order to make better use of the JSP resources at your disposal, you can make use of the JavaServer Pages Standard Tag Library (JSTL) to access and display data taken from the Logic Layer. This library comes bundled with the IDE. You therefore need to make sure the JSTL library is added to the web project's compilation classpath, then add the relevent taglib
directives to each of the JSP pages. This allows the server we are using to identify the tags when it reads them from the JSP pages.
Adding taglib
Directives to the JSP Pages
Regardless of what server you are using, you need to add the necessary taglib
directives to JSP pages:
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
Adding JSP and JSTL Code
Finally, add the code to each page. The pages require that you implement an SQL query that utilizes the JSTL <sql:query> tags and the datasource created earlier in the tutorial.
- Add the following SQL query beneath the
taglib
directives you added in the previous step:
<sql:query var="subjects" dataSource="jdbc/IFPWAFCAD">
SELECT subject_id, name FROM Subject
</sql:query>
The JSTL <sql:query>
tags enable you to use SQL query language directly in a JSP page. A resultset
is generated from the query, and the acquired data can then be inserted into the page using an iterator tag (<c:forEach>
) from the JSTL core
library.
- Replace the empty
<option>
tags in the HTML form with the following iterator (changes in bold):
<select name="subject_id">
<c:forEach var="subject" items="${subjects.rows}">
<option value="${subject.subject_id}">${subject.name}</option>
</c:forEach>
</select>
The forEach
tag loops through all id
and name
values from the generated resultset
, and inserts each pair into the HTML option
tags. In this manner, the form's drop-down list is populated with data.