From IntelliJ-Wiki

This tutorial is intended to help you get started with developing applications for Java Platform, Enterprise Edition (Java EE) in IntelliJ IDEA.

Though the application that we develop here - as its name suggests - is a very basic one, the tasks discussed in this tutorial are typical of developing Java EE applications of any complexity.

Before you start

To be able to perform the tasks discussed in this tutorial, make sure that the following software is installed on your computer:

IntelliJ IDEA Ultimate Edition, version 12.

Java Development Kit (JDK), version 1.6 (also referred to as 6) or later. You can download the necessary version of Oracle JDK from the Java SE Downloads page.

JBoss Application Server (JBoss AS), version 7.1 or later, or JBoss Enterprise Application Platform (JBoss EAP), version 6. You can download the necessary version of JBoss from the JBoss Downloads page. (Instead, you can use any other Java EE 6-compatible application server.)

A web browser

Preparing the IDE

Preparing IntelliJ IDEA for Java EE application development includes:

1. Making sure that the necessary plugins are enabled.

2. Defining the JDK that you are going to use in IntelliJ IDEA.

3. Defining a Java EE-compatible application server that you will use to deploy and run your applications (e.g. JBoss) in the IDE.

Making sure that the necessary plugins are enabled

Plugins are extensions to IntelliJ IDEA core functionality. To be able to perform the tasks discussed in this tutorial, the following plugins have to be enabled:

Java EE Integration. This plugin provides support for Java EE application development.

JBoss Integration. This plugin lets the IDE interact with JBoss. (If you want to use a different Java EE application server, the corresponding server-specific plugin should be enabled.)

To make sure that these plugins are enabled:

1. Open the Settings dialog (e.g. by pressing CTRL+ALT+S).

2. In the left-hand pane of the dialog, under IDE Settings, select Plugins (1 on the following picture).

3. On the Plugins page that opens in the right-hand part of the dialog, type java ee in the search box (2). As a result, only the Java EE Integration plugin is shown in the list.

Make sure that the check box next to Java EE Integration is selected (3).

4. Find the JBoss Integration plugin and make sure that this plugin is also enabled (1, 2).

5. Click OK.

6. If suggested, restart IntelliJ IDEA.

Defining a JDK in IntelliJ IDEA

If the JDK that you are going to use is already defined in IntelliJ IDEA, skip this section and proceed to defining JBoss in the IDE. Otherwise:

1. Open the Project Structure dialog (e.g. by pressing CTRL+ALT+SHIFT+S).

2. In the leftmost pane, under Platform Settings, select SDKs (1).

3. Above the pane to the right, click (2) and select JDK (3).

4. In the Select Home Directory for JDK dialog that opens, select the folder in which you have the necessary JDK version installed (1) and click OK (2).

5. Click OK in the Project Structure dialog.

Defining JBoss in IntelliJ IDEA

To define JBoss in IntelliJ IDEA, use the following procedure. In a similar way, you can define any other application server.

1. Open the Settings dialog (CTRL+ALT+S).

2. In the left-hand part of the dialog, under IDE Settings, select Application Servers (1).

3. In the right-hand part of the dialog, under Application Servers, click (2) and select JBoss Server (3).

4. In the JBoss Server dialog, click to the right of the JBoss Home field.

5. In the JBoss Home Directory dialog, specify the directory where JBoss is installed (1), and click OK (2).

6. Click OK in the JBoss Server dialog.

IntelliJ IDEA registers the .jar files necessary for working with JBoss. You can see their list under Libraries. (An application server library that contains the corresponding files is created.)

7. Click OK in the Settings dialog.

The IDE is ready. Let's now discuss the application that we are going to develop.

The Hello World Java EE application

The business logic of the application will be implemented by an enterprise bean HelloWorldBean. The only function of the bean will be to generate the text Hello, World!

To access the bean, the application will provide a web interface. This interface will be implemented by two JSP pages and a servlet.

