This article, the second of two parts, will take you through writing a simple application with J2EE 1.4 SDK. It is excerpted from chapter two of the book Beginning J2EE 1.4 From Novice to Professional, written by James L. Weaver, Kevin Mukhar, and Jim Crume (Apress, 2005; ISBN: 1590593413).
Getting Set with J2EE, conclusion - How it works (Page 3 of 4 )
Before we look at what we've done in this example, you should stop the J2EE server by selecting the following option from the Windows Start menu:
Start | All Programs | Sun Microsystems | J2EE 1.4 SDK | Stop Default Domain
How It Works
The JSP file that you created is a text file that consists of HTML and embedded snippets of code. Notice in this file that there are tags with enclosed Java code, as we discussed in Chapter 1:
<%-- file: index.jsp desc: Test installation of J2EE SDK 1.4 --%> <html> <head> <title>Hello World - test the J2EE SDK installation </title> </head> <body> <% for (int i = 1; i < 5; i++) { %> <h<%=i%>>Hello World</h<%=i%>> <% } %> </body> </html>
When the JSP is compiled into a servlet, that servlet code expands the JSP's code snippets and HTML into code that writes HTML to an output stream:
out.write("\n\n"); out.write("<html>\n"); out.write("<head>\n"); out.write(" <title>Hello World - test the J2EE SDK installation"); out.write(" </title>\n"); out.write("</head>\n"); out.write("<body>\n"); for (int i = 1; i < 5; i++) { out.write("\n "); out.write("<h"); out.write(String.valueOf(i)); out.write(">Hello World"); out.write("</h"); out.write(String.valueOf(i)); out.write(">\n"); } out.write("</body>\n"); out.write("</html>\n");
That code, when executed, will write the following HTML code to the stream that is sent back to the requesting browser:
<html> <head> <title>Hello Hello World - test the J2EE SDK installation </title> </head> <body> <h1>Hello World</h1> <h2>Hello World</h2> <h3>Hello World</h3> <h4>Hello World</h4> </body> </html>
That's how the JSP code works. The process of packaging and deployment has a few more steps. Let's dig in a bit and see what's happening:
In order to deploy a J2EE application to a server, it has to be bundled up into an archive--that's a single file that packages up all the requisite files. The Web Archive (WAR) has to contain the components that we've created for the application (the JSP file), as well other support files. Those support files include a deployment descriptor that tells the server what's contained in the WAR and how to run it, a manifest for the archive, which is an application table of contents, and a file containing deployment information specific to the J2EE reference implementation server:
Once those contents have been assembled into a WAR file, that WAR can then be deployed to the J2EE server. That process sends the archive to the server, which then reads the deployment descriptor to determine how to unbundle the contents. In the case of this application, it sees that the WAR contains a JSP, and so it compiles that JSP into a servlet.
In order to run the application once it is deployed, you have to request the JSP by requesting an URL with your web browser. Notice that the URL consists of the protocol (http), the server name (localhost), the root context of the application (hello) and the requested resource (index.jsp):
The server receives the incoming HTTP request, and uses the deployment information to invoke the appropriate servlet in a servlet container. The servlet writes HTML to an output stream, which is returned to the web browser by the server.
Problems and Solutions
If you run into any difficulties, the following table lists some common problems and how to fix them:
Problem
Solution
Verifier reports errors.
Carefully retrace your steps and ensure that the steps are followed correctly as described.
When testing the JSP, the web browser reports "Page cannot be displayed" when tryin to open the URL http://localhost:8080.
Make certain that there weren't any errors reported when starting the J2EE server.
Make certain that you've specified port "8080" in the URL (this is the default port used by the J2EE server).
When testing the JSP, it reports a compilation error in the web browser.
Double-check the code in index.jsp. If you've mistyped something, the server won't be able to compile the JSP. The message in the web browser should give you a hint where to look.