Java EE 5 Development using GlassFish Application Server

By David R. Heffelfinger
  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Getting Started with GlassFish

About this book

GlassFish is a free, open-source Java EE 5-compliant application server that is quickly gaining massive popularity.

This book explains GlassFish installation and configuration, and then moves on to Java EE 5 application development, covering all major Java EE 5 APIs.

Chapter 1 provides an overview of Glassfish, including how to install it, configure it, and verify the installation.

Chapter 2 covers how to develop server-side web applications using the Servlet API.  

Chapter 3 explains how to develop web applications using JavaServer Pages (JSPs), including how to develop and use JSP custom tags.

Chapter 4 discusses how to develop Java EE applications that interact with a relational database system through the Java Persistence API (JPA) and through the Java Database Connectivity API (JDBC).

Chapter 5 explains how to use the JSP Standard Tag Library (JSTL) when developing JavaServer Pages.

Chapter 6 covers how to develop applications using the JavaServer Faces (JSF) component framework to build web applications.

Chapter 7 explains how to develop messaging applications though the Java Messaging Service (JMS) API.

Chapter 8 covers securing J2EE applications through the Java Authentication and Authorization Service (JAAS).

Chapter 9 discusses how to develop Enterprise Java Beans that adhere to the EJB 3 specification.

Chapter 10 explains how to develop and deploy web services that conform to the JAX-WS 2.1 specification.

Chapter 11 covers frameworks that build on top of the Java EE 5 specification, including Seam, Facelets, and Ajax4Jsf.

The appendices cover some of the advanced features of the GlassFish server.

Publication date:
October 2007
Publisher
Packt
Pages
424
ISBN
9781847192608

 

Chapter 1. Getting Started with GlassFish

In this chapter, we will discuss how to get started with GlassFish. Some of the topics discussed in this chapter are:

  • An overview of Java EE and GlassFish

  • Obtaining GlassFish

  • Installing GlassFish

  • Verifying the GlassFish Installation

  • Deploying Java EE Applications

  • Setting Up Database Connectivity

Overview of Java EE and GlassFish

Java EE (formerly called J2EE) is a standard set of technologies for server-side Java development. Java EE technologies include Servlets, JavaServer Pages (JSPs), JavaServer Faces (JSF), Enterprise JavaBeans (EJBs), and the Java Messaging Service (JMS).

Several commercial and open-source application Java EE servers exist. Java EE application servers, such as GlassFish, allow application developers to develop and deploy Java EE-compliant applications. Other open-source Java EE application servers include Red Hat's JBoss, the Apache Software Foundation's Geronimo, and ObjectWeb's JOnAS.

Commercial application servers include BEA's Weblogic, IBM's Websphere, and the Oracle Application Server.

GlassFish is an open-source, freely available, Java EE application server. GlassFish is dual licensed under the Common Development and Distribution License (CDDL) and the GNU Public License (GPL) version 2.

Note

To find out more about GlassFish's license, see https://glassfish.dev.java.net/public/CDDL+GPL.html.

Like all Java EE-compliant application servers, GlassFish provides the necessary libraries to allow us to develop and deploy Java applications compliant with Java EE specifications.

GlassFish Advantages

With so many options in Java EE application servers, why choose GlassFish? Besides the obvious advantage of GlassFish being available free of charge, it offers the following benefits:

  • It is made by Sun Microsystems

    • Sun Microsystems is the steward of Java language, and the Java EE specification.

  • Commercial support is available

    • Sun Microsystems sells a re-packaged version of GlassFish called the Sun Java System Application Server. Commercial support is available (at a cost) from Sun Microsystems for this re-packaged GlassFish version. Many software development shops will not use any software for which commercial support is not available; therefore commercial support availability allows GlassFish to be used in environments where it otherwise wouldn't be.

  • It is the Java EE reference Implementation

    • GlassFish is the Java EE Reference implementation. What this means is that other application servers may use GlassFish to make sure their product complies with the specification. GlassFish could theoretically be used to debug other application servers. If an application deployed under another application server is not behaving properly, but it does behave properly when deployed under GlassFish, then it is more than likely that the improper behavior is due to a bug in the other application server.

  • It supports the latest versions of the Java EE specification

    • GlassFish is the reference Java EE specification, so it tends to implement the latest specifications before any other application server in the market.

 