The first of the pages (index.jsp) will include a form with the Say Hello button. Clicking this button will activate the servlet HelloWorldServlet. The servlet will call the appropriate bean method, store the result (the text Hello, World!) as a request attribute and pass the control of the application to the second of the JSP pages (hello.jsp). The page will retrieve the value of the appropriate request attribute and display it.

The following sections show the contents of all the application source files as well as corresponding application deployment descriptors.

HelloWorldBean.java

package mybeans; import javax.ejb.Stateless; @Stateless(name = "HelloWorldEJB" ) public class HelloWorldBean { public HelloWorldBean() { } public String sayHello() { return "Hello, World!" ; } }

index.jsp

<%@ page contentType =" text/html;charset=UTF-8 " language =" java " %> < html > < head > < title ></ title > </ head > < body > < form method= "get" action= "helloworld" > < input type= "submit" value= "Say Hello" /> </ form > </ body > </ html >

HelloWorldServlet.java

package myservlets; import mybeans.HelloWorldBean; import javax.ejb.EJB; import java.io.IOException; @javax.servlet.annotation.WebServlet(name = "HelloWorldServlet" , urlPatterns = "/helloworld" ) public class HelloWorldServlet extends javax.servlet.http.HttpServlet { @EJB private HelloWorldBean helloWorldBean ; protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException { } @Override protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException { String hello= helloWorldBean .sayHello(); request.setAttribute( "hello" ,hello); request.getRequestDispatcher( "hello.jsp" ).forward(request,response); } }

hello.jsp

<%@ page contentType =" text/html;charset=UTF-8 " language =" java " %> < html > < head > < title ></ title > </ head > < body > < h1 > ${ requestScope.hello } </ h1 > </ body > </ html >

application.xml

<? xml version= "1.0" encoding= "UTF-8" ?> < application xmlns= "http://java.sun.com/xml/ns/javaee" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" version= "6" > < module id= "ejb-EJB" > < ejb >ejb.jar</ ejb > </ module > < module id= "web-Web" > < web > < web-uri >web.war</ web-uri > < context-root >webWeb</ context-root > </ web > </ module > </ application >

ejb-jar.xml

<? xml version= "1.0" encoding= "UTF-8" ?> < ejb-jar xmlns= "http://java.sun.com/xml/ns/javaee" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd" version= "3.1" > < enterprise-beans > < session > < ejb-name >HelloWorldEJB</ ejb-name > < ejb-class >mybeans.HelloWorldBean</ ejb-class > < session-type >Stateless</ session-type > < transaction-type >Container</ transaction-type > </ session > </ enterprise-beans > </ ejb-jar >

Preparing a project

Now that we know what application we are going to develop, let us create a project suitable for the purpose.

Intended project structure

The project that we will create will include three Java modules (in IntelliJ IDEA terminology). In each of the modules, different additional features will be enabled - depending on the purpose of a particular module. (Those additional features in IntelliJ IDEA are often referred to as facets.)

ejb. This module will be used to develop the bean. So, when creating this module, we will enable the EJB 3.1 development support. As a result, IntelliJ IDEA will download the necessary implementation JAR files and organize those files in a library. In addition to that, the IDE will create the deployment descriptor ejb-jar.xml and a specification of an EJB JAR artifact.

web. This module will be used to develop the web interface (the JSPs and the servlet). So, when creating this module, we will enable web application development support. As a result, IntelliJ IDEA will create the starting page of the application and a WAR artifact specification.

ear. This module will be used for generating the overall application EAR artifact. When creating this module we will enable the Java EE 6 application development support and also specify that JBoss is going to be used. As a result, IntelliJ IDEA will create the Java EE application deployment descriptor application.xml , an EAR artifact specification, and also a run configuration for running the artifact in the context of JBoss.

Note that this project structure seems optimal not only for developing the Hello World application. This or similar structure may also be used to develop more complex Java EE applications when EJB and web application modules (in Java EE terminology) should be combined in an enterprise archive.

Project creation scenario

When preparing a project for our application development we will:

1. Create an empty project.

