Java EE Development with Eclipse - Second Edition

5 (1 reviews total)
By Ram Kulkarni
  • 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. Introducing JEE and Eclipse

About this book

Java EE is a technology for developing enterprise class, scalable applications. With recent changes to Java EE specifications, JEE application development has become a lot simpler. However, recent changes have also added many new specifications, some of which compete with existing JEE specification. Along with JEE specifications and APIs, it is also very important to understand the entire application development process and tools that can help simplify and accelerate JEE application development.

This guide provides a complete overview of developing JEE applications using Eclipse. The many features of the Eclipse IDE are explained. These enable the rapid development, debugging, testing, and deployment of JEE applications. You’ll explore not just different JEE technologies and how to use them (JSP, JSF, JPA, JDBC, EJB, web services etc.), but also suitable technologies for different scenarios.

The book starts with how to set up the development environment for JEE applications and then goes on to describe many JEE specifications in detail, with an emphasis on examples. You’ll learn how to deploy an example application on Tomcat and Glassfish Application Server.

You’ll create a simple application that reads from a queue, processes the request, and publishes results to a topic and Eclipse MAT (Memory Analysis Tool) to debug memory issues.

Publication date:
September 2015
Publisher
Packt
Pages
444
ISBN
9781785285349

 

Chapter 1. Introducing JEE and Eclipse

Java Enterprise Edition (JEE, which was earlier called J2EE) has been around for many years now. It is a very robust platform for developing enterprise applications. J2EE was first released in 1999, but underwent major changes in version 5, in 2006. Since version 5, it has been renamed Java Enterprise Edition (JEE). Recent versions of JEE has made developing a multi-tier distributed application a lot easier. J2EE had focused on core services and had left the tasks that made application development easier to external frameworks, for example, MVC and persistent frameworks. But JEE has brought many of these frameworks in the core services. Along with the support for annotations, these services simplify application development to a large extent.

Any runtime technology is not good without great development tools. Integrated Development Environment (IDE) plays a major part in developing applications faster, and Eclipse provides just that for JEE. Not only do you get a good editing support in Eclipse, but you also get support for build, unit testing, version control, and many other tasks important in different phases of software application development.

The goal of this book is to show how you can efficiently develop JEE application using Eclipse by using many of its features during different phases of the application development. But first, the following is a brief introduction to JEE and Eclipse.

 

Java Enterprise Edition (JEE)


