Jakarta EE Programming/Frameworks

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Most of the Jakarta EE technologies are used with a framework. This is the main difference with the plain old Java programming. So you must become familiar to the framework usage.

Framework vs library[edit | edit source]

A framework or a library is a code that you use but you don't write on your own. Consider the following code:

class MainProgram {
    public static void main(String[] args) {
        SubProgram.doProcess();
    }
}
class SubProgram {
    public static void doProcess() {
        System.out.println("Do the process.");
    }
}

The code on the left is calling the code on the right. If you use a library, you are writing the code on the left and you don't write the code on the right, so the library is on the right:

class MainProgram {
    public static void main(String[] args) {
        SubProgram.doProcess();
    }
}
class SubProgram {
    public static void doProcess() {
        System.out.println("Do the process.");
    }
}

If you use a framework, you are writing the code on the right and you don't write the code on the left, so the framework is on the left:

class MainProgram {
    public static void main(String[] args) {
        SubProgram.doProcess();
    }
}
class SubProgram {
    public static void doProcess() {
        System.out.println("Do the process.");
    }
}

This means that you will not launch your own code. You will launch a product that will read your code and use it. You don't know how your code will be used.

Frameworks in Jakarta EE[edit | edit source]

  • When you build a Jakarta EE, you generate a .ear or .war file. This file is respectively executed in an application server or a servlet container. Application servers and servlet containers are frameworks. The most used Jakarta EE application servers are Glassfish, WildFly and Geronimo. The most used Jakarta EE servlet container is Tomcat.
  • When you write a servlet, it will be executed in a servlet container, which is a framework.
  • When you write a .jsp, it will be read by the JSP compiler, which is a framework. The Apache Tomcat JSP compiler is Tomcat Jasper.

There are lots of other frameworks that are used in Jakarta EE. They do lots of things you wouldn't be able to write on your own.

Bad practice[edit | edit source]

Do not debug your code as you would debug it with a library. Jakarta EE coding needs other practices than Java coding. Even if you have the source code of the framework, do not try to read it. Most of the time, it is impossible to understand and you will waste your time. You will never find the way the framework is calling your code (a framework often generates some classes at runtime).

When you have an error, you have to read the framework specifications. It is the only way to find a solution. Most of the time, the stacktraces are very long. Only read the part that mentions your own code. If the stacktrace mentions an error code, search the error code in the framework specifications.

Sometimes, the framework specifications are not enough to find the bug. In this case, do not try to understand the error on your own, if the error is in a framework class. The best thing to do is to find someone who already encounters this error and knows the solution. It can be colleagues or people on an internet forum. The most famous is Stack Overflow.