Book Image

Java EE 8 Development with Eclipse - Third Edition

By : Ram Kulkarni
Book Image

Java EE 8 Development with Eclipse - Third Edition

By: Ram Kulkarni

Overview of this book

Java EE is one of the most popular tools for enterprise application design and development. With recent changes to Java EE 8 specifications, Java EE application development has become a lot simpler with the new specifications, some of which compete with the existing specifications. This guide provides a complete overview of developing highly performant, robust and secure enterprise applications with Java EE with Eclipse. The book begins by exploring different Java EE technologies and how to use them (JSP, JSF, JPA, JDBC, EJB, and more), along with suitable technologies for different scenarios. You will learn how to set up the development environment for Java EE applications and understand Java EE specifications in detail, with an emphasis on examples. The book takes you through deployment of an application in Tomcat, GlassFish Servers, and also in the cloud. It goes beyond the basics and covers topics like debugging, testing, deployment, and securing your Java EE applications. You'll also get to know techniques to develop cloud-ready microservices in Java EE.
Table of Contents (20 chapters)
Title Page
Copyright and Credits
Dedication
Packt Upsell
Free Chapter
1
Introducing JEE and Eclipse
Index

Installing products


In the subsequent chapters, we will learn how to develop JEE applications in Eclipse. But the applications are going to need a JEE application server and a database. We are going to use the Tomcat web container in the initial few chapters and then use the GlassFish JEE application server. We are going to use a MySQL database.

We are going to need these products for many of the applications that we are going to develop. So the following sections describe how to install and configure Eclipse, Tomcat, GlassFish, and MySQL.

Installing Eclipse

Download the latest version of Eclipse from https://eclipse.org/downloads/. You will see many different packages for Eclipse. Make sure you install the Eclipse IDE for Java EE Developers package. Select an appropriate package based on your OS and JVM architecture (32 or 64 bit). You may want to run the command java -version to know whether the JVM is 32-bit or 64-bit.

Note

If you plan to use Eclipse for AWS development, then it is recommended to download Eclipse from the Oomph installer. Refer to https://wiki.eclipse.org/Eclipse_Installer and https://docs.aws.amazon.com/toolkit-for-eclipse/v1/user-guide/setup-install.html.

Unzip the downloaded ZIP file and then run the Eclipse application (you must install JDK before you run Eclipse). The first time you run Eclipse, you will be asked to specify a workspace. Create a new folder in your filesystem and select that as the initial workspace folder. If you intend to use the same folder for the workspace on every launch of Eclipse, then check the Use this as the default and do not ask again checkbox:

Figure 1.5: Select Eclipse workspace

You will then see the default Java EE perspective of Eclipse as shown in Figure 1.2.

Installing the Tomcat server

Tomcat is a web container. It supports APIs in the presentation layer described earlier. In addition, it supports JDBC and JPA. It is easy to configure and could be a good option if you do not want to use EJBs.

Download the latest version of Tomcat from http://tomcat.apache.org/. Unzip the downloaded file in a folder. Set the JAVA_HOME environment variable to point to the folder where JDK is installed (the folder path should be the JDK folder, which has bin as one of the subfolders). To start the server, run startup.bat in Command Prompt on Windows and startup.sh in a Terminal window on Mac and Linux. If there are no errors, then you should see the message Server startup in --ms or Tomcat started.

The default Tomcat installation is configured to use port 8080. If you want to change the port, open server.xml under the conf folder and look for a connector declaration such as the following:

<Connector port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" /> 

Change the port value to any port number you want, though in this book we will be using the default port 8080. Before we open the default page of Tomcat, we will add a user for administration of the Tomcat server. Open tomcat-users.xml under the conf folder using any text editor. At the end of the file, you will see commented example of how to add users. Add the following configuration before the closure of </tomcat-users> tag:

  <role rolename="manager-gui"/> 
  <user username="admin" password="admin" roles="manager-gui"/> 

Here, we are adding a user admin, with password also as admin, to a role called manager-gui. This role has access to web pages for managing an application in Tomcat. This and other security roles are defined in web.xml of the manager application. You can find it at webapps/manager/WEB-INF/web.xml.

Note

For more information for managing Tomcat server, refer to http://tomcat.apache.org/tomcat-8.0-doc/manager-howto.html.

After making the preceding changes, open a web browser and browse to http://localhost:8080 (modify the port number if you have changed the default port). You will see the following default Tomcat page:

Figure 1.6: The default Tomcat web application

Click on the Manager App button on the right. You will be asked for the username and password. Enter the username and password you configured in tomcat-users.xml for manager-gui, as described earlier. After you are successfully logged in, you will see the Tomcat Web Application Manager page, as shown in Figure 1.7. You can see all the applications deployed in Tomcat in this page. You can also deploy your applications from this page:

Figure 1.7: Tomcat Web Application Manager

To stop the Tomcat server, press Ctrl/cmd + C or run the shutdown script in the bin folder.