JEE is a collection of many different specifications intended to perform specific tasks. These specifications are defined by the Java Community Process (https://www.jcp.org) program. Currently, JEE is in version 7. However, different specifications of JEE are at their own different versions.

JEE specifications can be broadly classified in the following groups:

  • Presentation layer

  • Business layer

  • Enterprise integration layer

Note that JEE specification does not necessarily classify APIs in such broad groups, but such classification could help in better understanding the purpose of the different standards and APIs in JEE. Before we see APIs in each of these categories, let's understand a typical JEE web application flow where each of these layers fits in.

Figure 1.1 A typical JEE web application flow

Requests start from client. Client can be any application requesting services from a remote application – for example, it could be a browser or a desktop application. The request is first received by Web Server at the destination. Examples of Web Servers are Apache Web Server, IIS, Nginx, and so on. If it is a request for static content, then it is served by the web server(s). However, dynamic request typically requires an Application Server to process it. JEE servers are such Application Servers that handle the dynamic requests. Most JEE specification APIs execute in the application server. Examples of JEE application servers are WebLogic, WebSphere, GlassFish, JBoss, and so on.

Most non-trivial JEE applications access external systems such as database or Enterprise Integration Server (EIS) for data and process it. Response is returned from the application server to the web server and then to the clients.

The following is the brief description of each of the JEE specifications in different layers of applications that we saw previously. We will see how to use these APIs in more detail in subsequent chapters. However, note that the following is not the exhaustive list of all the specifications in JEE. We will see the most commonly used specifications here. For the exhaustive list, please visit http://www.oracle.com/technetwork/java/javaee/tech/index.html.

The presentation layer

JEE specifications or technologies in this group receive the request from web server and send back the response, typically, in an HTML format. However, it is also possible to return only the data from the presentation layer, for example, in JavaScript Object Notation (JSON) or eXtensible Markup Language (XML) format, which could be consumed by AJAX (Asynchronous JavaScript and XML) calls to update only part of the page, instead of rendering the entire HTML page. Classes in the presentation layer are mostly executed in a Web Container – it is a part of the application server that handles web requests. Tomcat is an example of a popular Web Container.

Now, we will take a look at some of the specifications in this group.

Java Servlet

Java servlets are server side modules, typically used to process a request and send back response in the web applications. Servlets are useful for handling requests that do not generate large HTML markup responses. They are typically used as controllers in MVC (Model View Controller) frameworks, for forwarding/redirecting requests or for generating non-HTML responses, such as PDFs. To generate an HTML response from Servlet, you need to embed the HTML code (as Java String) in the Java code. Therefore, it is not the most convenient option for generating large HTML response. JEE 7 contains Servlet API 3.1.

Java Server Pages

Like Servlets, JSPs are also server side modules used to process the web requests. JSPs (Java Server Pages) are great for handling requests that generate large HTML markup responses. In JSP pages, Java code or JSP tags can be mixed with other HTML code, such as HTML tags, JavaScript, and CSS. Since Java code is embedded in the larger HTML code, it is easier (than Servlet) to generate an HTML response from the JSP pages. JSP specification 2.3 is included in JEE 7.

Java Server Faces

Java Server Faces makes creating user interface on the server side modular by incorporating the MVC design pattern in its implementation. It also provides easy to use tags for common user interface controls that can save states across multiple request-response exchanges between the client and server. For example, if you have a page that posts form data from a browser, you can have JSF save that data in a Java Bean so that it can be used subsequently in the response to the same or different request. JSF also makes it easier to handle UI events on the server side and specify page navigation in an application.

You write the Java Server Faces (JSF) code in JSP, using custom JSP tags created for JSF. Java Server Faces API 2.2 is part of JEE 7.

The business layer

The business layer is where you typically write code to handle the business logic of your application. The request to this layer could come from the presentation layer, directly from the client application, or from the middle layer consisting of, but not limited to, web services. Classes in this layer are executed in the application container part of JEE Server. GlassFish and WebSphere are examples of web container plus application container.

Let us take a tour of some of the specifications in this group.

Enterprise Java Beans

Enterprise Java Beans (EJBs) are the Java classes where you can write your business logic. Though it is not a strict requirement to use EJBs to write business logic, they do provide many of the services that are essential in enterprise applications. These services are security, transaction management, component lookup, object pooling, and so on. You can have EJBs distributed across multiple servers and let the application container (also called EJB container) take care of component look up (searching component) and component pooling (useful for scalability). This can improve scalability of the application.

EJBs are of two types:

  • Session beans: Session beans are called directly by clients or middle tier objects

  • Message driven beans: Message driven beans are called in response to Java Messaging Service (JSM) events

JMS and message driven beans can be used for handling asynchronous requests. In a typical asynchronous request processing scenario, the client puts a request in a messaging queue or a topic and does not wait for immediate response. Server application gets the request message, either directly using JMS APIs or by using MDB. It processes the request and may put response in a different queue or topic to which the client would listen and get the response.

Java EE 7 contains EJB specification 3.2 and JMS specification 2.0.

The enterprise integration layer

APIs in this layer are used for interacting with external (to JEE application) systems in Enterprise. Most applications would need to access database, and APIs to access it fall in this group.

Java Database Connectivity (JDBC)

JDBC is a specification to access relational database in a common and consistent way. Using JDBC you can execute SQL statements and get results on different databases using common APIs. Database specific driver sits between the JDBC call and the database, which translates JDBC calls to database vendor specific API calls. JDBC can be used in both the Presentation and Business layers directly, but it is recommended to separate the database calls from both UI and the business code. Typically, this is done by creating Data Access Objects (DAO) which encapsulate logic to access the database.

JEE 7 contains JDBC specification 4.0.

The Java Persistent API (JPA)

One of the problems of using JDBC APIs directly is that you have to constantly map the data between Java Objects and the data in columns of rows in relational database. Frameworks such as Hibernate and Spring have made this process simpler by using a concept known as Object Relationship Mapping (ORM). ORM is incorporated in JEE in the form of Java Persistent API (JPA). JPA gives you the flexibility to map the objects to the tables in relational database and execute the queries with or without using Structured Query Language (SQL). Though when used in the content of JPA, query language is called Java Persistence Query Language. JPA specification 2.1 is a part of JEE.

Java Connector Architecture (JCA)

JCA APIs can be used in JEE applications for communicating with Enterprise Integration Systems, such as SAP, Salesforce, and so on. Just like you have database drivers to broker communication between JDBC APIs and relational database, you have JCA adapters between JCA calls and EIS. Most EIS applications now provide REST APIs, which are lightweight and easy to use, so REST could replace JCA in some cases. However, if you use JCA, you get transaction and pooling support from JEE application server.

Web services

Web services are remote application components that expose self-contained APIs. Broadly, web services can be classified based on the following two standards:

  • Simple Object Access Protocol (SOAP)

  • Representational State Transfer (REST)

Web services can play a major role in integrating disparate applications, because they are standard based and platform independent.

JEE provides many specifications to simplify development and consumption of both types of web services, for example, JAX-WS (Java API for XML – web services) and JAX-RS (Java API for RESTful web services).

The preceding are just some of the specifications that are part of JEE. There are many other independent specifications, such as web services, and many enabling specifications, such as dependency injection and concurrency utilities, that we will see in subsequent chapters.

Eclipse IDE

As mentioned earlier, a good IDE is essential for better productivity while coding. Eclipse is one such IDE, which has great editor features and many integration points with JEE technologies. The primary purpose of this book is to show you how to develop JEE applications using Eclipse. So following is a quick introduction to Eclipse, if you are not already familiar with it.

Eclipse is an open source IDE for developing applications in many different programming languages. It is quite popular for developing many different types of Java applications. Its architecture is pluggable – there is a core IDE and many different plugins can be added to it. In fact, support for many languages is added as Eclipse plugins, including support for Java.

Along with editor support, Eclipse has plugins to interact with many of the external systems used during development. For example, source control systems such as SVN and Git, build tools such as Apache Ant and Maven, file explorer for remote systems using FTP, managing servers such as Tomcat and GlassFish, database explorer, memory and CPU profiler, and so on. We will see many of these features in the subsequent chapters.

Figure 1.2 Default Eclipse View

Figure 1.2 shows the default view of Eclipse for JEE application development. When working with Eclipse, it is good to understand the following terms used in the context of Eclipse.

Workspace

The Eclipse workspace is a collection of projects, settings, and preferences. Workspace is a folder where Eclipse stores this information. You must create a workspace to use Eclipse. You can create multiple workspaces, but at a time only one can be opened by one running instance of Eclipse. However, you can launch multiple instances of Eclipse with different workspaces.

Plugin

Eclipse has pluggable architecture. Many of the features of Eclipse are implemented as plugins, for example, editor plugins for Java and many other languages, plugins for SVN and Git, and many others. Default installation of Eclipse comes with many built-in plugins and you can add more plugins for the features you want later.

Editors and views

Most windows in Eclipse can be classified either as editor or views. Editor is something where you can change the information displayed in it. View just displays the information and does not allow you to change it. An example of an editor is the Java editor where you write a code. An example of view is the outline view that displays the hierarchical structure of the code you are editing (in case of Java editor, it shows classes in a file, and methods in them).

To see all views in a given Eclipse installation, open the Window | Show View | Other menu.

Figure 1.3 Show all Eclipse Views

Perspective

Perspective is a collection of editors and views, and how they are laid out or arranged in the main Eclipse window. At different stages of development, you need different views to be displayed. For example, when you are editing a code, you need to see the Project Explorer and Task views, but when you are debugging an application, you don't need those views, but instead want to see the variables and breakpoints view. So, the editing perspective displays, among other views and editor, the Project Explorer and Task view and the Debug perspective displays views and editors relevant to the debugging activities. You can change the default perspectives to suit your purpose, though.

Eclipse preferences

The Eclipse preferences window is where you customize many features of plugins/features. Preferences are available from the Window menu in Windows and Linux installation of Eclipse, and from Eclipse menu in Mac installation of Eclipse.

Figure 1.4 Eclipse Preferences

 

Installing products


In the subsequent chapters, we will see how to develop JEE applications using different APIs in Eclipse. But the applications are going to need a JEE application server and a database. We are going to use Tomcat web container for the initial few chapters and then use GlassFish JEE application server. We are going to use MySQL database. We are going to need these products for most 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 (Version 4.4)

You can download 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 if the JVM is 32-bit or 64-bit.

Unzip the downloaded zip file and then run the Eclipse application (you need to 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 file system and select that as the initial workspace folder. If you intend to use the same folder for workspace on every launch of Eclipse, then check the Use this as the default and do not ask again check box.

Figure 1.5 Select Eclipse Workspace

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

Installing Tomcat

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

Download Tomcat from http://tomcat.apache.org/. At the time of writing, the latest version of Tomcat available was 8. Download the zip file and unzip 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 sub folders). Then run startup.bat at the Command Prompt on Windows and startup.sh in a Terminal window on Mac and Linux, to start the Tomcat server. If there are no errors, then you should see the message Server startup in --ms or Tomcat started.

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 connector declaration like:

<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 in any text editor. At the end of the file you will see commented example of how to add users. Add the following configuration before closure of the </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. For more information for managing Tomcat server, see 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 port number if you have changed the default port as described previously). 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 user name and password. Enter the user name 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 the following image. You can see 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/COMMAND + C or run 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 Full Platform. See https://glassfish.java.net/webprofileORfullplatform31x.html for comparison of Web Profile and Full Platform.

Unzip the downloaded file in a folder. Default port of GlassFish server is also 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 screenshot:

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 preceding page to open GlassFish administrator. For details on administrating GlassFish server, see https://glassfish.java.net/docs/4.0/administration-guide.pdf.

Figure 1.9 The GlassFish administrator

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

Installing MySQL

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

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. Following instructions show the download options using the web installer.

Web installer first downloads a small application, and when you run that, it gives you options to select components that you want to install.

We would like to install MySQL Workbench too, which is a client application to manage MySQL Server. As of writing this chapter, MySQL Workbench required Visual C++ 2013 Runtime for Windows installation. If you don't have it already installed, you can download it from http://www.microsoft.com/en-in/download/details.aspx?id=40784.

  1. Select the Custom option and click on Next.

    Figure 1.10 MySQL Installer for Windows

  2. Select MySQL Server and MySQL Workbench products and complete the installation. During the installation of 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 user other than root for applications to use.

    Figure 1.11 Select MySQL Products and Features to Install

  3. Make sure you select All Hosts when adding a user so that you are able to access MySQL database from any remote machine that has network access to the machine where MySQL is installed.

    Figure 1.12 Add MySQL User

  4. 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

  5. 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 Mac OS X

OS X versions before 10.7 had MySQL server installed by default. See http://dev.mysql.com/doc/mysql-macosx-excerpt/5.7/en/macosx-installation-server.html to know which version of MySQL was installed for different versions of OS X. If you are using OS X 10.7 or later, then you will have 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 instruction for OS X. Note that users on OS X should have administrator privileges to install the MySQL server.

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

  1. To start it from the Command Prompt, execute the following command in Terminal:

    sudo /usr/local/mysql/support-files/mysql.server start
    
  2. To start it from System Preferences, open the preferences and click the MySQL icon.

    Figure 1.15 MySQL System Preferences - OSX

  3. Click the Start MySQL Server button.

  4. Next, download MySQL Workbench for OSX from http://dev.mysql.com/downloads/workbench/.

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 user either from the Command Prompt or by using MySQL Workbench.

  1. To execute SQL and other commands from the Command Prompt, open Terminal and type the following:

    mysql -u root -p <root_password>
    
  2. Once logged in successfully, you will see the mysql Command Prompt:

    mysql>
    
  3. To create a user, first select the mysql database.

    mysql>user mysql;
    Database changed
    mysql>insert into user (host, user, password, select_priv, insert_priv, update_priv)
               values ('%', 'user1', password('usper1_pass'),'Y','Y','Y');
    

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

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

Figure 1.16 Creating a user in MySQL Workbench

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

 

Summary


In this chapter, we had a brief introduction to different JEE APIs for the presentation layer, business layer, and Enterprise integration layer. We learnt some of the important terminologies in Eclipse IDE. We then learned how to install Eclipse, Tomcat, GlassFish, MySQL, and MySQL Workbench. We are going to use these products in this book to develop JEE applications.

In the next chapter, we will configure the JEE server and database in Eclipse, and create a simple application using Servlet, JSP, and Java Server Faces.

We will also see how to crate JEE Web Applications using Servlet, JSP, and JSF. Along with that, we will learn how to use Maven to build and package the JEE applications.

About the Author

  • Ram Kulkarni

    Ram Kulkarni has more than two decades of experience in developing software. He has architected and developed many enterprise web applications, client-server and desktop applications, application servers, and IDE and mobile applications. He is also the author of Eclipse 4 RCP Development How-To, published by Packt Publishing.

    Browse publications by this author

Latest Reviews

(1 reviews total)
Excellent