Using Integrated Development Environment (IDE) for Jakarta /Java EE

Eclipse, IntelliJ, NetBeans, ...

Eclipse JEE (for Jakarta EE)

Install

  1. Goto "download eclipse packages" @ https://www.eclipse.org/downloads/packages/. Choose "Eclipse IDE for Enterprise Java and Web Developers " to download eclipse-jee-202x-xx-R-win32-x86_64.zip (520MB).
  2. Unzip.
  3. You can launch Eclipse by running eclipse.exe.

Configure JDK

Assume that you have installed the latest JDK (otherwise, read "JDK HowTo").

  1. Launch Eclipse.
  2. Set Default JRE: Select "Window" menu ⇒ Preferences ⇒ Java ⇒ Installed JRE ⇒ set default to latest JDK.
  3. Set Compiler level: Select "Window" menu ⇒ Preferences ⇒ Java ⇒ Compiler ⇒ Compiler compliance level ⇒ choose your JDK level.

Connecting to MySQL Database Server

Reference: "Data Tools Platform User Documentation" @ Eclipse Welcome page.

Assume that you have installed "MySQL Community Server" and "MySQL Connector/J Driver". Otherwise, read "How to install and get started with MySQL".

To use Eclipse for MySQL development:

  1. Start the MySQL server. Start a client to check the database-name, which is needed to create a database connection URL in the form of jdbc:mysql://localhost:3306/database-name.
  2. Switch to "Database Development" Perspective: Select "Window" menu ⇒ Open Perspective ⇒ Other ⇒ Database Development.
  3. Create a Database Connection: Right-click on "Database Connection" ⇒ New ⇒ Next.
    Take note that ONE database connection connects to ONE particular database in the MySQL database server.
    1. In "Connection Profile", choose "MySQL" ⇒ Next.
    2. In "Drivers", click the "+" icon for "New Driver Definition".
    3. In "Specify a Driver Template and Definition Name":
      1. Select "Name/Type" tab ⇒ Choose one of the database driver as our template (e.g. MySQL 5.1) for further customization ⇒ Set your "Driver name", e.g., "mysql-connector-java-8.3.0".
      2. Switch to "JAR List" tab ⇒ Clear All ⇒ Click "Add JAR/Zip" and select the driver JAR file, e.g., mysql-connector-java-8.3.0.jar.
      3. Switch to "Properties" tab ⇒ Check Connection URL, Database name, Username, Password.
      4. Start the MySQL server.
      5. Click "Test Connection" (MySQL server has been started).
    4. In "Datasource Explorer", you can "connect" and "disconnect" the connection.
  4. To view and edit table visually, expand database "Schemas" to look for the table. Right-right on the table ⇒ Data ⇒ Edit. You can modify the cells and "save" the changes.
  5. To create a new SQL script, choose File ⇒ New ⇒ SQL File ⇒ You may use an existing project or create a new project (General|Project or Web|Dynamic Web Project) ⇒ Enter filename, and set the connection profile name ⇒ Finish. Enter a SQL statement (e.g., SELECT * FROM tablename) ⇒ Right-click on the text ⇒ "Execute Current Text" or "Execute All".
  6. To use an existing SQL file, drop the file into a project and open the SQL file. In Connection profile, set the type and connection name. Right-click on a statement ⇒ "Execute All".

Configure Tomcat Web Server

Assume that you have installed Apache Tomcat Server (version 10 or above) (otherwise, read "Tomcat HowTo").

  1. Launch Eclipse.
  2. Configure Tomcat Server:
    1. Select "File" menu ⇒ New ⇒ Other... ⇒ Server ⇒ Server ⇒ Apache ⇒ Tomcat v10.x server ⇒ Next.
    2. In "Tomcat installed directory": Browse to select Tomcat's installed directory (TOMCAT_HOME) ⇒ Finish.
  3. Start/Restart/Stop Tomcat Server: Select bottom "Server" tab ⇒ Push Start/Stop server buttons.

First Servlet

  1. Create New Webapp Project: Launch Eclipse ⇒ File ⇒ New ⇒ Dynamic Web Project. In "Project name", enter "FirstServletProject".
  2. Create New Package: Expand "Java Resources" node ⇒ Right-click on "src/main/java" ⇒ New ⇒ Package ⇒ Enter "com.nowhere".
  3. Write the First Servlet: Right-click on "com.nowhere" ⇒ New ⇒ Class (or "Servlet", if exists) ⇒ Enter "HelloServlet". Enter the followings:
    package com.nowhere;
    
    import java.io.*;
    import jakarta.servlet.*;             // Tomcat 10
    import jakarta.servlet.http.*;        // Tomcat 10
    import jakarta.servlet.annotation.*;  // Tomcat 10
    //import javax.servlet.*;             // Tomcat 9
    //import javax.servlet.http.*;        // Tomcat 9
    //import javax.servlet.annotation.*;  // Tomcat 9
     
    @WebServlet("/sayhello")   // Configure the request URL for this servlet (Tomcat 7/Servlet 3.0 upwards)
    public class HelloServlet extends HttpServlet {
    
       // The doGet() runs once per HTTP GET request to this HTTP servlet.
       @Override
       public void doGet(HttpServletRequest request, HttpServletResponse response)
             throws IOException, ServletException {
     
          // Set the response's MIME type of the response message
          response.setContentType("text/html");
          // Allocate an output writer to write the response message into the network socket
          PrintWriter out = response.getWriter();  // throw IOException
          
          // Write the response message, in an HTML page
          // Using triple-quoted multi-line string (Text Block) (JDK 15)
          // The beginning triple-quote must be in its own line (i.e., ends with a newline)
          out.println("""
                <!DOCTYPE html>
                <html>
                <head><title>Hello, World</title></head>
                <body>
                  <h1>Hello, world!</h1>
                  <p>Request URI: %s</p>
                  <p>Protocol: %s</p>
                  <p>PathInfo: %s</p>
                  <p>Remote Address: %s</p>
                  <p>A Random Number: <strong>%f</strong></p>
                </body>
                </html>
                """.formatted(request.getRequestURI(), request.getProtocol(),
                              request.getPathInfo(), request.getRemoteAddr(),
                              Math.random()));   // Multi-line text block (JDK 15) 
          out.close();  // Always close the output writer
          
          // For testing and debugging - Print a message to Tomcat's console
          System.out.println("hello world, to Tomcat!");   // Check Tomcat's console for this message
       }
    }
    How It Works:
    • [TODO]
  4. Setup Servlet API (External JAR): Servlet packages (jakarta.servlet, jakarta.servlet.http) are not part of JDK, but belongs to Jakarta EE (formerly Java EE). Tomcat has a copy of Servlet API under its "lib" called "servlet-api.jar".
    Right-click on project-name ⇒ Properties ⇒ Java Build Path ⇒ Libraries ⇒ Classpath ⇒ Add External JARs ⇒ Browse to select tomcat/lib’s servlet-api.jar.
  5. Deploy the Servlet (in Tomcat Server): Right-click on project-name ⇒ Run As ⇒ Run on Server (assume that you have installed and configured Tomcat server).
  6. Invoke the Servlet: From a web browser, enter URL "http://localhost:8080/FirstServletProject/sayhello, where 8080 is the server's TCP port number, FirstServletProject is the webapp-name (same as project-name) and sayhello the the URL of the HelloServlet (configured via @WebServlet annotation).
  7. Check out the Directories and Resources:
    • Under src\main\webapp, there is a WEB-INF with a deployment descriptor web.xml.
    • Under src\main\webapp, there is also a META-INF with with MANIFEST.MF.
    • Under "Referenced Libraries", there is a "servlet-api.jar" (configured via External JARs). Expand it, you can find jakarta.servlet and jakarta.servlet.http packages, and HttpServlet class.

First JSP

  1. Create a new Webapp Project: Select "File" menu ⇒ New ⇒ Dynamic Web Project (under "Web" category) ⇒ In "Project Name", enter "FirstJspProject" ⇒ Finish.
  2. Create a new JSP File: Right-click on "webapp" ⇒ New ⇒ JSP File ⇒ In "File Name", enter "hello.jsp" ⇒ "Finish".
  3. Enter the following HTML/JSP codes:
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Hello JSP</title>
    </head>
    <body>
    <h1>Hello World, from JSP</h1>
    <p>Method: <%= request.getMethod() %></p>
    <p>Request URI: <%= request.getRequestURI() %></p>
    <p>Protocol: <%= request.getProtocol() %></p>
    <p>PathInfo: <%= request.getPathInfo() %></p>
    <p>Remote Address: <%= request.getRemoteAddr() %></p>
    <% double num = Math.random();
       if (num > 0.75) { %>
         <h2>You'll have a lucky day!</h2><p>(<%= num %>)</p>
    <% } else { %>
         <h2>Well, life goes on ... </h2><p>(<%= num %>)</p>
    <% } %>
    <h3><a href="<%= request.getRequestURI() %>">Try Again</a></h3>
    </body>
    </html>
  4. If error on jakarta.servlet appears, follow the previous "Write the First Servlet" section to add servlet-api.jar as external JAR.
  5. To execute the JSP, right-click on "hello.jsp" ⇒ Run As ⇒ Run on Server (assume that you have installed and configured Tomcat server).

Exporting a Webapp as a WAR (Webapp Archive) file

  1. Right-click on the project-name to be exported ⇒ Export ⇒ War File ⇒ In "Destination", specify the destination directory and filename (the filename shall be the webapp name) ⇒ Finish.
  2. To deploy the war file in Tomcat, simply drop the war file into Tomcat's "webapps" folder. The war file will be automatically extracted and deployed. The web application name is the war-filename.
  3. You could use WinZip (or WinRAR) to view the content of the war file, as war-file is in ZIP format.
Deploying a webapp outside the Tomcat's webapps directory

To deploy a webapp (called hello) outside the <TOMCAT_HOME>\webapps directory, create a hello.xml file as follows and place it under the <TOMCAT_HOME>\conf\Catalina\localhost:

<Context displayName="hello"
     docBase="C:\path\to\webapp"
     path="/hello"
     reloadable="true" />

First JSF

[TODO]

Debugging Webapps

You can debug a webapp just like standalone application. For example, you can set breakpoints, single-step through the programs, etc.

IntelliJ IDEA Webapps (for Jakarta EE)

Install

For developing webapps, you probably need to download IntelliJ Ultimate, which is not free (but 30-day trial). You may be able to get it free with a .edu account (Check out "Education" ⇒ Free License ⇒ For Students and Teachers.

Goto IntelliJ mother site, download and install IntelliJ Ultimate.

First Maven Webapp to Configure Tomcat Server

We shall now write our first webapp (with Maven) and configure it to run under Tomcat Server.

  1. Assume that you have install the Tomcat 10+ server. Otherwise, see "Tomcat HowTo".
  2. Launch IntelliJ ⇒ New Project ⇒ Choose Generators: Maven Archetype ⇒ In "Name", enter "FirstServletProject" ⇒ In "Location" select your location ⇒ In "Archetype", select "webapp" (i.e., org.apache.maven.archetypes:maven-archetype-webapp) ⇒ Create.
  3. Inspect the project created: Expand "src" ⇒ main ⇒ webapp and check out:
    • index.jsp: which is the home page of your webapp.
    • Directory WEB-INF: which is a hidden directory keeping servlets and deployment descriptor web.xml.
  4. Configure Tomcat Server:
    1. Click top menu item "Current File" ⇒ Edit configuration ⇒ "+" (Add new configuration) ⇒ Tomcat Server ⇒ Local.
    2. In "Server" pane: Configure… ⇒ In "Tomcat Home", browse to select your tomcat installed directory.
    3. In "Deployment" pane: "+" (add) ⇒ artifact… ⇒ choose either the uncompressed “FirstServletProject:war exploded”, or the compressed “FirstServletProject:war” ⇒ OK
  5. Click "Run Tomcat 10.x.y" ⇒ A web browser is automatically launched with the URL http://localhost:9999/FirstServletProject_war_exploded/. This directory request shows index.jsp (as a welcome page configured under Tomcat\conf\web.xml).

First Servlet

Let's continue to write our first servlet:

  1. Continue from the previous maven project "FirstServletProject".
  2. Expand "src" ⇒ Right-click "main" ⇒ new ⇒ Directory ⇒ Enter "java" (to be used as the Java source root).
  3. Right-click "java" ⇒ New ⇒ Package ⇒ Enter "com.nowhere".
  4. Right-click "com.nowhere" ⇒ New ⇒ Java Class ⇒ Enter "HelloServlet" ⇒ Write the "HelloServlet.java" (as in the "Eclipse JEE" section).
  5. Open "pom.xml":
    1. Include jakarta.servlet <dependency> under <dependencies>. This dependency is used to include the Jakarta Servlet 6.0 packages (jakarta.servlet and jakarta.servlet.http) used in the servlet programs (which is not part of JDK or Java SE, but belongs to Java EE or now Jakarta EE). The scope of "provided" is to indicate that there is no need to include this jar file in the output (because tomcat server has a copy). This is used in compilation only.
    2. Also include the properties to set the JDK compiler and target levels.
    The entire working "pom.xml" is produced as follows:
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.nowhere</groupId>
      <artifactId>FirstServletProject</artifactId>
      <packaging>war</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>FirstServletProject Maven Webapp</name>
      <url>http://maven.apache.org</url>
    
      <dependencies>
        <dependency>
          <groupId>jakarta.servlet</groupId>
          <artifactId>jakarta.servlet-api</artifactId>
          <version>6.0.0</version>
          <scope>provided</scope>
        </dependency>
      </dependencies>
    
      <build>
        <finalName>FirstServletProject</finalName>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <source>21</source>
              <target>21</target>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>
    Expand the "External Libraries", you can find "Maven:jakarta.servlet":jakarta.servlet-api:6.0.0" included. You can find the Jakarta Servlet packages under it.
  6. Now, you can test the servlet by push the "Run Tomcat" button, and issue URL http://localhost:9999/FirstServletProject_war_exploded/sayhello. The HelloServlet is mapped to URL "/sayhello" using @WebServlet annotation.
Empty Project with Many Maven Modules

You can also create one project with many Maven modules, as follows:

  1. New Project ⇒ Empty Project ⇒ In "Name", Enter "JakartaServletProjects".
  2. Right-click on project-name ⇒ New ⇒ Module ⇒ In "Generators", choose "Maven Archetype" ⇒ In "Name", enter "helloapp" ⇒ In "Archetype", select "org.apache.maven.archetypes:maven-archetype-webapp".
  3. Expand "helloapp" ⇒ src ⇒ Right-click "main" ⇒ New ⇒ Directory ⇒ Enter "java" (as Java source root).
  4. Right-click "java" ⇒ New ⇒ Package ⇒ Enter "com.nowhere". Keep all the servlets here.
  5. You can create more Maven modules, similarly.

[TODO] Why?

NetBeans (for Jakarta EE)

Connecting to MySQL Database Server

NetBeans provides direct support to MySQL server. You can use NetBeans as a GUI client to access a MySQL server, as well as an administrative tool (e.g., start and stop the server).

Assume that you have installed "MySQL Community Database Server" and "MySQL Connector/J Driver" (otherwise, read MySQL HowTo).

Configuring NetBeans to Support MySQL

Select "Window" menu ⇒ Services. The "Services" tab shall appear on the left sidebar.

  1. Right-click on the "Databases" node ⇒ "Register MySQL Server". (If you have already registered a MySQL server, you can right-click on Server node "MySQL Server at hostname:port" ⇒ Properties, to modify its properties.)
  2. Switch to "Basic Properties" tab: Enter the hostname, port number, username and password.
  3. Switch to "Admin Properties" tab:
    1. Leave the "Path/URL to admin tool" empty.
    2. In "Path to start command", browse to select "<MYSQL_HOME>\bin\mysqld.exe"; in the "Arguments", enter "--console"
    3. In "Path to stop command", browse to select "<MYSQL_HOME>\bin\mysqladmin.exe", in the "Arguments", enter "-u username -ppassword shutdown".
  4. Open "Drivers" node ⇒ Right-click "MySQL (Connector/J driver) ⇒ Connect using... ⇒ Add ⇒ Browse to select your MySQL Connector/J Driver JAR file ⇒ Next. In Customize Connection: set database name, username and password.
  5. A server node "MySQL Server at hostname:port" appears.
Database Administration - Start/Stop the Server and Create Databases
  1. You can start the MySQL server by right-clicking on the server node ⇒ select "start".
  2. Once the MySQL server is started and connected, you can see the list of databases by expanding the MySQL server node. You can create a new database by right-clicking on it and choose "Create Database...".
Create a new Connection

You need a connection to manipulate data. You can create multiple connections with different users and default databases.

  1. Open "Databases" and "MySQL server xxx" ⇒ Right-click on a database ⇒ Connect... ⇒ A Connection "jdbc:mysql://localhost:3306/databasename?..." appears at the end.
  2. OR: Right-click on the "Databases" ⇒ "New Connection..." ⇒ Select the driver "MySQL Connector/J" ⇒ Next ⇒ Enter hostname, port number, default database, a general username and password ⇒ "Test Connection" (make sure that MySQL is started) ⇒ Finish. A connection node "jdbc:mysql://hostname:port/databaseName" appears at the end.
Manipulating Data via a Connection
  1. Right-click on a connection node (e.g., "jdbc:mysql://hostname:port/databaseName") ⇒ Choose "Connect" (if not connected, provided that the MySQL server has been started).
  2. You can expand the connection node to view all the databases.
  3. Expand an existing database. There are three sub-nodes "Tables", "View" and "Procedures". Right-click on the "Tables" to create table or execute command. Similarly, right-click on the "View" and "Procedures".
  4. To view/manipulate the records in a table, right-click on the selected table ⇒ You can choose to "View Data...", "Execute Command...", etc.
  5. You can right-click on the connection to "connect" or "disconnect" from the server.
From a Project: Create a SQL Script and Run the Script

Suppose that your project needs to run a SQL script: Right-clicking on the project ⇒ New File ⇒ Other ⇒ SQL File ⇒ Enter the filename ⇒ Finish.

You can run the script by right-clicking on the SQL script ⇒ Run File ⇒ Select an existing connection (or create a new connection) to run the script. You could also run a single statement (right-click on the statement ⇒ Run Statement) or a selected group of statements (highlight the statements ⇒ Right-click ⇒ Run Selection).

Configure Tomcat Server

I shall assume that you have installed a Tomcat 10+ server (otherwise, read Tomcat-HowTo).

To configure Tomcat Server, select "Tools" menu ⇒ "Servers" ⇒ click "Add Servers":

  1. Choose Server: Select "Apache Tomcat or TomEE" ⇒ Next
  2. Installation and Login Details: In "Server Location", browse to set Tomcat installation directory ($CATALINA_HOME) ⇒ Enter the username/password of a tomcat user with "manager" role. You could either check the "create user if it does not exist" or define the tomcat user in "$CATALINA_HOME\conf\tomcat-users.xml" as follows:
    <tomcat-users>
       <role rolename="manager"/>
       <user username="tomcatmanager" password="xxxx" roles="manager,manager-script,admin" />
    </tomcat-users>
  3. Running the Tomcat Server: Choose "Services" ⇒ Expand "Servers" node ⇒ Right-click on the desired server ⇒ Start/Stop/Restart. You can also find these buttons on the left margin of the Tomcat console.

First Servlet and JSP

Create a New Webapp Project
  1. From "File" menu ⇒ choose "New Project...".
  2. In "Choose Project" ⇒ Under "Categories", choose "Java with Maven" ⇒ Under "Projects", choose "Web Application" ⇒ "Next".
  3. In "Name and Location" ⇒ In "Project Name", enter "FirstServletProject" ⇒ In "Project Location", select a suitable directory to save your works ⇒ In "Group Id", enter "com.nowhere" ⇒ Next.
  4. In "Settings" ⇒ Choose your server, or "add" a new server ⇒ Finish.
Write a Hello-World Servlet
  1. Right-click on the project "FirstServletProject" ⇒ New ⇒ Servlet...
  2. In "Name and Location" ⇒ In "Class Name", enter "HelloServlet" ⇒ In "Package", select "com.nowhere.firstservletproject" ⇒ Next.
  3. In "Configure Servlet Deployment" ⇒ In "Servlet Name", enter "HelloServlet" ⇒ In "URL Pattern", enter "/sayhello" ⇒ Finish.
  4. A sample servlet is created. You need to replace "javax." to "jakarta."
  5. To execute the servlet: Right-click on the project ⇒ run ⇒ Change the URL to http://localhost:8080/FirstServletProject/sayhello.
Write a Hello-World JSP
  1. Right-click on the project "FirstServletProject" ⇒ New ⇒ JSP...
  2. In "Name and Location" ⇒ In "File Name", enter "hello.jsp" ⇒ Finish.
  3. A sample hello-world JSP page is created. Add a few JSP lines under the <h1>:
    <% double num = Math.random();
       if (num > 0.75) { %>
          <h2>You'll have a lucky day!</h2><p>(<%= num %>)</p>
    <% } else { %>
          <h2>Well, life goes on ... </h2><p>(<%= num %>)</p>
    <% } %>
    <h3><a href="<%= request.getRequestURI() %>">Try Again</a></h3>
  4. To execute the JSP: Right-click on the JSP file ⇒ Run File (URL http://localhost:8080/FirstServletProject/hello.jsp)
Exporting to a WAR-file

A WAR (Web Archive) file is basically a zip file for distributing web application in single file. You can use WinZip or WinRAR to inspect or unzip the war file.

To distribute the project as a war-file, right-click project ⇒ "Clean and Build". The war file is created. Goto "File" tab, look under the "target". You can deploy the web application by dropping the war-file into Tomcat's "webapps" directory. Tomcat will automatically unzip the war-file and deploy the application upon startup.

Write a JSF Facelet (to check)
  1. Right-click on the project "FirstServletProject" ⇒ New ⇒ Other ⇒ Categories: Web ⇒ File Types: JSF Page ⇒ Next.
  2. In "Name and Location" ⇒ In "File Name", enter "hellojsf" ⇒ Finish.
  3. A sample hello-world JSF page "hellojsf.xhtml" is created, as follows:
    <?xml version='1.0' encoding='UTF-8' ?>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="jakarta.faces.html">
        <h:head>
            <title>Facelet Title</title>
        </h:head>
        <h:body>
            Hello from Facelets
        </h:body>
    </html>
    To run this facelet, right-click on the project ⇒ Run.

Debugging Web Applications in NetBeans

You can debug a webapp just like standalone application. For example, you can set breakpoints, single-step through the programs, etc.

VS Code for Jakarta EE [TODO]

[TODO]

Latest version tested: Eclipse JEE 2024-03, IntelliJ IDEA 2024.1, NetBeans 21, Jakarta Servlet 6.0
Last modified: April 2024