Installing the GlassFish server

Download GlassFish from https://glassfish.java.net/download.html. GlassFish comes in two flavors: Web Profile and Full Platform. Web Profile is like Tomcat, which does not include EJB support. So download the Full Platform. Unzip the downloaded file in a folder. The default port of the GlassFish server is 8080. If you want to change that, open glassfish/domains/domain1/config/domain.xml in a text editor (you could open it in Eclipse too, using the File | Open File menu option) and look for 8080. You should see it in one of the <network-listener>. Change the port if you want to (which may be the case if some other application is already using that port).

To start the server, run the startserv script (.bat or .sh depending on the OS you use). Once the server has started, open a web browser and browse to http://localhost:8080. You should see a page like the following:

Figure 1.8: The default Glassfish web application

This page is located at glassfish/domains/domain1/docroot/index.html. Click on the go to the Administration Console link in the page to open the GlassFish administrator (see the following screenshot):

Figure 1.9: The Glassfish administrator

Note

For details on administrating the GlassFish server, refer to https://javaee.github.io/glassfish/doc/5.0/administration-guide.pdf.

To stop the GlassFish Server, run the stopserv script in the glassfish/bin folder.

Installing MySQL

We will be using a MySQL database for many of the examples in this book. The following sections describe how to install and configure MySQL for different platforms.

We would like to install MySQL Workbench too, which is a client application to manage MySQL Server. Download MySQL Workbench from https://dev.mysql.com/downloads/workbench/.

Installing MySQL on Windows

Download MySQL Community Server from http://dev.mysql.com/downloads/mysql/. You can either download the web installer or the all-in-one installer. The web installer would download only those components that you have selected. The following instructions show the download options using the web installer.

The web installer first downloads a small application, and it gives you options to select the components that you want to install:

  1. Select the Custom option and click on Next:

Figure 1.10: MySQL Installer for Windows

  1. Select the MySQL Server and MySQLWorkbench products and complete the installation. During the installation of the server, you will be asked to set the root password and given the option to add more users. It is always a good idea to add a user other than root for applications to use:

Figure 1.11: Select MySQL products and features to Install

  1. Make sure you selectAll Hostswhen adding a user so that you are able to access MySQL database from any remote machine that hasnetworkaccess to themachinewhere MySQL is installed:

Figure 1.12: Add MySQL user

  1. Run MySQL Workbench after installation. You will find that the default connection to the local MySQL instance is already created for you:

Figure 1.13: MySQL Workbench connections

  1. Click on the local connection and you will be asked to enter the root password. Enter the root password that you typed during the installation of MySQL Server. MySQL Workbench opens and displays the default test schema:

Figure 1.14: My SQL Workbench

Installing MySQL on macOS X

OS X versions before 10.7 had MySQL Server installed by default. If you are using OS X 10.7 or later, then you will need to download and install MySQL Community Server from http://dev.mysql.com/downloads/mysql/.

There are many different ways to install MySQL on OS X. See http://dev.mysql.com/doc/refman/5.7/en/osx-installation.html for installation instructions for OS X. Note that users on OS X should have administrator privileges to install MySQL Server.

Once you install the server, you can start it either from Command Prompt or from the system preferences:

  1. To start it from Command Prompt, execute the following command in the Terminal:
sudo /usr/local/mysql/support-files/mysql.server start
  1. To start it from System Preferences, open the preferences and click the MySQL icon:

Figure 1.15: MySQL System Preferences - OS X

  1. Click the Start MySQL Server button.

Installing MySQL on Linux

There are many different ways to install MySQL on Linux. Refer to https://dev.mysql.com/doc/refman/5.7/en/linux-installation.html for details.

Creating MySQL users

You can create MySQL users either from Command Prompt or by using MySQL Workbench:

  1. To execute SQL and other commands from Command Prompt, open the Terminal and type the following command:
mysql -u root -p<root_password>
  1. Once logged in successfully, you will see the mysql Command Prompt:
mysql>
  1. To create a user, first select the mysql database:
mysql>use mysql;Database changedmysql>create user 'user1'@'%' identified by 'user1_pass';
mysql>grant all privileges on *.* to 'user1'@'%' with grant option

The preceding command will create a user named 'user1' with password 'user1_pass' having all privileges, for example to insert, update, and select from the database. And because we have specified the host as '%', this user can access the server from any host.

Note

See https://dev.mysql.com/doc/refman/5.7/en/adding-users.html for more details on adding users to MySQL database

If you prefer a graphical user interface (GUI) to manage the users, then run MySQL Workbench, connect to the local MySQL server (see Figure 1.13 MySQL Workbench connections), and then click on Users and Privileges under the Management section:

Figure 1.16: Creating a user in MySQL Workbench

Having installed all the preceding products, you should be in a position to start developing JEE applications. We may need some additional software, but we will see how to install and configure it at the appropriate time.