Overview of Java EE and GlassFish


Java EE (formerly called J2EE) is a standard set of technologies for server-side Java development. Java EE technologies include Servlets, JavaServer Pages (JSPs), JavaServer Faces (JSF), Enterprise JavaBeans (EJBs), and the Java Messaging Service (JMS).

Several commercial and open-source application Java EE servers exist. Java EE application servers, such as GlassFish, allow application developers to develop and deploy Java EE-compliant applications. Other open-source Java EE application servers include Red Hat's JBoss, the Apache Software Foundation's Geronimo, and ObjectWeb's JOnAS.

Commercial application servers include BEA's Weblogic, IBM's Websphere, and the Oracle Application Server.

GlassFish is an open-source, freely available, Java EE application server. GlassFish is dual licensed under the Common Development and Distribution License (CDDL) and the GNU Public License (GPL) version 2.

Note

To find out more about GlassFish's license, see https://glassfish.dev.java.net/public/CDDL+GPL.html.

Like all Java EE-compliant application servers, GlassFish provides the necessary libraries to allow us to develop and deploy Java applications compliant with Java EE specifications.

GlassFish Advantages

With so many options in Java EE application servers, why choose GlassFish? Besides the obvious advantage of GlassFish being available free of charge, it offers the following benefits:

  • It is made by Sun Microsystems

    • Sun Microsystems is the steward of Java language, and the Java EE specification.

  • Commercial support is available

    • Sun Microsystems sells a re-packaged version of GlassFish called the Sun Java System Application Server. Commercial support is available (at a cost) from Sun Microsystems for this re-packaged GlassFish version. Many software development shops will not use any software for which commercial support is not available; therefore commercial support availability allows GlassFish to be used in environments where it otherwise wouldn't be.

  • It is the Java EE reference Implementation

    • GlassFish is the Java EE Reference implementation. What this means is that other application servers may use GlassFish to make sure their product complies with the specification. GlassFish could theoretically be used to debug other application servers. If an application deployed under another application server is not behaving properly, but it does behave properly when deployed under GlassFish, then it is more than likely that the improper behavior is due to a bug in the other application server.

  • It supports the latest versions of the Java EE specification

    • GlassFish is the reference Java EE specification, so it tends to implement the latest specifications before any other application server in the market.

 

Obtaining GlassFish


GlassFish can be downloaded from https://glassfish.dev.java.net by clicking an image that looks like this:

The image should be near the top right window of the page.

After clicking on the image, and scrolling down to a section titled Binary builds around the middle of the resulting page, you should see links to download GlassFish for several different architectures. Currently Solaris Sparc, Solaris x86, Windows, Linux, and MacOS are supported.

To download GlassFish, simply click on the link for your platform; the file should start downloading immediately. After the file finishes downloading, you should have a file called something like glassfish-installer-v2-b58g.jar; the exact file name will depend on the exact GlassFish version and platform.

 

Installing GlassFish


Installing GlassFish is an easy process; however, GlassFish assumes that some dependencies are present in your system.

GlassFish Dependencies

Before GlassFish can be installed, a recent version of the Java Development Kit must be present in your system and (optionally) the Apache ANT tool.

  • Java Development Kit

    • In order to install GlassFish, a recent version of the Java Development Kit (JDK) must be installed on your workstation (JDK 1.5 or newer required), and the java executable must be in your system PATH. The latest JDK can be downloaded from http://java.sun.com/. Please refer to the JDK installation instructions for your particular platform at http://java.sun.com/javase/6/webnotes/install/index.html.

  • ANT (Optional)

    • Ant is a very popular build tool; it can be downloaded from http://ant.apache.org. A version of ANT is included with GlassFish, therefore this step is optional. If you already have ANT installed on your system you can use it to install GlassFish. Just make sure that the ant script is executable and in your system's PATH. Please note that GlassFish requires ANT 1.6.5 or later.

Performing the Installation

Once the dependencies have been installed, copy the file downloaded in the previous section to an appropriate installation location and run the following command from a terminal window:

java -Xmx256m -jar glassfish-installer-v2-b58g.jar

Note

The actual file name at the end of the command will depend on the version of GlassFish downloaded.

After running this command, a window prompting you to accept the license terms will show up.

