This article provides a high-level introduction to J2EE. It is taken from chapter one of the book Beginning J2EE 1.4 From Novice to Professional, written by James L. Weaver, Kevin Mukhar, and Jim Crume (Apress, 2004; ISBN: 1590593413).
You are no doubt familiar with accessing simple, static HTML pages using a browser that sends a request to a web server, which in turn sends back a web page that’s stored at the server. In that role, the web server is simply being used as a virtual librarian that returns a document based on a request.
That model of serving up static web pages doesn’t provide for dynamically generated content, though. For example, suppose that the web client wants the server to return a list of HTML documents based on some query criteria. In that case, some means of generating HTML on the fly and returning it to the client is needed.
A servlet is a Java component implementing the javax.servlet.Servlet interface. It gets invoked as a result of a client request for that particular servlet. The servlet model is pretty generic and not necessarily bound to the Web and HTTP, but nearly all of the servlets that you’ll encounter will fall into that category. The web server receives a request for a given servlet in the form of an HTTP query. The web server in turn invokes the servlet and passes back the results to the requesting client. The servlet can be passed parameters from the requesting web client. The servlet is free to perform whatever computations it cares to, and spits out results in the form of HTML back to the client.
The servlet itself is managed and invoked by the J2EE servlet container. When the web server receives the request for the servlet, it notifies the servlet container, which in turn will load the servlet as necessary, and invoke the appropriate javax.servlet.Servlet interface service method to satisfy the request.
If you’ve done any web application programming using CGI (common gateway interface), you’ll be familiar with the limitations of that mechanism including lack of portability and no intrinsic support for session management (a much-overused example is the ability to maintain a list of items in a virtual shopping cart). If you’ve not done any development using CGI, consider yourself lucky and take my word for it—life with J2EE is a whole lot better! Java servlets are portable, and as you will see in later chapters, the servlet containers provide support for session management that allows you to write complex web-based applications. Servlets can also incorporate JavaBean components (which share little more than a name with Enterprise JavaBeans) that provide an additional degree of application compartmentalization.
JavaServer Pages, like servlets, are concerned with dynamically generated Web content. These two web components comprise a huge percentage of the content of real-world J2EE applications.
Building servlets involves building Java components that emit HTML. In a lot of cases that works out well, but isn’t very accessible for people who spend their time on the visual side of building web applications but don’t necessarily care to know much about software development. Enter the JavaServer Page. JavaServer Pages are HTML-based text documents with Java code with scriptlets—or chunks of Java code—embedded into the HTML document within XML tags.
When JSPs are deployed, something pretty remarkable happens—the contents of the JSP are rolled inside out like a sock, and a servlet is created based on the embedded tags and Java code scriptlets. This happens pretty much invisibly. If you care to, you can dig under the covers and see how it works (which makes learning about servlets all the more worthwhile):
The developer of a JavaServer Page can create both static and dynamic content in a JSP. Because content based on HTML, XML, etc. forms the basis of a JSP, a non-technical person can create and update that portion of a page. A more technical Java developer can create the snippets of Java code that will interface with data sources, perform calculations, and so on—the dynamic stuff.
Since an executing JSP is a servlet, JSP provides the same support for session management as servlets. JSPs can also load and call methods of JavaBean components, access server-based data sources, or perform complex calculations at the server.
JSPs are introduced in detail in Chapter 3. Chapter 4 continues with more advanced JSP concepts.