IntelliJ IDEA 2016.3 Help

Tutorial: Developing a Java EE application

This tutorial illustrates the Java EE application development workflow.

The application that we will 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

Make sure that the following software is installed on your computer:

  • IntelliJ IDEA ULTIMATE Edition.
  • Java SE Development Kit (JDK), version 6 or later. Download Oracle JDK.
  • 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

  1. Click Create New Project on the Welcome screen, or select File | New | Project.

    The New Project wizard opens.

  2. In the left-hand pane, select Java Enterprise.
  3. If the JDK that you want to use is already defined in IntelliJ IDEA, select that JDK from the Project SDK list. Otherwise, click New, select JDK, and select the JDK installation folder in the dialog that opens.
  4. Specify the application server that you are going to use. (We'll use GlassFish Server.)

    If GlassFish is not defined in IntelliJ IDEA yet, click New to the right of the Application Server field and select Glassfish Server.

    In the Glassfish Server dialog, specify the GlassFish Server installation directory.

  5. Under Additional Libraries and Frameworks, select the Web Application check box.
    /help/img/idea/2016.3/HWJEE009NewProjectFirstPageFinal.png

    Click Next.

  6. Specify the name for your new project (e.g. JavaEEHelloWorld).
    /help/img/idea/2016.3/HWJEE010NewProjectName.png

    Click Finish and wait while IntelliJ IDEA is creating the project.

Exploring the project structure

When the project is created, you'll see something similar to this in the Project tool window.

/help/img/idea/2016.3/HWJEE011ProjectInit.png
  • JavaEEHelloWorld is a module folder (which in this case coincides with the project folder). The .idea folder and the file JavaEEHelloWorld.iml contain configuration data for your project and module respectively. The folder src is for your Java source code. The folder web is for the web part of your application. At the moment this folder contains the deployment descriptor WEB-INF/web.xml and the file index.jsp intended as a starting page of your application.
  • External Libraries include your JDK and the JAR files for working with GlassFish.

Developing source code

Our application will be a single JSP page application. Its only function will be to output the text Hello, World!

  1. Open index.jsp for editing: select the file in the Project tool window and press F4.
  2. Between <body> and </body> type Hello, World!
    /help/img/idea/2016.3/HWJEEIndexJSPHelloWorld.png

    The code at this step is ready.

Running the application

In the upper-right part of the workspace, click /help/img/idea/2016.3/run.png.

/help/img/idea/2016.3/HWJEE019Run.png

IntelliJ IDEA compiles your source code and builds an application artifact.

After that, the Run tool window opens. IntelliJ IDEA starts the server and deploys the artifact onto it.

/help/img/idea/2016.3/HWJEE020RunToolWindow.png

Finally, your default web browser starts and you see the application output Hello, World! there.

/help/img/idea/2016.3/HWJEE021HelloWorldInBrowser.png

Modifying the code and observing the changes

  1. In index.jsp, change Hello, World! to Hello!.
    /help/img/idea/2016.3/HWJEE022ModifyCode.png
  2. In the Run tool window, click Update /help/img/idea/2016.3/update_icon.png.
    /help/img/idea/2016.3/HWJEE023RunToolWindowUpdate.png
  3. In the Update dialog, select Update resources and click OK. (For more information, see Application update options.)
    /help/img/idea/2016.3/HWJEE024UpdateDialog.png
  4. Switch to the web browser and reload the page to see the changes.
    /help/img/idea/2016.3/HWJEE025HelloInBrowser.png

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 (/help/img/idea/2016.3/run.png), 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.

  1. Click the run configuration selector and select Edit Configurations.
    /help/img/idea/2016.3/HWJEE026EditConfigurations.png

    The Run/Debug Configurations dialog opens and the settings for the GlassFish run configuration are shown.

    /help/img/idea/2016.3/HWJEE027RunDebugConfigurations.png

    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.

    /help/img/idea/2016.3/HWJEE028RunConfigBeforeLaunch.png
  2. Select the Startup/Connection tab to see how the server is started in the run, debug and code coverage modes.
    /help/img/idea/2016.3/HWJEE030RunConfigStartupConnection.png
  3. Select the Deployment tab to see which artifacts are deployed after the server is started.
    /help/img/idea/2016.3/HWJEE029RunConfigDeployment.png
  4. 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 (http://localhost:8080/JavaEEHelloWorld_war_exploded).

    The settings to the right of On 'Update' action specify that on clicking /help/img/idea/2016.3/update_icon.png 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).

    /help/img/idea/2016.3/HWJEE028RunConfigServer.png
  5. 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.

  1. Open the Project Structure dialog: File | Project Structure or Ctrl+Shift+Alt+S.
  2. Under Project Settings, select Artifacts.

    The available artifact configurations are shown in the pane to the right under /help/img/idea/2016.3/new.png and /help/img/idea/2016.3/delete.png. (There's only one configuration at the moment.)

    /help/img/idea/2016.3/HWJEE032ProjectStructureArtifacts.png

    The artifact settings are shown in the right-hand part of the dialog.

    /help/img/idea/2016.3/HWJEE033ProjectStructureArtifactSettings.png

    Type. The artifact type is Web Application: Exploded. This is a decompressed web application archive (WAR), a directory structure that is ready for deployment onto a web server.

    Output directory. The artifact, when built, is placed into <project_folder>/out/artifacts/JavaEEHelloWorld_war_exploded.

    Output Layout. The artifact structure is shown in the left-hand pane of the Output Layout tab.

    The <output root> corresponds to the output directory. Other elements have the following meanings:

    • 'JavaEEHelloWorld' compile output represents compiled Java classes whose sources are located in the src directory. These are placed into WEB-INF/classes in the output directory.
    • 'Web' facet resources represent the contents of the web directory.

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:

  1. Click /help/img/idea/2016.3/new.png, point to Web Application: Archive and select For 'JavaEEHelloWorld: war exploded'.
    /help/img/idea/2016.3/HWJEE034ProjectStructureNewArtifact.png

    A new artifact configuration is created and its settings are shown in the right-hand part of the dialog.

    /help/img/idea/2016.3/HWJEE035ProjectStructureNewArtifactSettings.png
  2. Create a manifest file for your archive: click Create Manifest and agree to the location suggested by IntelliJ IDEA (web/META-INF/MANIFEST.MF).
  3. Click OK in the Project Structure dialog.
  4. Select Build | Build Artifacts.
    /help/img/idea/2016.3/HWJEE036BuildArtifacts.png
  5. In the Build Artifact popup, point to JavaEEHelloWorld:war and select Build.
    /help/img/idea/2016.3/HWJEE037BuildArtifactPopup.png

    Now if you look at the out/artifacts/JavaEEHelloWorld_war folder, you'll see the archive there.

    /help/img/idea/2016.3/HWJEE038ArtifactInProjectToolWindow.png

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

  1. Click the run configuration selector and select Edit Configurations.
    /help/img/idea/2016.3/HWJEE026EditConfigurations.png
  2. Click /help/img/idea/2016.3/new.png, point to GlassFish Server and select Remote.
    /help/img/idea/2016.3/HWJEE039NewRemoteServerRunConfig.png
  3. Change the run configuration name Unnamed to something more sensible (e.g. GlassFishRemote).
    /help/img/idea/2016.3/HWJEE040RunConfigName.png
  4. Specify the artifact to be deployed to the server: select the Deployment tab, click /help/img/idea/2016.3/new.png and select Artifact.
    /help/img/idea/2016.3/HWJEE041DeploymentPlusArtifact.png

    In the dialog that opens, select the WAR artifact.

    /help/img/idea/2016.3/HWJEE042SelectArtifact.png

    The result should look similar to this:

    /help/img/idea/2016.3/HWJEE043ArtifactDefined.png
  5. Click OK in the Run/Debug Configurations dialog.

    Now let's see how this run configuration works.

  6. Execute the run configuration: click /help/img/idea/2016.3/run.png.
    /help/img/idea/2016.3/HWJEE044Run.png

    The run configuration output is shown in the Run tool window.

    /help/img/idea/2016.3/HWJEE045RunToolWindow.png

    After a while, a new tab in your web browser opens, and you see the application output there.

    /help/img/idea/2016.3/HWJEE046HelloInBrowser.png

Packaging the application into an EAR: Using Java EE Application support

To package your Java EE application into an EAR, you should:

  1. Create a Java EE deployment descriptor application.xml.
  2. Configure an EAR artifact.
  3. 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:

  1. In the Project tool window, right-click your module folder and select Add Framework Support.
    /help/img/idea/2016.3/HWJEE051AddFrameworkSupport.png
  2. In the dialog that opens, select the JavaEE Application check box and click OK.
    /help/img/idea/2016.3/HWJEE052JavaEEAppSupport.png

    Note the descriptor file META-INF/application.xml created in your module folder.

    /help/img/idea/2016.3/HWJEE053AppXMLInProject.png
  3. Open the file in the editor (F4).
    /help/img/idea/2016.3/HWJEE054AppXMLInEditor.png

    At the moment, the file is almost empty.

  4. Now let's look at the artifact configurations.

    Note that a new configuration appeared, the one for an exploded EAR artifact.

    /help/img/idea/2016.3/HWJEE055Artifacts.png

    Currently only JavaEE Application facet resources (META-INF/application.xml) are included in the artifact.

    /help/img/idea/2016.3/HWJEE056ExplodedEARInitial.png
  5. 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.
    /help/img/idea/2016.3/HWJEE057ExplodedEARWithWAR.png

    (An alternative way of getting the same result would be /help/img/idea/2016.3/new.png | Artifact | JavaEEHelloWorld: war exploded.)

  6. 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.xml by this quick fix.)
  7. Create a configuration for an EAR artifact: /help/img/idea/2016.3/new.png | JavaEE Application: Archive | For 'JavaEEHelloWorld:ear exploded'.
    /help/img/idea/2016.3/HWJEE058NewEAR.png
  8. To create a manifest file, click Create Manifest and agree to the default file location (<project_folder>/META-INF/MANIFEST.MF).
    /help/img/idea/2016.3/HWJEE059EARStructure.png
  9. Click OK in the Project Structure dialog.

    See that your application.xml has changed. This is the result of applying the quick fix.

    /help/img/idea/2016.3/HWJEE060AppXMLChanged.png

    Let's now create a run configuration for building and deploying the EAR artifact.

  10. Click the run configuration selector and select Edit Configurations. Then, in the Run/Debug Configurations dialog, select /help/img/idea/2016.3/new.png | GlassFish Server | Remote.
  11. Specify a descriptive name for your run configuration, e.g. GlassFishRemoteEAR.
    /help/img/idea/2016.3/HWJEE061NewRunConfigName.png
  12. Include the EAR artifact in the deployment list: switch onto the Deployment tab and select /help/img/idea/2016.3/new.png | Artifact | JavaEEHelloWorld:ear.

    Note that the Build 'JavaEEHelloWorld:ear' artifact task is included in the Before launch task list automatically.

    /help/img/idea/2016.3/HWJEE062RunConfigDeployEAR.png
  13. 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 application.xml.
    /help/img/idea/2016.3/HWJEE063RunConfigURL.png
  14. Click OK in the Run/Debug Configurations dialog.
  15. Execute the run configuration (/help/img/idea/2016.3/run.png).

    As before, another tab opens in the Run tool window showing the run configuration output.

    /help/img/idea/2016.3/HWJEE064RunEAR.png

    Then, the application output is shown in the browser.

    /help/img/idea/2016.3/HWJEE065EARHelloInBrowser.png

    Now if you look at the Project tool window, you'll see your archive in the out/artifacts/JavaEEHelloWorld_ear folder.

    /help/img/idea/2016.3/HWJEE066EARInProject.png

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.

Tool windows. To open the tool windows, you can, for example, select View | Tool Windows | Web or View | Tool Windows | JavaEE:App.

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:

Facets. To view or edit the facet settings, open the Project Structure dialog, select Modules, and then select Web or javaEEApplication under the module node. For more info, see:

See Also

Last modified: 29 November 2016