Scroll all the way down and click on the button labeled Accept.

You should see a lot of text scrolling on your terminal window, after the text stops scrolling, you should see the following text at the bottom of your terminal window:

installation complete.

The installer creates a directory called glassfish at the location where we ran the above command from.

Although we saw the above message when we completed the step required in the previous paragraph, we are not quite done installing GlassFish. Inside the glassfish directory, there is an ANT build script that must be executed to complete the installation. The file name for this build script is setup.xml. This script can be executed from the command line by changing to the GlassFish installation directory and typing the following command:

ant -f setup.xml

After executing this command you should see the following message at the bottom of the terminal the time taken may vary:

BUILD SUCCESSFUL
Total time: 43 seconds

Note

The above command assumes that ANT 1.6.5 or newer is installed in the system, and that the ant executable is in the system PATH. GlassFish includes ANT; to access it from any directory add [glassfish installation directory]/glassfish/lib/ant/bin to your system PATH.

We have now successfully installed GlassFish and we are ready to start it for the first time.

 

Verifying the Installation


To start GlassFish, change directory to [glassfish installation directory]/glassfish/bin, and execute the following command:

./asadmin start-domain domain1

Note

The above command and most commands shown in this chapter assume a Unix or Unix-like operating system. For Windows systems, the initial "./" is not necessary.

After executing the above command you should see a message similar to the following in the command line console:

Domain domain1 is ready to receive client requests. Additional services are being started in background.

The above message will be followed by additional information indicating the ports that GlassFish listens to and other information.

We can then open a browser window and type the following URL in the browser's location text field: http://localhost:8080.

If everything went well you should see a page similar to the following:

Note

Getting Help

If any of the above steps fail, a good place to ask for help is the GlassFish forums at http://forums.java.net/jive/forum.jspa?forumID=56.

Deploying Our First Java EE Application

To further test that our GlassFish installation is running properly, we will deploy a war (Web ARchive) file and make sure it deploys and executes properly. Before moving on, please download the file simpleapp.war from this book's website.

Deploying an Application through the Web Console

To deploy simpleapp.war, open a browser and navigate to the following URL: http://localhost:4848; you should be greeted with a login screen that looks like the following:

The default administrator user name/password combination is admin/adminadmin and log in using these credentials; you should see a page like the following:

Note

Changing the Administrator Password

It is good practice to change the default administrator password. To change your administrator password click on the Application Server menu item at the top left, then click on the Administrator Password tab. Then enter and confirm the new administrator password and click on the Save button.

Now, click on the Deploy Web Application item on the main page (highlighted in the previous screenshot). You should now see a screen like the following:

The war file can either be deployed by "uploading it" to the server (this functionality is there to upload files to remote servers; in our case the workstation and server are one and the same), or by navigating the file system to select the desired war file.

Even though our server and workstation are one and the same, we will use the upload option as navigation is much easier (selecting the local packaged file option will result in us having to navigate through the whole directory tree; by selecting the upload option we only have to navigate from our home directory).

After clicking on the Browse button and navigating to the location of simpleapp.war, the screen will look similar to this:

Notice how the Application Name and Context Root text fields are automatically filled out.

Clicking on the OK button at the top right of the page will deploy the application.

As can be seen in the screenshot above, our simpleapp application now has been deployed.

To execute the simpleapp application, type the following URL in the browser's location text field: http://localhost:8080/simpleapp/simpleservlet. The resulting page should look like this:

That's it! We have successfully deployed our first Java EE application.

Undeploying an Application through the Web Console

In the next section, we explain how to deploy a web application through the command line. In order for the instructions in the next section to work, we need to undeploy simpleapp.war.

To undeploy the application that we deployed in the previous section, log in to the GlassFish web console by typing the following URL in the browser: http://localhost:4848 and entering the admin user name and corresponding password.

Then click on the Web Applications menu item near the top left of the page and click on the checkbox by simpleapp web application.

Then click on the Undeploy button; the application will be undeployed and removed from the list of deployed applications.

Deploying an Application through the Command Line

Now that we have undeployed the simpleapp file, we are ready to deploy it using the command line. To deploy the application in this manner, simply copy simpleapp.war to [glassfish installation directory]/glassfish/domains/domain1/autodeploy. The application will automatically be deployed just by copying it to this directory.