2. Define the project SDK (JDK).

3. Add the necessary modules to the project (ejb, web and ear).

Creating an empty project

To create a project that doesn't contain any modules:

1. If no project is currently open in IntelliJ IDEA, click Create New Project on the Welcome screen. Otherwise, select New Project from the File menu.

As a result, the New Project wizard opens.

2. In the left-hand pane, under Other, select Empty Module (1).

3. In the right-hand part of the page, in the Project name field, type the name of your new project (e.g. JavaEEHelloWorld ) (2).

4. Click Finish (3).

When the project is created, the Project Structure dialog opens. Let's start our work with the project by defining the project JDK.

Defining the project JDK

To define the project JDK:

1. In the Project Structure dialog, under Project Settings, select Project (1).

2. Select the necessary JDK from the list under Project SDK (2) and click Apply (3).

Adding the module ejb

1. In the Project Structure dialog, under Project Settings, select Modules (1).

2. Click (2) and select New Module (3).

3. In the New Module wizard that opens, select Java Module (1).

4. In the Module name field, type the name of the module ( ejb ) (2).

5. Click Next.

6. On the next page of the wizard, select the Enterprise JavaBeans check box. Make sure that the associated EJB settings are as shown on the following picture.

7. Click Finish.

8. In the Project Structure dialog, click Apply.

Adding the module web

1. In the Project Structure dialog, click and select New Module.

2. On the first page of the New Module wizard that opens, make sure that Java Module is selected, specify the module name ( web ), and click Next.

3. On the next page of the wizard, select the Web Application check box (1) and then select 3.0 from the Version list (2). (This is the version of the servlet specification to be supported.)

4. Click Finish.

5. In the Project Structure dialog, click Apply.

Adding the module ear

1. In the Project Structure dialog, click and select New Module.

2. On the first page of the New Module wizard, specify the module name ( ear ) (1).

3. Under More Settings, clear the Create source root check box (2). (We are not going to use this module for developing source code.)

4. Click Next (3).

5. On the next page of the wizard, select the JavaEE Application check box. Make sure that 6 is selected in the Version list.

6. Select the Application Server check box. Make sure that the version of JBoss you are going to use in your project is selected in the Application Server field.

7. Clear the Web Application check box. (This check box was selected automatically when you selected Application Server. Because we already have a separate module for developing the web interface, we don't need web application development support in the module that we are creating.)

8. Click Finish to complete the wizard.

9. In the Project Structure dialog, click OK.

Exploring the project structure

In the Project tool window, the structure of your project looks similar to this:

The first three top-level nodes (ear, ejb and web) are the module folders; the .iml files are used to store configuration data for the modules. External Libraries represent the "external resources" necessary for your development work. Currently in this category are your JDK and the JBoss application server library.

The module folders currently contain:

ear contains a directory META-INF with the Java EE application deployment descriptor application.xml inside.

ejb contains the following folders and files: lib is a folder with .jar library files for EJB and servlet development. (These were downloaded by IntelliJ IDEA when creating the module.) META-INF contains the EJB deployment descriptor ejb-jar.xml . src is a folder for your EJB source files.

contains the following folders and files:

web contains the following folders and files: scr is intended for your web interface's Java classes (e.g. servlet classes). web is for web resources (web pages, images, stylesheets, etc.). Currently in this folder is the file index.jsp intended as a starting page of your application.

contains the following folders and files:

Developing the application source code

Now that the project is ready, let's start to develop the application source code.

Creating the bean

To create the bean source file and a package for it:

1. In the Project tool window, within the module ejb , select the folder src (1), press ALT+INSERT and select Stateless Session Bean (2).

2. In the New Stateless Session Bean dialog that opens, in the <ejb-name> field, replace Session with HelloWorld (3).

3. In the Package field, specify the name of the package to be created (e.g. mybeans ) (2).

4. Click OK (3).

5. Open the bean source file in the editor. (The quickest way to do that is by using the EJB tool window which is currently active: 1) Press ENTER to expand the current node HelloWorldEJB (mybeans). 2) Press the DOWN ARROW to select HelloWorldBean. 3) Press F4 to open the file.)

