An Introduction to J2EE
A Java edition that serve business computing and WWW needs
Internet based Java applications are not restricted to just JWS applications and Applets. Java offers J2EE (Java Enterprise Edition) to serve business computing and WWW needs. J2EE applications are very different from applets or even JWS Applications.
While an applet is deployed as part of a web page and runs on the client's machine, enterprise applications run from a central server - a computer or group of computers that are ran by the organization. The server 'hosts' the organization's domain. This domain is a web site that may consist of static HTML pages, dynamic pages that process forms, enterprise components like Enterprise Java Beans, web services that carry out specific functions, and more. The scope of Java Enterprise Applications has a wide range: domains that use J2EE technology to offer services online include banks, educational institutions, airlines, auction houses, online publishing companies and many others.
What sort of work do Enterprise Applications and components carry out? In one scenario: a servlet may process a bank customer's submitted form and pass its information on to a database or an EJB; an EJB or Enterprise Java Bean (a program that uses a specific package within J2EE) in the server hosting a Bank's domain may do the work of retrieving a user's account balance from a database on the server; a web service may check for the availability of funds in a foreign branch; and a JSP or Java Server Page may format results and send them back to the user in the form of a web page. On the other hand, a servlet may process a user's keyword entries into an article search form and retrieve matching entries from a database of news articles stored in XML format and display the articles on the Internet browser. The possibilities are endless.
All this work is accomplished through the TCP/IP protocol - a way of sending information and data converted to 'bits' (sequences of 0s and 1s - represented as off and on pulses) over long distances via cables and phone lines. Running over TCP/IP is HTTP or Hypertext Transfer Protocol. Servers 'wait' on a specific 'port'. A port is a socket that receives TCP/IP data which is translated into HTTP requests. An HTTP request is a message from a remote computer (referred to as a client). A request may ask for a specific web page or may contain data from a submitted form. Basically, an HTTP Request is sent to the server of a domain or web site each time a user clicks on a link or submits a form.
Figure 8a: HTTP Messages
The Enterprise Java Applications on the server handle these requests. They construct and send back HTTP response messages to the client who made the request via the Internet through the TCP/IP protocol. This response most often translates into an HTML web page but may be any type of data.
J2EE applications are made up of components. A component is a self-contained functional software unit that is assembled using J2EE related classes and protocols and communicates with other J2EE components. Application clients and applets are components that run on client computers. Java Servlets and JSP are web components that run on a server that handle HTTP requests and responses and construct web pages on the fly. EJB or Enterprise Java Beans are business components that carry out specialized business tasks (portfolio calculations, revenue calculations, compound interest etc.) through a series of well defined operations. The only difference between J2EE components and standard Java applications is that the former make up parts of a whole - they form an application that complies with the J2EE specification. J2EE components are deployed (or installed) on a J2EE server.
Servlets and JSPs are thin clients that usually just pass on tasks like database queries, data calculations and other heavyweight operations to EJBs on the J2EE server. An applet is a small client application that executes in the Java virtual machine installed in the Web browser of the client computer. A J2EE application client provides a rich user interface created from Swing or the Abstract Windowing Toolkit (AWT) APIs (and sometimes even a command-line interface). Application clients directly access EJBs running in the business tier but can also open TCP/IP connections and transfer HTTP messages.
Figure 8b: J2EE Components (from java.sun.com)
There are three kinds of enterprise beans: session beans, entity beans, and message-driven beans. A session bean is used to model a temporary conversation with a client. A session bean on an airline's domain may deal with a generic unregistered user's online ticket search. If the user suddenly closes his browser, the session bean and the search data are gone. An entity bean represents persistent data stored in one row of a database table. After user log in, all transactions, such as ticket purchase would take place through Entity beans. Underlying services ensure that the entity bean data is saved even if some malfunction occurs at the server level or if the user closes the browser all of a sudden. A message-driven bean combines session bean features with a Java Message Service ("JMS") message listener. A user may place an advance request for a service that is only available at certain times of the day. In this case, a message driven bean may be used to add his request to a queue that will be processed when the service becomes available later in the day.
Containers are the interface between J2EE components and Operating System related platform-specific functionality. All J2EE components should be deployed onto an appropriate container before execution. The container 'translates' the needs of the component to the OS and takes care of the actual execution. Containers are used to customize and manage security, transactions, Java Naming and Directory Interface (JNDI) lookups on filesystems and devices, and remote connections. The container also manages enterprise bean and servlet life cycles, database connections, and access to the J2EE platform APIs.
Figure 8c: Typical J2EE Architecture
Servlets are programs that run on a Web server and construct web pages on the fly. Dynamic web pages are based on data submitted by users through forms or other means. Servlet technology may be used to drive results pages from search engines and online order processing systems. Java servlets are more efficient, easier to use, more powerful, more portable, and cheaper than traditional CGI than many alternative CGI-like technologies. Servlets are very efficient because each HTTP request is handled by a lightweight Java thread within a servlet container rather than an operating system process. Only a single instance of a servlet class is loaded onto memory and each request for the page is handled by a thread. Other CGI type programs execute once per request.
A servlet's life cycle begins with a call to an init() method. The init() method may be overridden if any variables and parameters need one time initialization or if a reference to the Servlet's context is sought. More important are the doPost and doGet methods that take references to the HTTP request message and HTTP response message as parameters. Overrides of these methods provide the actual functionality of the servlet.
Java Server Pages (JSP) is a technology that allows the developer to combine regular, static HTML with dynamically-generated HTML. The HTML is interspersed with Java commands surrounded by special tags (<% and %>). JSPS are compiled and formed into a servlet by the container at run time.
Let us experiment with multi-tiered architecture and J2EE components by downloading a free Servlet container called Tomcat and using it to run elementary J2EE components. As you may recall, J2EE components should first be deployed within a container that manages security, transactions, and others, and serves as an interface between the component and the underlying OS. Tomcat is bundled with NetBeans 5.5. However, downloading and setting up the server is a good exercise. Also, the servlet API is bundled with tomcat; this is all we need to develop thin clients.
- Go to the Tomcat 5.5 downloads page.
- Click on the Windows Service Installer (pgp, md5) (about 5 MB) link under 'Core' in 'Binary Distributions'.
- Once you have downloaded the product, double click on the downloaded file (apache-tomcat-5.5.20.exe) to install it on your computer. You may be prompted for a user name/password. Enter very simple values that you can remember and use throughout this exercise. The general convention for a test environment is to use just the user name 'admin' with no password.
- Once tomcat has been successfully installed, the following items will be available on your start menu under 'Apache Tomcat 5.5':
Figure 8d: Tomcat menu items with key use
- Now, let us deploy the JWS application we created earlier over the web.
- Open the ShowFruits.jnlp file.
- Change 'codebase = "file:///......' to the following and save it:
codebase="http://localhost:8080/"
- Click on Start -> Apache Tomcat 5.5 -> Tomcat 5.5 Program Directory
- Go to the Subfolder wepapps -> ROOT (typically, C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\ROOT)
- Now, copy the modified Showfruits.jnlp and the original ShowFruits.jar into this directory.
- If you have not already started up Tomcat, Click on Start -> Apache Tomcat 5.5 -> Configure Tomcat and click on the 'start' button. The service should start up. Starting Tomcat gets your web server and the JSP/servlet container going.
- If you do not see the following icon -
- on the bottom right hand side of the monitor, you can bring it up by clicking on Start -> Apache Tomcat 5.5 -> Configure Tomcat. You can now bring up the configure Tomcat pop up by double clicking on this icon. - Now, open up Internet Explorer and enter the following URL: http://localhost:8080/ShowFruits.jnlp. You will see the following screen. A pop-up will appear with a message that the application is unsigned and will ask that you verify if you really want to run it.
Screen Shot 8i: ShowFruits Over the Web
- The Applet will start exactly as it did before; only, you have now deployed it over the Internet. Compare screenshot 8f with screenshot 8i. The first screenshot simply says 'Java Loading' while the new screenshot shows the value of the title and vendor tags under the information tag in the JNLP file. This is because the Tomcat server actually knows how to interpret and process the JNLP configuration file and deploy the application correctly.
In the next chapter, we will learn to deploy a web application containing JSP and servlets using tomcat. We will later write programs that takes input from a database or an XML file.