We can verify that the application has successfully been deployed by looking at the server log. The server log can be found at [glassfish installation directory]/glassfish/domains/domain1/logs/server.log. The last few lines on this file should look something like this:

[#|2007-02-13T20:57:41.825-0500|INFO|sun-appserver9.1|javax.enterprise.system.tools.deployment|_ThreadID=23;_ThreadName=Timer-4;|deployed with moduleid = simpleapp|#]
[#|2007-02-13T20:57:42.100-0500|INFO|sun-appserver9.1|javax.enterprise.system.tools.deployment|_ThreadID=23;_ThreadName=Timer-4;|[AutoDeploy] Successfully autodeployed : /opt/glassfish/domains/domain1/autodeploy/simpleapp.war.|#]

Of course, we can also verify the deployment by navigating to the URL for the application, which will be the same one that we used when deploying through the web console: http://localhost:8080/simpleapp/simpleservlet; the application should execute properly.

An alternative way of deploying an application through the command line is to use the following command:

asadmin deploy [path to file]/simpleapp.war

The server log file should show a message similar to the following:

[#|2007-02-15T18:03:13.879-0500|INFO|sun-appserver9.1|javax.enterprise.system.tools.deployment|_ThreadID=15;_ThreadName=Thread-25;|deployed with moduleid = simpleapp|#]

Undeploying an Application through the Command Line

To undeploy an application from the command line simply delete it from the [glassfish installation directory]/glassfish/domains/domain1/autodeploy directory. It will be automatically undeployed; this can be verified by looking at the server log, which should have some lines that look something like this:

[#|2007-02-13T21:04:43.753-0500|INFO|sun-appserver9.1|javax.enterprise.system.tools.deployment|_ThreadID=23;_ThreadName=Timer-4;|Autoundeploying application :simpleapp|#]
[#|2007-02-13T21:04:44.023-0500|INFO|sun-appserver9.1|javax.enterprise.system.tools.deployment|_ThreadID=23;_ThreadName=Timer-4;|[AutoDeploy] Successfully autoundeployed : /opt/glassfish/domains/domain1/autodeploy/simpleapp.war.|#]

As can be seen from this section and the previous one, deploying and undeploying an application through the command line is a very simple and fast process that saves a lot of time when testing applications. All future examples in this book will be deployed by copying the appropriate files to the autodeploy directory.

The asadmin executable can be used to undeploy an application as well, by issuing a command like the following:

asadmin undeploy simpleapp

The following message should be shown at the bottom of the terminal window:

Command undeploy executed successfully.

Please note that the file extension is not used to undeploy the application, the argument to asadmin undeploy should be the context root for the application (which is typed right after http://localhost:4848 to access the application through the browser), which defaults to the war file name.

Note

In the next chapter, we will see how to change the default context root for an application.

 

GlassFish Domains Explained


The alert reader might have noticed that the autodeploy directory is under a domains/domain1 subdirectory. GlassFish has a concept of domains. Domains allow a collection of related applications to be deployed together. A default domain called domain1 is created when installing GlassFish.

Creating Domains

Additional domains can be created from the command line by issuing the following command:

asadmin create-domain domainname

The above command takes several parameters to specify ports where the domain will listen to for several services (HTTP, Admin, JMS, IIOP, secure HTTP, etc.); type the following command in the command line to see this parameters:

asadmin create-domain --help

If we want several domains to execute concurrently on the same server, these ports must be chosen carefully, because specifying the same ports for different services (or even the same service across domains) will prevent one of the domains from working properly.

The default ports for the default domain1 domain are listed in the following table:

Service

Port

Admin

4848

HTTP

8080

Java Messaging System (JMS)

7676

Internet Inter-ORB Protocol (IIOP)

3700

Secure HTTP (HTTPS)

8181

Secure IIOP

3820

Mutual Authorization IIOP

3920

Java Management Extensions (JMX) Administration

8686

Please note that when creating a domain, the only port that needs to be specified is the admin port; if the other ports are not specified, the default ports listed in the table above will be used. Care must be taken when creating a domain, because, as explained above, two domains cannot run concurrently in the same server if any of their services listen for connections on the same port.

An alternative method of creating a domain, without having to specify ports for every service, is to issue the following command:

asadmin createdomain --portbase [port number] domainname

The value of the --portbase parameter dictates the base port for the domain; ports for the different services will be offsets of the given port number. The following table lists the ports assigned to all the different services.

Service

Port

Admin

portbase + 48

HTTP

portbase + 80

Java Messaging System (JMS)

portbase + 76

Internet Inter-ORB Protocol (IIOP)

portbase + 37

Secure HTTP (HTTPS)

portbase + 81

Secure IIOP

portbase + 38

Mutual Authorization IIOP

portbase + 39

Java Management Extensions (JMX) Administration

portbase + 86

Of course, care must be taken when choosing the value for portbase, making sure that none of the assigned ports collide with any other domain.

Note

As a rule of thumb, creating domains using a portbase number greater than 8000 and divisible by 1000 should create domains that don't conflict with each other; for example, it should be safe to create a domain using a portbase of 9000, another one using a portbase of 10000, so on and so forth.

Deleting Domains

Deleting a domain is very simple; it can be accomplished by issuing the following command in the command line:

asadmin delete-domain domainname

We should see a message like the following on the terminal window:

Domain domainname deleted.

Please use the above command with care; once a domain is deleted, it cannot be easily recreated (all deployed applications will be gone, as well as any connection pools, data sources, etc.).

Stopping a Domain

A domain that is executing can be stopped by issuing the following command:

asadmin stop-domain domainname

The above command will stop the domain named domainname.

Note

If only one domain is running, the domain name argument is optional.

Note

This book will assume the reader is working with the default domain called domain1 and the default ports. If this is not the case, instructions given need to be modified to match the appropriate domain and port.

Setting Up Database Connectivity

Any non-trivial Java EE application will connect to a Relational Database Management Server (RDBMS). Supported RDBMS systems include JavaDB, 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 PostgreSQL; the procedure is similar for others.

Note

GlassFish comes bundled with an RDBMS called JavaDB. This RDBMS is based on Apache Derby. To limit the downloads and configuration needed to follow this book's code, all examples needing an RDBMS will use the embedded JavaDB RDBMS.

Setting Up Connection Pools

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 asadmin stop-domain domain1 followed by executing asadmin start-domain domain1.

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 (assuming the current domain is listening on the default admin port).

Then click on Resources->JDBC->Connection Pools; the browser should now look something like this:

Click on the New... button; after entering the appropriate values for our RDBMS, the page should look something like this:

After entering the appropriate data for the RDBMS and clicking the Next button, you should see a page like the following:

Most of the default values on this page are sensible; scroll all the way down and enter the appropriate data for our RDBMS, then click on the Finish button at the top right of the screen.

Our newly created connection pool should now be visible in the list of connection pools.

After clicking on the JNDI name for the new connection pool, and clicking on the Ping button, you should see a message like the following:

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

Setting Up Data Sources

Java EE applications don't access connection pools directly, instead they access a data source, which points to a connection pool. To set up a new data source, click on the JDBC Resources menu item on the left-hand side of the web console, then click on the New... button.

After filling out the appropriate information for our new data source, you should see a page like this:

After clicking the OK button, you can see our newly created data source:

 

Summary


In this chapter, we discussed how to download and install GlassFish. We also discussed several methods of deploying Java EE application: through the GlassFish web console, through the asadmin command, and by copying the file to the autodeploy directory. We also discussed basic GlassFish administration tasks like setting up domains and setting up database connectivity by adding connection pools and data sources.

About the Author

  • David R. Heffelfinger

    David R. Heffelfinger is an independent consultant based in the Washington D.C. area.

    He is a Java Champion, a member of the NetBeans Dream Team, and is a part of the JavaOne content committee.

    He has written several books on Java EE, application servers, NetBeans, and JasperReports.

    His previous titles include Java EE 7 Development with NetBeans 8, Java EE 7 with GlassFish 4 Application Server, and JasperReports 3.5 For Java Developers, and others.

    David has been a speaker at software conferences such as JavaOne and Oracle Code on multiple occasions.

    He has also been a speaker at NetBeans Day in San Francisco and Montreal, showcasing NetBeans features that greatly enhance the development of Java EE applications. You can follow him on Twitter at @ensode.

    Browse publications by this author
Book Title
Access this book, plus 7,500 other titles for FREE
Access now