Adding code to HelloWorldBean.java

The code in its final state should look similar to this:

package mybeans; import javax.ejb.Stateless; @Stateless(name = "HelloWorldEJB" ) public class HelloWorldBean { public HelloWorldBean() { } public String sayHello() { return "Hello, World!" ; } }

IntelliJ IDEA has already provided some code for the bean. Add the fragment shown above on yellow background.

Creating the servlet

1. In the Project tool window, within the module web , select the folder src (1), press ALT+INSERT and select Servlet (2).

2. In the New Servlet dialog that opens, in the Name field, specify the servlet name (e.g. HelloWorldServlet ) (1).

3. In the Package field, specify the name of the package to be created (e.g. myservlets ) (2).

4. Click OK (3).

Adding code to HelloWorldServlet.java

The servlet code in its final state should be as follows. The fragments to be added are on yellow background. Note that the necessary import statements may be added by IntelliJ IDEA automatically if you use the corresponding quick fixes as suggested later in this section.

package myservlets; import mybeans.HelloWorldBean; import javax.ejb.EJB; import java.io.IOException; @javax.servlet.annotation.WebServlet(name = "HelloWorldServlet" , urlPatterns = "/helloworld" ) public class HelloWorldServlet extends javax.servlet.http.HttpServlet { @EJB private HelloWorldBean helloWorldBean ; protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException { } @Override protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException { String hello= helloWorldBean .sayHello(); request.setAttribute( "hello" ,hello); request.getRequestDispatcher( "hello.jsp" ).forward(request,response); } }

Initially, certain code fragments in the editor are shown red. The problem is removed by adding the library Java EE 6-3.1 (corresponding to the folder lib in the module ejb) to dependencies of the module web. This may be done by using the quick fix provided by IntelliJ IDEA:

1. Place the cursor within WebServlet and press ALT+ENTER to see the list of suggested quick fixes (1).

2. Select Add library 'Java EE 6-3.1' to classpath from the list (2).

3. After adding

@EJB private HelloWorldBean helloWorldBean ;

use the IntelliJ IDEA quick fix for HelloWorldBean as shown on the following picture (1, 2).

Adding code to index.jsp

The content of index.jsp in its final state should be similar to this:

<%@ page contentType =" text/html;charset=UTF-8 " language =" java " %> < html > < head > < title ></ title > </ head > < body > < form method= "get" action= "helloworld" > < input type= "submit" value= "Say Hello" /> </ form > </ body > </ html >

1. You don't need to create the file index.jsp because IntelliJ IDEA has already done that for you. So open this file in the editor. (For example, select the file in the Project tool window and press F4.)

2. Add the missing elements (shown on yellow background above).

Creating hello.jsp

1. In the module web , select the folder web (1) and press ALT+INSERT.

2. Select JavaEE Web Page (2).

3. In the Create JavaEE Web Page dialog that opens, specify the page name ( hello ) (1) and click OK (2).

Adding code to hello.jsp

The content of the page in its final state should be as follows.

<%@ page contentType =" text/html;charset=UTF-8 " language =" java " %> < html > < head > < title ></ title > </ head > < body > < h1 > ${ requestScope.hello } </ h1 > </ body > </ html >

All you have to do, obviously, is to add <h1>${requestScope.hello}</h1> within the <body> element.

Configuring the application artifact and fixing application.xml

Now we are going to configure the intended project output or, in IntelliJ IDEA terms, the application artifact. At the same time, we will use IntelliJ IDEA quick fixes to add the necessary definitions to the deployment descriptor application.xml .

1. To start with, open application.xml in the editor to see that it's nearly empty.

2. Open the Project Structure dialog (e.g. CTRL+ALT+SHIFT+S) and select Artifacts.

Note that there are already three artifact specifications in the project. These were created by IntelliJ IDEA when creating the corresponding modules. (The part preceding a colon in the artifact names correspond to the names of the modules).

