
| Goals | After completing this chapter, the student will be able to
|
| Prerequisites | The student will need to be famliar with web browsers, an understanding of HTML, and familiarity with Java and its use in programming. |
| Objectives | This chapter is presented to provide an understanding of the role that Servlets and Java Server Pages play in the web application development process and to understand what J2EE is. |
J2EE (Java 2 Enterprise Edition) is a specification for developing enterprise and distributed applications from JavaSoft (Sun Microsystems). J2EE is not one thing; it encompasses a large set of technologies:
In this course, we will talk about JSP, servlets, EJB, JDBC, and touch on JNDI.
J2EE is useful for creating web applications like Yahoo! Mail, a web-based stock trading system such as E*TRADE, or an online auction house like eBay. Anytime there is a need for many people to access a collection of data in a distributed manner, an implementation of the J2EE specification can provide a solution.
Since J2EE is a specification, it isn't located anywhere. Instead vendors make their products adhere to the J2EE specification.
Examples of the vendors offering J2EE compliant products are
In this course, we will concentrate on JBoss, which is a free, open-source J2EE server. The JBoss server itself is written in Java, so it can run on Windows, Linux, Mac OS X, Solaris, or any other platform which fully supports the Java virtual machine.
Java Server Pages (JSP) are Sun's solution to the generation of dynamic HTML. With JSPs, you can generate HTML on the fly, which is important because
JSPs are HTML-like pages that can contain a mixture of HTML tags, data, and Java code. Here is an example of a JSP that contains a simple for loop to count to 10:
(If you have not already done so, you can download this and other examples used in this course. Mac OS X or other UNIX users click here instead.)
| code/Chapter1/counter/counter.jsp |
|---|
|
We then deploy this on a web server that knows how to handle JSPs. (We'll see how to deploy it later in this chapter.) The JSP will be mapped to a specific web address on that server, and when a user comes along and types in that web address, the web server will find this JSP, dynamically compile the Java code in it, run it, merge it with the HTML tags that are interspersed around the Java code, and output an HTML page back to the user's browser. The generated HTML will look like this:
|
Servlets are pure Java objects that generate HTML by writing it to a stream. The files do not contain any HTML tags; they simply contain Java code which write out HTML (as strings) to produce the page that is sent to the user's browser. Historically, this used to be the only way to generate dynamic content in Java, but it is quite painful to develop any large scale application with a bunch of servlets running around doing println() statements. This is why Sun invented JSPs in the first place: to compete with the ease-of-development of Microsoft's ASP files, which could -- you guessed it -- mix HTML tags and code in a single file.
JSPs and servlets are two different ways to accomplish the same goal: generating dynamic HTML pages using Java code. One puts Java code in your HTML, and one puts HTML in your Java code. Functionally, they are equivalent. In fact, under the covers, the web server takes a JSP and converts it to the corresponding servlet and dynamically compiles it.
Here is a Java servlet that produces the same content as the JSP we just saw above:
| code/Chapter1/counter/counterAsServlet.java |
|---|
|
You can still write Java servlets manually if you want to, and many people still swear by them. However, JSPs are more flexible, and as we'll see in later chapters, can be combined with other features to create a powerful templating engine. We will not be writing servlets in this course, although we will see them sneak in behind the scenes in chapter 4.
JSPs live in an object called a container, which is essentially a server. JSPs can define information for the container with directives.
Here is what directives look like in a general form:
<%@ directive attribute="someValue" attribute="anotherValue" ... %>
There are three directives:
<%@ page language="java" %> will always be the first line of every JSP file.
Declarations are used to specify supplemental methods and variables. You can think of these are the page's private functions; they can only be called by the JSP where they are defined, or by another JSP that includes it (using the <@ include > directive).
Here is a sample declaration:
<%!
// this integer can be used anywhere in this JSP page
private int myVariable = -1;
// this function can be called from anywhere in this JSP page
public boolean isPositive() {
return ( myVariable > 0 );
}
%>
Scriptlets are bits of Java code. They can do anything (the full power of Java is available in every JSP), but they will most likely concentrate on generating HTML code or setting up variables to be part of later expressions (see below).
The first JSP we saw above, counter.jsp, contained a scriptlet with a for loop.
Expressions are special-purpose mini-scriptlets used for evaluating expressions. This could be something as simple as outputting the value of a variable, or a more complicated Java expression, like calling a function and outputting the result.
The JSP example counter.jsp also contained an expression:
<%= counter %>
Note that counter is defined as an int, but we do not need to explicitly convert it to a string. Expressions are implicitly converted to strings when they are output. (The println() function works the same way; it can take pretty much anything and print it. That makes sense if you think about it. Remember that JSPs automatically get converted to Java servlets under the covers. So an expression like this in a JSP file is converted to a println statement in a servlet that you never see. So anything println can print, an expression can contain, without any explicit conversion.)
Now lets create a web application using JBoss with Tomcat. JBoss is an open-sourced J2EE server from the JBoss Group (www.jboss.org). It provides support for integrating Tomcat, which is the open-sourced Servlet and JSP engine from the Apache Group's Jakarta project (jakarta.apache.org).
Note: If JBoss with Tomcat is not already installed, the instructor will provide assistance with getting it installed.
The files for a J2EE web application are placed in a web application archive file (.war extension). Inside the web application archive are JSPs, HTML pages, .properties files, XML configuration files, images, and class files, as well as any customized data files that might need to be included. At a minimum, the web application archive will consist of a WEB-INF folder at the top level. Inside the WEB-INF folder will be a web.xml file. The web.xml file provides the J2EE server with information specific to the web application contained in the same web application archive. For example, it provides the name for the default page to serve, if one is not specified.
Do this:
| code/Chapter1/MyGreeting |
|---|
MyGreeting
|
+-- HelloWorld.jsp (*)
|
+-- WEB-INF
|
+-- web.xml (*)
(*) denotes a file |
| code/Chapter1/MyGreeting/HelloWorld.jsp |
|---|
|
| code/Chapter1/MyGreeting/WEB-INF/web.xml |
|---|
|
|
|
|
Create a web application that displays the current time in a web page. Provide a link inside the web page to reload the page with the an updated time. Use JSPs in order to produce the dynamic content.