Developing a Java EE Application
This tutorial illustrates the Java EE application development workflow.
The application that we are going to develop will be a minimal one. It'll be a one JSP page Java web application. However, the IntelliJ IDEA features shown here are applicable to Java EE applications of any complexity.
- Before you start
- Creating a project
- Exploring the project structure
- Developing source code
- Running the application
- Modifying the code and observing the changes
- Exploring a run configuration
- Exploring an artifact configuration
- Packaging the application into a WAR file
- Deploying an artifact onto a running server
- Packaging the application into an EAR: Using Java EE Application support
- Looking at other features (tool windows and facets)
Before you start
Make sure that the following software is installed on your computer:
- IntelliJ IDEA ULTIMATE Edition.
- GlassFish Server, version 3.0.1 or later. Download GlassFish. (You can use any other Java EE-enabled application server. GlassFish is used here just as an example.)
- A web browser.
Creating a project
- Click Create New Project on the Welcome screen, or select File | New | Project.
- In the left-hand pane, select Java Enterprise.
- Specify the JDK that you are going to use (the Project SDK field): select one from the list, click New and select the JDK installation folder, or click Download JDK.
- Specify your application server. (We'll use GlassFish Server.)
- Under Additional Libraries and Frameworks, select the Web Application check box.
- Specify the name for your new project (e.g. JavaEEHelloWorld).
Exploring the project structure
- JavaEEHelloWorld is a module folder (which in this case coincides with the project
.ideafolder and the file
JavaEEHelloWorld.imlcontain configuration data for your project and module respectively. The folder
srcis for your Java source code. The folder
webis for the web part of your application. At the moment this folder contains the deployment descriptor
WEB-INF/web.xmland the file
index.jspintended as a starting page of your application.
- External Libraries include your JDK and the JAR files for working with GlassFish.
Developing source code
index.jspfor editing: select the file in the Project tool window and press F4.
The code at this step is ready.
Running the application
In the upper-right part of the workspace, click .
IntelliJ IDEA compiles your source code and builds an application artifact.
Finally, your default web browser starts and you see the application output
Hello, World! there.
Modifying the code and observing the changes
- In the Run tool window, click Update .
- In the Update dialog, select Update resources and click OK. (For more information, see Application update options.)
- Switch to the web browser and reload the page to see the changes.
See also, Updating Applications on Application Servers.
Exploring a run configuration
When creating the project, we specified GlassFish as an application server. As a result, IntelliJ IDEA created a run configuration for GlassFish.
When we performed the Run command (), we started that run configuration. Now let's take a look at the run configuration and see how its settings map onto the events that we've just observed.
Click the run configuration selector and select Edit Configurations.
The Before launch task list (in the lower part of the dialog) specifies that the application code should be compiled and the corresponding artifact should be built prior to executing the run configuration.
- Select the Startup/Connection tab to see how the server is started in the run, debug and code coverage modes.
- Select the Deployment tab to see which artifacts are deployed after the server is started.
- Go back to the Server tab.
The settings under Open browser specify that after launch (i.e. after the server is started and the artifacts are deployed onto it) the default web browser should start and go to the specified URL (
The settings to the right of On 'Update' action specify that on clicking in the Run tool window the Update dialog should be shown and the Update resources option should be used by default. (The last used update option becomes the default one).
- Click OK.
Exploring an artifact configuration
When creating the project, we indicated that we were going to develop a web application. As a result, IntelliJ IDEA, among other things, created a configuration for building a web application artifact. Let's have a look at this configuration.
- Open the Project Structure dialog: File | Project Structure or Ctrl+Shift+Alt+S.
Under Project Settings, select Artifacts.
The available artifact configurations are shown in the pane to the right under and . (There's only one configuration at the moment.)
The artifact settings are shown in the right-hand part of the dialog.
Packaging the application into a WAR file
When you get to the stage when you are happy with your application, you may want to place it in a WAR (web application archive). To do that, you should create an appropriate artifact configuration and then build the artifact:
Click , point to Web Application: Archive and
select For 'JavaEEHelloWorld: war exploded'.
A new artifact configuration is created and its settings are shown in the right-hand part of the dialog.
Create a manifest file for your archive:
click Create Manifest and
agree to the location suggested by IntelliJ IDEA (
- Click OK in the Project Structure dialog.
- Select Build | Build Artifacts.
In the Build Artifact popup,
point to JavaEEHelloWorld:war and
Now if you look at the
out/artifacts/JavaEEHelloWorld_warfolder, you'll see the archive there.
Deploying an artifact onto a running server
Sometimes you need to deploy your app onto a running server. This section provides a how-to example.
Server run configurations that don't start a server are called remote. Such run configurations can be used, for example, for deploying applications to servers that are already running. (See Local and remote run configurations.)
Let's create a run configuration for deploying our WAR artifact to the running server and see how it works. (By now, the server has been started by the run configuration discussed earlier.)
- Click the run configuration selector and select Edit Configurations.
- Click , point to GlassFish Server and select Remote.
- Change the run configuration name Unnamed to something more sensible (e.g. GlassFishRemote).
Specify the artifact to be deployed to the server: select the Deployment tab,
click and select Artifact.
In the dialog that opens, select the WAR artifact.
The result should look similar to this:
Click OK in the Run/Debug Configurations dialog.
Now let's see how this run configuration works.
Execute the run configuration: click .
After a while, a new tab in your web browser opens, and you see the application output there.
Packaging the application into an EAR: Using Java EE Application support
To package your Java EE application into an EAR, you should:
Create a Java EE deployment descriptor
- Configure an EAR artifact.
- Build that artifact.
As we are about to see, IntelliJ IDEA performs most of these tasks for you as part of its Java EE Application support:
- In the Project tool window, right-click your module folder and select Add Framework Support.
In the dialog that opens,
select the JavaEE Application check box and click OK.
Note the descriptor file
META-INF/application.xmlcreated in your module folder.
Open the file in the editor (F4).
At the moment, the file is almost empty.
Now let's look at the artifact configurations.
Note that a new configuration appeared, the one for an exploded EAR artifact.
Currently only JavaEE Application facet resources (
META-INF/application.xml) are included in the artifact.
- Let's add a copy of the exploded WAR artifact to the EAR artifact structure. To do that, under Available Elements, expand the Artifacts node and double-click the exploded WAR artifact. Here is the result.
Note the message Web facet isn't registered in application.xml. Click Fix.
(A bit later, we'll look at the changes made to
application.xmlby this quick fix.)
- Create a configuration for an EAR artifact: .
To create a manifest file, click Create Manifest and agree to the default file location
Click OK in the Project Structure dialog.
See that your
application.xmlhas changed. This is the result of applying the quick fix.
Let's now create a run configuration for building and deploying the EAR artifact.
- Click the run configuration selector and select Edit Configurations. Then, in the Run/Debug Configurations dialog, select .
- Specify a descriptive name for your run configuration, e.g. GlassFishRemoteEAR.
- Include the EAR artifact in the deployment list: switch onto the Deployment tab and select .
Switch to the Server tab and check the URL in the Open browser section.
The part that follows
http://localhost:8080/should correspond to the
<context-root>element in your
- Click OK in the Run/Debug Configurations dialog.
Execute the run configuration ().
Then, the application output is shown in the browser.
Looking at other features (tool windows and facets)
As part of its Web Application and Java EE Application support, IntelliJ IDEA:
- Made the Web and JavaEE:App tool windows available.
- Created the Web and Java EE Application facets.
Very briefly, the Web and JavaEE:App tool windows provide the functions similar to those of the Project tool window but only for your Web and Java EE Application facet resources respectively. For more info, see: