Eclipse JEE (for Jakarta EE)
Install
- 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). - Unzip.
- You can launch Eclipse by running
eclipse.exe
.
Configure JDK
Assume that you have installed the latest JDK (otherwise, read "JDK HowTo").
- Launch Eclipse.
- Set Default JRE: Select "Window" menu ⇒ Preferences ⇒ Java ⇒ Installed JRE ⇒ set default to latest JDK.
- 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:
- 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.
- Switch to "Database Development" Perspective: Select "Window" menu ⇒ Open Perspective ⇒ Other ⇒ Database Development.
- 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.- In "Connection Profile", choose "MySQL" ⇒ Next.
- In "Drivers", click the "+" icon for "New Driver Definition".
- In "Specify a Driver Template and Definition Name":
- 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".
- 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.
- Switch to "Properties" tab ⇒ Check Connection URL, Database name, Username, Password.
- Start the MySQL server.
- Click "Test Connection" (MySQL server has been started).
- In "Datasource Explorer", you can "connect" and "disconnect" the connection.
- 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.
- 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".
- 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").
- Launch Eclipse.
- Configure Tomcat Server:
- Select "File" menu ⇒ New ⇒ Other... ⇒ Server ⇒ Server ⇒ Apache ⇒ Tomcat v10.x server ⇒ Next.
- In "Tomcat installed directory": Browse to select Tomcat's installed directory (
TOMCAT_HOME
) ⇒ Finish.
- Start/Restart/Stop Tomcat Server: Select bottom "Server" tab ⇒ Push Start/Stop server buttons.
First Servlet
- Create New Webapp Project: Launch Eclipse ⇒ File ⇒ New ⇒
Dynamic Web Project. In "Project name", enter "
FirstServletProject
". - Create New Package: Expand "Java Resources" node ⇒ Right-click on "
src/main/java
" ⇒ New ⇒ Package ⇒ Enter "com.nowhere
". - Write the First Servlet: Right-click on "
com.nowhere
" ⇒ New ⇒ Class (or "Servlet", if exists) ⇒ Enter "HelloServlet
". Enter the followings:
How It Works: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 } }
- [TODO]
- 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
’sservlet-api.jar
. - 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).
- 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) andsayhello
the the URL of theHelloServlet
(configured via@WebServlet
annotation). - Check out the Directories and Resources:
- Under
src\main\webapp
, there is aWEB-INF
with a deployment descriptorweb.xml
. - Under
src\main\webapp
, there is also aMETA-INF
with withMANIFEST.MF
. - Under "Referenced Libraries", there is a "
servlet-api.jar
" (configured via External JARs). Expand it, you can findjakarta.servlet
andjakarta.servlet.http
packages, andHttpServlet
class.
- Under
First JSP
- Create a new Webapp Project: Select "File" menu ⇒ New ⇒ Dynamic Web Project (under "Web" category) ⇒ In "Project Name", enter "
FirstJspProject
" ⇒ Finish. - Create a new JSP File: Right-click on "
webapp
" ⇒ New ⇒ JSP File ⇒ In "File Name", enter "hello.jsp
" ⇒ "Finish". - 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>
- If error on
jakarta.servlet
appears, follow the previous "Write the First Servlet" section to addservlet-api.jar
as external JAR. - 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
- 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.
- 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. - 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.
- Assume that you have install the Tomcat 10+ server. Otherwise, see "Tomcat HowTo".
- 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. - 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 descriptorweb.xml
.
- Configure Tomcat Server:
- Click top menu item "Current File" ⇒ Edit configuration ⇒ "+" (Add new configuration) ⇒ Tomcat Server ⇒ Local.
- In "Server" pane: Configure… ⇒ In "Tomcat Home", browse to select your tomcat installed directory.
- In "Deployment" pane: "+" (add) ⇒ artifact… ⇒ choose either the uncompressed “FirstServletProject:war exploded”, or the compressed “FirstServletProject:war” ⇒ OK
- 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 underTomcat\conf\web.xml
).
First Servlet
Let's continue to write our first servlet:
- Continue from the previous maven project "
FirstServletProject
". - Expand "src" ⇒ Right-click "main" ⇒ new ⇒ Directory ⇒ Enter "
java
" (to be used as the Java source root). - Right-click "
java
" ⇒ New ⇒ Package ⇒ Enter "com.nowhere
". - Right-click "
com.nowhere
" ⇒ New ⇒ Java Class ⇒ Enter "HelloServlet
" ⇒ Write the "HelloServlet.java
" (as in the "Eclipse JEE" section). - Open "
pom.xml
":- Include
jakarta.servlet
<dependency>
under<dependencies>
. This dependency is used to include the Jakarta Servlet 6.0 packages (jakarta.servlet
andjakarta.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). Thescope
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. - Also include the properties to set the JDK compiler and target levels.
pom.xml
" is produced as follows:
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.<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>
- Include
- 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:
- New Project ⇒ Empty Project ⇒ In "Name", Enter "
JakartaServletProjects
". - 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". - Expand "
helloapp
" ⇒ src ⇒ Right-click "main" ⇒ New ⇒ Directory ⇒ Enter "java
" (as Java source root). - Right-click "
java
" ⇒ New ⇒ Package ⇒ Enter "com.nowhere
". Keep all the servlets here. - 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.
- 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.) - Switch to "Basic Properties" tab: Enter the hostname, port number, username and password.
- Switch to "Admin Properties" tab:
- Leave the "Path/URL to admin tool" empty.
- In "Path to start command", browse to select "
<MYSQL_HOME>\bin\mysqld.exe
"; in the "Arguments", enter "--console
" - In "Path to stop command", browse to select "
<MYSQL_HOME>\bin\mysqladmin.exe
", in the "Arguments", enter "-u username -ppassword shutdown
".
- 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.
- A server node "
MySQL Server at hostname:port
" appears.
Database Administration - Start/Stop the Server and Create Databases
- You can start the MySQL server by right-clicking on the server node ⇒ select "start".
- 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.
- Open "Databases" and "MySQL server xxx" ⇒ Right-click on a database ⇒ Connect... ⇒ A Connection "jdbc:mysql://localhost:3306/databasename?..." appears at the end.
- 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
- 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). - You can expand the connection node to view all the databases.
- 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".
- To view/manipulate the records in a table, right-click on the selected table ⇒ You can choose to "View Data...", "Execute Command...", etc.
- 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":
- Choose Server: Select "Apache Tomcat or TomEE" ⇒ Next
- 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>
- 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
- From "File" menu ⇒ choose "New Project...".
- In "Choose Project" ⇒ Under "Categories", choose "Java with Maven" ⇒ Under "Projects", choose "Web Application" ⇒ "Next".
- 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. - In "Settings" ⇒ Choose your server, or "add" a new server ⇒ Finish.
Write a Hello-World Servlet
- Right-click on the project "
FirstServletProject
" ⇒ New ⇒ Servlet... - In "Name and Location" ⇒ In "Class Name", enter "
HelloServlet
" ⇒ In "Package", select "com.nowhere.firstservletproject
" ⇒ Next. - In "Configure Servlet Deployment" ⇒ In "Servlet Name", enter "
HelloServlet
" ⇒ In "URL Pattern", enter "/sayhello
" ⇒ Finish. - A sample servlet is created. You need to replace "
javax.
" to "jakarta.
" - To execute the servlet: Right-click on the project ⇒ run ⇒ Change the URL to
http://localhost:8080/FirstServletProject/sayhello
.
Write a Hello-World JSP
- Right-click on the project "
FirstServletProject
" ⇒ New ⇒ JSP... - In "Name and Location" ⇒ In "File Name", enter "
hello.jsp
" ⇒ Finish. - 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>
- 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)
- Right-click on the project "
FirstServletProject
" ⇒ New ⇒ Other ⇒ Categories: Web ⇒ File Types: JSF Page ⇒ Next. - In "Name and Location" ⇒ In "File Name", enter "
hellojsf
" ⇒ Finish. - 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]