Also note the pink error bar in the lower part of the dialog. IntelliJ IDEA informs us of a problem with the web artifact. What the IDE is telling us is really true, however, we are not going to build and use this artifact separately. So the error may well be ignored.

3. Click Ignore error .

Now we are going to add missing parts to the ear artifact. (Select this artifact if it's not yet selected.)

4. Select the Show content of elements check box to see a more detailed picture of the artifact.

Now you can see that the artifact configuration includes only the folder META-INF with the deployment descriptor application.xml .

To complete the ear artifact we will simply add the other two artifacts. (The way we organized the modules in our project, among other things, made the structures of the auto-generated artifact specifications very logical.)

5. Under Available Elements, expand the Artifacts node (1), right-click the ejb artifact (2) and select Put into /ejb.jar (3).

Note how the structure of the ear artifact changed. Not bad, isn't it?

Also note the warning 'EJB' facet isn't registered in 'application.xml'. This means that the EJB module (in Java EE terms) is included in the artifact but it's definition is missing from the application descriptor.

6. Click Fix.

7. Add the web artifact to the ear artifact (1, 2).

8. Now to fix application.xml , click Fix. (When clicking Fix, you shouldn't be suggested any fix options, and the warning should disappear at once.)

9. If the warning disappears, click OK in the Project Structure dialog and check your application.xml.

Otherwise, if when clicking Fix the following set of options is suggested

don't select any of those option. Try using the error bar instead.

10. Click 2 errors found to expand the error bar.

11. Within the bar where the problem with application.xml is reported, click the light bulb (1) and then click Fix (2).

12. If the warning doesn't disappear, click OK in the Project Structure dialog anyway and check your application.xml .

The content of application.xml should be as follows. (If it isn't, make the necessary edits.)

<? xml version= "1.0" encoding= "UTF-8" ?> < application xmlns= "http://java.sun.com/xml/ns/javaee" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" version= "6" > < module id= "ejb-EJB" > < ejb >ejb.jar</ ejb > </ module > < module id= "web-Web" > < web > < web-uri >web.war</ web-uri > < context-root >webWeb</ context-root > </ web > </ module > </ application >

Building and running the application

Now it looks like it's time to build and run the application. As we are about to see, we don't need to build the application separately. IntelliJ IDEA will do that for us automatically as part of executing the corresponding run configuration.

Checking run configuration settings

Let's take a quick look at the run configuration that IntelliJ IDEA created when creating the module ear.

1. Click the run configuration selector on the toolbar (1) and select Edit Configurations (2).

The Run/Debug Configurations dialog opens and the settings for the only existing run configuration are displayed. Note:

The application is going to be run using JBoss (the Application server field).

A web browser will be started and the page with the URL http://localhost:8080/webWeb will be displayed (the Start browser option and the Startup page field. If you look at application.xml you'll see that the context root of the application is webWeb . Consequently, the default page ( index.jsp ) associated with this context root will be shown.)

As part of executing this run configuration, IntelliJ IDEA will compile the project and build the ear artifact (the Before launch options in the lower part of the dialog.)

2. Click the Deployment tab.

As you can see, IntelliJ IDEA will deploy the ear artifact each time it starts (or restarts) the server.

So everything looks fine.

3. Click OK in the Run/Debug Configurations dialog.

Running the application

1. To run the application (or, to be more precise, to execute the corresponding run configuration), click Run to the right of the run configuration selector on the toolbar.

IntelliJ IDEA compiles the application classes, builds the artifact, starts JBoss and deploys the artifact onto the server. You can monitor the process in the Run tool window which opens in the lower part of IntelliJ IDEA workspace.

Then, your default web browser starts, and the starting page of your application is displayed.

2. Click the Say Hello button on this page.

As a result, the Hello World page is shown.

So the application is working fine. At this step, our exercises are completed, and we hope that now you have an impression of how to develop Java EE applications in IntelliJ IDEA.