Java EE application is a packaged software which run in Java EE application server. There are two types of it. One is for web usage which is a packaged file with .war extension and the other is for enterprise usage which is a packaged file with .ear as extension. There are a lot of components in those two packaged file. Such as your java class, application deployment descriptor, third party libraries that your java class depended, Servlet, JSP, Listeners and Filters.
WAR (Web Application Archive)
Generally WAR includes servlet, jsp, listeners and filters.
Servlet is the most important component in a WAR. It is just a java file which extend HttpServlet. It receives Http request from client and then call some java bean to do business logic, after that it will give response data back to the client. Servlet is the foundation of a lot of modern java frameworks such as struts, spring, JSF etc. If you want to learn all those popular frameworks you had better learn servlet first.
JSP is the abbreviation of Java Server Pages. It is used to create GUI( Graphical User Interfaces) part for your web application. There are also a lot of technical skills in JSP such as JSTL (JSP Standard Tag Library), Custom Tags, JUEL( Java Unified Extension Language), localization and internationalization.
Listeners is java classes which listen on all life cycle events of web application. The events include servlet container start events, servlet container stop events, session create events and session destroy events. It is very easy to create a listener in, you just need to make your java code to implement ServletContextListener interface to create an application level listener, and implement HttpSessionListener to create a session level listener.
Filters is java classes which can intercept request to servlet, it can also change the response data before servlet send data back to client. You can do a lot of things in Filter such as format request data, authentication, set response page encoding, and encryption. It is especially useful when you want to do pre-processing or post-processing for not only one servlet. If you want to create a servlet Filter class, you just need to implement javax.servlet.Filter interface.
Web application is deployed and run in web application server. You can package it in a .war file or in unarchived directory. WAR is the abbreviation of Web Application Archive. WAR file is just a zip format compressed file. Whether archived or unarchived, your web application should has following directory structure.
As you can see from above picture, the WEB-INF directory is the most important directory. It include all resource files for your web application.
WEB-INF/classes include all the java class files you have created.
WEB-INF/lib include all the third party jar files your java code will use.
WEB-INF/i18n is used to store localization (L10n) and internationalization (i18n) files. This directory is not a must have but it is a good habit to use this directory.
WEB-INF/tags is used to store JSP tag files
WEB-INF/tld is used to save tag library descriptors.
WEB-INF/web.xml is the key in web application. It is also called deployment descriptor. It is a xml format file. It contains all the definition of servlet, listener, filter in your web application. You can also configure other information in web.xml. Such as taglib, security and authentication, welcome file list, error handler etc. We will introduce more about web.xml in other articles.
EAR (Enterprise Application Archives)
There is also another zip format compressed archive file in Java EE, it is EAR( the abbreviation of Enterprise Application Archives). EAR archive file include jars, wars and other configuration files. It is used to package multiple web modules, EJB modules and client modules into one single deployable archive. The file extension is .ear. Create .ear file is just like building LEGO, you just need to package several Java EE modules into it, do not need more coding. Below is the directory structure for EAR.
META-INF/application.xml is the key file in EAR. It is an xml file. It is used to configure the deployment settings for modules (web modules, ejb moduels) and components. Java EE server will execute and call modules or components at run time according to this file.
There are four kind of modules that you can deploy in EAR.
- EJB module is also a zip file with .jar file extension. It include enterprise java bean classes and ejb-jar.xml which is EJB deployment descriptor xml file. Enterprise java bean runs in EJB container. WebSphere, Weblogic and Jboss are popular Enterprise Application Containers which include EJB container and Web container. Tomcat and Jetty are just Web containers which can not run EJB.
- Application client module is a .jar extension file. It contains class files and application-client.xml which configure deployment settings for this client application.
- Resource adapter modules is a rar compress file. The file extension is .rar. It includes all classes, interfaces, native libraries and resource adapter deployment descriptor.
Class Loader Architecture
The class loader order for Java EE is different from Java SE application.
Java SE use parent-first class loader model, when you load a java class in low-level ClassLoader, it will delegates the request to it’s parent ClassLoader first, then the parent ClassLoader will delegate the request to it’s parent ClassLoader also until the root ClassLoader. So the required class will be find in the order from root ClassLoader to child ClassLoader int the class loader tree. This way can make your code secure.
Java EE use parent-last class loader model, Because the same third-party java library may exist both in the server ClassLoader and your application ClassLoader. But their version maybe conflict. Third party library version conflicts may also exist between different applications. So Java EE application’s ClassLoader is isolated from each other, this way can reduce conflicts between them and improve code security. Code in different applications will not harming each other. When the application can not find the required class in it’s own ClassLoader, it will delegate the request to it’s parent ClassLoader then.