Book Image

Java EE 7 with GlassFish 4 Application Server

By :
Book Image

Java EE 7 with GlassFish 4 Application Server

By:

Overview of this book

<p>GlassFish is a free, open source application server which supports all the major Java features such as Enterprise JavaBeans, JPA, JavaServer Faces, JMS, RMI, JavaServer Pages, and servlets. It is the first Java EE 7 compliant application server. All major Java EE technologies and API's are covered in this version of Java. GlassFish server allows the user to work with the extensile, adaptable, and lightweight Java EE 7 platform. <br /><br />This book explores the installation and configuration of GlassFish, and then moves on to Java EE 7 application development, covering all major Java EE 7 APIs. It focuses on going beyond the basics to develop Java applications deployed to the GlassFish 4 application server. The book covers all major Java EE 7 APIs including JSF 2.2, EJB 3.2, CDI 1.1, the Java API for WebSocket, JAX-WS, JAX-RS and more. <br /><br />The book also introduces JSON-P, the Java API for JSON (Javascript Object Notation) Processing. This advanced topic deals with how the two APIs are used to process JSON function, namely the Model API and the Streaming API. Apart from revisiting Java Server Faces (JSF), it explains why Facelets, the new features introduced in modern versions of JSF, are the preferred view technology over Java Server Pages (JSP)<br /><br />The later chapters explore competing implementations of the WebSocket standard in Java, describing the updates in JMS; which aims to provide a simpler API and reduction in boilerplate code among a host of other features. Readers will also learn how to secure Java EE applications by taking advantage of GlassFish's built-in security features. Finally, we learn more about the RESTful web service development using the JAX-RS specification.</p>
Table of Contents (18 chapters)
Java EE 7 with GlassFish 4 Application Server
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
9
Securing Java EE Applications
Index

Setting up Database Connectivity


Any nontrivial Java EE application will connect to a Relational Database Management System (RDBMS). Supported RDBMS systems include Java DB, Oracle, Derby, Sybase, DB2, PointBase, MySQL, PostgreSQL, Informix, Cloudscape, and SQL Server. In this section, we will demonstrate how to set up GlassFish to communicate with a MySQL database. The procedure is similar for other RDBMS systems.

Note

GlassFish comes bundled with an RDBMS called JavaDB. This RDBMS is based on Apache Derby. To limit the download and configuration needed to follow this book's code, all examples needing an RDBMS will use the bundled JavaDB RDBMS. The instructions in this section are to illustrate how to connect GlassFish to a third-party RDBMS.

Setting up connection pools

Opening and closing the database connections is a relatively slow operation. For performance reasons, GlassFish and other Java EE application servers keep a pool of open database connections; when a deployed application requires a database connection, one is provided from the pool; when the application no longer needs the database connection, the said connection is returned to the pool.

The first step to follow when setting up a connection pool is to copy the JAR file containing the JDBC driver for our RDBMS in the lib directory of the domain (consult your RDBMS documentation for information on where to obtain this JAR file). If the GlassFish domain where we want to add the connection pool is running when copying the JDBC driver, it must be restarted for the change to take effect. The domain can be restarted by executing the following command:

 asadmin restart-domain domainname

Once the JDBC driver has been copied to the appropriate location and the application server has been restarted, log in to the admin console by pointing the browser to http://localhost:4848.

Then, navigate to Resources | JDBC | JDBC Connection Pools. The browser should now look something like what is shown in the following screenshot:

Click on the New... button. After entering the appropriate values for our RDBMS, the main area of the page should look something like the following screenshot:

After clicking on the Next button, we should see a page similar to the one shown in the following screenshot:

Most of the default values on the top portion of the page shown in the preceding screenshot are sensible. Scroll all the way down and enter the appropriate property values for our RDBMS (at a minimum, username, password, and URL). Then, click on the Finish button at the top right of the screen.

Property names vary depending on the RDBMS we are using, but usually there is a URL property where we should enter the JDBC URL for our database, plus username and password properties where we should enter authentication credentials for our database.

Our newly created connection pool should now be visible in the list of connection pools as shown in the following screenshot:

In some cases, the GlassFish domain may need to be restarted after setting up a new connection pool.

We can verify that our connection pool was successfully set up by clicking on its pool name and then enabling the Ping button on the resulting page as shown in the following screenshot:

Our connection pool is now ready to be used by our applications.

Setting up the data sources

Java EE applications don't access connection pools directly; they access a data source instead, which points to a connection pool. To set up a new data source, click on the JDBC icon under the Resources menu item on the left-hand side of the web console, then click on the JDBC Connection Pools tab, and then click on the New... button. After filling out the appropriate information for our new data source, the main area of the web console should look something like what is shown in the following screenshot:

After clicking on the OK button, we can see our newly created data source as shown in the following screenshot: