- Java EE SDK;
- Java EE Application Server GlassFish;
- Setting up IntelliJ IDEA.
- Show how you can prepare a working environment for working with Java EE applications.
- Show how you can deploy and run Java EE applications from IntelliJ IDEA on the GlassFish server.
- Download and install Java EE 7 SDK.
- Let's raise Java EE Application Server GlassFish.
- Let's learn how to start and stop the server via the command line.
- Let's configure IntelliJ IDEA to deploy applications to this server.
- Let's deploy and run the demo application, which, together with the GlassFish application server, is included in the Java EE 7 SDK.
- GlassFish 4.0;
- IntelliJ IDEA 2019.3.1 (Ultimate);
- Java EE SDK 7.
Installing Java EE SDK
Important condition. Before installing the Java EE SDK, you must make sure that the Java SE SDK is pre-installed.-
First, we need to download the SDK. To do this, you can type in a search engine: “Java EE SDK 7”, or directly go to the sdk download page from the Oracle website.
-
From the list of SDKs provided, you need to select the one that is suitable for your platform. The article describes installing the distribution: “java_ee_sdk-7-jdk7-windows-x64-ml.exe”
-
Run the installation file. During startup, the following error may occur (if it does not occur, then you can simply continue with the installation):
This means that the installer was unable to find the path to the pre-installed Java Runtime Environment. The error can be corrected by passing the path to the JRE manually using the command line. To do this, you need to run the installation file via PowerShell and pass the path to the pre-installed JRE via the -j parameter.
For example, like this:
-
The further installation procedure is quite typical for most programs. All you need to do is click on the Next and Install buttons.
The program will install the Java EE SDK, as well as the GlassFish server, which, upon completion of the installation, will be up and ready for use. -
To make sure that the GlassFish server is working, after installation is complete, you can open the server admin panel, which will be available at: http://localhost:4848/
Screenshot below:
GlassFish starting and stopping via command line
So, the server is up and running, but the server start was initiated by the installation program. Below we will try to figure out how to start and stop the server yourself. First, let's reveal such an entity as a domain. A domain is a set of one or more GlassFish server instances managed by one administration server. When installing the GlassFish server, a default domain is created - domain1. The following components are associated with it:- Server port (default 8080);
- Administration server port (default 4848);
- Administrator username and password (default is admin and no password required by default).
asadmin start-domain domain_name
asadmin stop-domain domain_name
Let's try to stop the server (since it is already running after installation) by running PowerShell from the subdirectory C:\glassfish4\glassfish\bin : The server is stopped. Now let's run it:
We analyze the first Java EE application, dukes-age
Next we'll look at a demo Java EE application: dukes-age. This application is included in the Java EE SDK along with the first-cup application. Descriptions of these applications are presented on the official Oracle documentation website in sections 3 and 4 of the introductory course on Java EE for beginner Java EE developers . In this article, we will briefly look at the dukes-age application and examine in more detail the process of launching this application on the GlassFish server from the IntelliJ IDEA development environment. The application is located inside the glassfish server directory. If you did not change the default path for the glassfish server when installing the Java EE SDK, you can find this application in the folder: C:\glassfish4\docs\firstcup\example\dukes-age . Probably every Java developer, beginner and experienced, has at least once seen the mascot of the Java programming language - Duke: Few people know that Duke has a birthday. He was born on May 23, 1995, when the first demo version of Java technology was published. The dukes-age app, as the name suggests, provides information about Duke's age. Let's open this application in IntelliJ IDEA: File -> New -> Project From Existing Sources... Then go to the folder with the dukes-age application, located by default at C:\glassfish4\docs\firstcup\example\dukes-age , select the file pom.xml and click OK: An application with the following structure will open: Let's take a look at the class codeDukesAgeResource
:
package firstcup.dukesage.resource;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
/**
* REST Web Service
*
*/
@Path("dukesAge")
public class DukesAgeResource {
/** Creates a new instance of DukesAgeResource */
public DukesAgeResource() {
}
/**
* Retrieves representation of an instance of DukesAgeResource
* @return an instance of java.lang.String
*/
@GET
@Produces("text/plain")
public String getText() {
// Create a new Calendar for Duke's birthday
Calendar dukesBirthday = new GregorianCalendar(1995, Calendar.MAY, 23);
// Create a new Calendar for today
Calendar now = GregorianCalendar.getInstance();
// Subtract today's year from Duke's birth year, 1995
int dukesAge = now.get(Calendar.YEAR) - dukesBirthday.get(Calendar.YEAR);
dukesBirthday.add(Calendar.YEAR, dukesAge);
// If today's date is before May 23, subtract a year from Duke's age
if (now.before(dukesBirthday)) {
dukesAge--;
}
// Return a String representation of Duke's age
return "" + dukesAge;
}
}
This is a JAX-RS RESTful web service with one method - getText()
. If you examine the code of this method, it becomes clear that the method returns the number of years that have passed since Duke's birth. That is, the method returns Duke's age. A class is a resource in a REST context.
- The annotation above the class
@Path("dukesAge")
defines the URI path to which calls will be processed by this class. - The annotation above the method
@GET
tells us that this method processes GET requests. - The annotation
@Produces("text/plain")
specifies the media type of the response to the request. It is not difficult to guess that the methodgetText()
will generate responses with media type: text/plain.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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/web-app_3_0.xsd">
<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
In this file, inside the tag servlet-mapping
, a tag url-pattern
with the value is defined /webapi/*
. Here a certain root point is defined, calls to which will be processed further by the service. If combined with the annotation @Path("dukesAge")
, it turns out that to get information about Duke’s age, you need to contact the address /webapi/dukesAge.
Setting up IntelliJ IDEA to run a Java EE application on a GlassFish server
Now that we have a very general idea of the dukes-age application (read more about it on the Oracle site ), let's configure IntelliJ IDEA so that we can run this application from our IDE. Setting up IntelliJ IDEA- To do this, go to the menu Run -> Edit Configurations.
- The Run/Debug Configurations panel will open:
- We need to add a new configuration. To do this, click on the plus sign, or use the Alt+Insert hotkey. In the list that opens, select GlassFish Server -> Local:
- In the window that opens, let's start setting up the configuration:
- The next step is to indicate the artifacts that will need to be deployed to the server. This can be done through the Deployment tab by clicking on the plus -> Artifact...
- Select dukes-age:war and click OK:
- Next, save our configuration by clicking on Apply and then OK.
Launching the application
Now let's try to run the application.-
To do this, you can use the hotkey Alt+Shift+F10, or through the menu Run -> Run...
And select the dukes-age configuration we created:
-
After this we see the startup logs in the Services panel:
-
Then, if everything is done according to the instructions, the browser will open and it will become clear that nothing is working:
-
To do this, let's go back to IntelliJ IDEA, then to the menu: Run -> Edit Configurations...
-
Let's select the previously saved configuration and update the URL in accordance with the screenshot below:
-
Let's save the configuration and restart our application using the Ctrl+Shift+F10 hotkey, or using a special button on the Services panel:
-
After the restart, if everything was done correctly, we will see Duke's age:
Conclusion
In this article, we looked at how you can configure your local environment to work with Java EE applications using the example of the GlassFish 4.0 server, the IntelliJ IDEA 2019.3.1 (Ultimate) development environment, as well as Java EE SDK version 7. We looked at how you can:- download and install Java EE SDK, as well as the GlassFish server;
- stop and start the GlassFish server via PowerShell;
- configure IntelliJ IDEA so that you can deploy Java EE applications from the IDE to the GlassFish server and launch them immediately.
Bonus task
- Repeat all the described steps yourself.
- Take an introductory course on Java EE for beginner Java EE developers from Oracle .
- Item with an asterisk. Try to deploy the second first-cup demo application yourself. This application is more interactive and also interacts with the dukes-age application. To complete this step, you will need to independently understand the following questions:
- How to enable the database inside the glassFish server;
- How to integrate two applications with each other (inside first-cup indicate the path to the dukes-age endpoint, which we discussed in this article);
- How to create a new configuration in Intellij IDEA for first-cup deployment (similar to what was shown in this article);
- Possibly many other questions)
GO TO FULL VERSION