Java EE Programming/Frameworks

From Wikibooks, open books for an open world
< Java EE Programming(Redirected from Frameworks)
Jump to: navigation, search

Most of the Java 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]

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

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

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:

User.svg
Package.svg
  1. class MainProgram {
    
  2.     public static void main(String[] args) {
    
  3.         SubProgram.doProcess();
    
  4.     }
    
  5. }
    
  1. class SubProgram {
    
  2.     public static void doProcess() {
    
  3.         System.out.println("Do the process.");
    
  4.     }
    
  5. }
    

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:

Package.svg
User.svg
  1. class MainProgram {
    
  2.     public static void main(String[] args) {
    
  3.         SubProgram.doProcess();
    
  4.     }
    
  5. }
    
  1. class SubProgram {
    
  2.     public static void doProcess() {
    
  3.         System.out.println("Do the process.");
    
  4.     }
    
  5. }
    

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 Java EE[edit]

  • When you build a Java 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 Java EE application servers are Glassfish, JBoss and Geronimo. The most used Java EE servlet container is Apache 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 Java EE. They do lots of things you wouldn't be able to write on your own.

Bad practice[edit]

Emblem-very-very-evil.svg

Do not debug your code as you would debug it with a library. Java 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).

Monobook icon.svg

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.

Talk-icon.svg

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.