Java Web Programming with Eclipse contents
Last modified January 06, 2009 01:47 pm
The term web application refers to a software system that provides a user interface through a web browser. Examples of web applications include blogs, wikis, online shopping, search engines, etc. Web application development became an important discipline following the adoption of the Internet by ordinary users. Many businesses now rely heavily on the web for both internal applications and to provide services to customers, and so there are many employment opportunities open to individuals with web development skills.
Web sites can be roughly classified as static or dynamic. Static web sites are those sites that use a web server to provide access to HTML documents that are stored in the file system. Dynamic web sites are those sites that construct the content of web pages from data that is stored in a database. The databases on which these dynamic sites are built are typically modified as a result of user interaction with site. Thus, users are presented with web pages that are uniquely generated for them based on their previous interactions with the site. The trend is for web pages to be generated from databases rather than being read from the file system.
There are numerous languages and frameworks that are used for web application development. Java is one of the older and more established languages in which web applications have been developed. This book covers Java-based web application development using Servlets and JSP. This book also covers the news feed protocol RSS version 2.0, and REST-based web services.
Java is a strict object-oriented language in which all function calls are made to either static methods of classes or to non-static methods that are invoked through class instances. These classes are organized into namespaces called packages, so that unqualified class names do not need to be globally unique.
An application programming interface (API) is a specification that defines how user code can access system functionality. The Java API refers to the specification that defines how Java code may access functionality, such as opening a file in the file system, creating a socket connection with another process, creating a linked-list of objects, etc. For example, the following line creates an instance of the Socket class, which can be used to make TCP connections to other processes.
java.net.Socket socket = new java.net.Socket("localhost", 8080);
The above line of code can be simplified by importing the class java.net.Socket into the local namespace by adding the following line just after the package declaration of a java source code file.
The above import statement allows for the following simplified version of the socket creation code give above, in which the package prefix qualifiers are dropped from the Socket class.
Socket socket = new Socket("localhost", 8080);
The Socket class is an example of a class that is part of the core Java API, which is available in all standard Java virtual machine (JVM) environments. However, web applications typically additional functionality that is not part of the core Java API. In particular, conventional web applications need to be able to access functionality provided through the Servlet API. Implementations of the Servlet API are provided by third parties, such as Apache, IBM, Oracle, etc. In fact, the Servlet API is provided by something called a web container (or Servlet container), which is defined within an extensive specification called Java 2 Enterprise Edition (J2EE). A web container is actually a web server that loads and executes Java Servlets to process incoming requests from browsers (or other HTTP clients).
The Java 2 Enterprise Edition is a specification developed by Sun Microsystems with input from other major companies in the industry through a mechanism called the Java Community Process (JCP). J2EE describes a set of services that application developers utilize to solve enterprise computing problems. There are many third party implementations of J2EE, including both expensive proprietary implementations, and free open source implementations. The Apache Software Foundation provides a free open source implementation of J2EE web containers, called Tomcat, which is widely used in the industry for web applications. This book describes the use of Tomcat, and does not discuss other web containers.
Tomcat is not a complete implementation of the J2EE standard. In particular, it does not provide an Enterprise Java Beans (EJB) container. JBoss is a popular, free, open source implementation of the complete J2EE standard. The JBoss project itself does not provide an implementation of the Web container; instead, it requires that a third party web container be plugged in. However, Tomcat is the default Web container that comes packaged and pre-configured with JBoss. A popular alternative to Tomcat is Resin and a popular non-free alternative to JBoss is IBM's WebSphere.
The use of Java Server Pages (JSP) is covered in this book. JSP is a notation that is added to HTML files, so that per-request functionality can be added to otherwise statically-defined HTML.
It is possible to place all program functionality into JSP files. However, this practice is widely frowned against, because it leads to code that is difficult to maintain after a project grows in size. There are several reasons for this. First, JSP files are compiled at runtime by the web container, so that syntax errors are detected at runtime. By placing this code into Servlets, syntax errors are detected at compile time, which allows syntax errors to be caught and fixed at an earlier point in time. Second, JSP files can not make use of inheritance, so that projects solely written in JSP look more like functional programs rather than object-oriented programs, and thus do not make use of the benefits provided by object-oriented design.
One can move away from pure JSP designs in two degrees: the java beans approach, and the servlet approach. The Java beans approach moves database access and other logic into Java beans, which are then invoked from the JSP. The Servlet approach also moves database access and other logic into Java beans, but also moves controller logic into Java Servlets, and invokes most of the Java bean functionality from the servlets. This book illustrates the Servlet approach.
Rich Site Summary (RSS) is an XML-based format for syndicating content. In this book, we show how RSS can be added to a Java-based Web application from both the client side and from the server side. Rather than develop all the needed code from scratch, we utilize functionality provided by two different open source projects: JDOM and Informa. JDOM is a library that simplifies the processing of XML documents, and Informa is a library that simplifies the parsing of RSS documents. These libraries come in the form of jar files, which are zip files containing the byte code that comprise Java class and interface definitions. To access the functionality provided by these libraries, the jar files must be placed in what is called the classpath. The classpath is basically a list of folders and jar files that the Java classloader searches to find definitions of Java classes. When the jar file of a library is present in the classpath, then its API becomes visible to application code.
There is a tool, called javadoc, that is used to generated API documentation from code and the comments embedded within it. This generated documentation is in the form of HTML pages, and is thus viewed within a browser. It is important to become familiar with javadoc-generated API documentation, because it is the standard means to document Java APIs.
Representational State Transfer (REST) is formally an architectural style for building distributed software applications, that is, software systems that run on separate computers and that communicate with each other over a network. However, the term is informally used to represent an approach to building Web services in which message data is placed in XML documents and transported using HTTP. In this second meaning, REST is an alternative to the more complex form of Web services based on SOAP and WSDL.
Software systems that provide programmatic interfaces to other systems (as opposed to browsers operated by human users) are similar to web applications in the sense that they rely on the Internet and usually rely on the HTTP protocol. These applications are referred to as web services. This book provides an introduction to REST-based web services.
When developing software, programmers tend to rely on integrated development environments (IDEs). An IDE provides important time-saving functionality, such as syntax checking, code assist and re-factoring. With syntax checking, the IDE will alert you if you have typed a command incorrectly. The term code assist refers to the ability of the IDE to suggest completions to a piece of code that you are working on. For example, if a locally scoped variable is named maximumLikelihoodEstimator and you wish to use this variable in an expression that you are typing, then you can invoke the code assist function to obtain a list of identifiers to choose from. As you type in characters, the list is narrowed to match only those identifiers that match with the characters you have so far entered. Re-factoring functionality allows you to change the name of an identifier in one part of the code, and have the IDE change the name as it occurs throughout the entire code base. These and other time saving functions make IDEs highly useful tools for developers.
Ant is the Java version of the make utility that is commonly used to build programs written in C or C++. Without an IDE such as Eclipse, a Java programmer would normally write ant build scripts to perform repetitive tasks, such as compiling and packaging compiled code into archives (jar files). With an IDE such as Eclipse, programmers can invoke commonly used operations such as building through the GUI of the IDE. However, there will always be a need to develop scripts in non-trivial projects to automate repetitive tasks. For this reason, this book covers the use of ant for running database regeneration scripts and for generating various objects needed for application security.
Ant is built into Eclipse, so the user can run ant build scripts through the Eclipse menu system. Ant build scripts are in the form of XML files. The default name of a build file is build.xml. The build file contains one or more targets, each of which perform some job. Each target is comprised of any number of tasks, which comprise the atomic actions that a user can specify.
Ant is a free, open source project maintained by the Apache Software Foundation. The ant build system is well documented, and there are numerous tutorials and articles on its use. This book employs ant in a simple way, so that students can become familiar with its use and the syntax of an ant build file.
There are many different ways to design Java-based web applications. This book presents a particular design. In this book, we show how to implement a web application that follows what is popularly called the model-view-controller (MVC) architecture. The MVC approach attempts to separate code into three areas of concern: persistence and business logic (model), user interface generation (view), and user input (controller). In java applications, MVC is usually implemented by generating HTML from Java Server Page (JSP) scripts, handling incoming requests with servlets. Persistence of data is done through data access objects (DAOs) or enterprise java beans (EJBs), and business logic is placed inside servlets, EJBs or other classes. In this book, we do not look at EJBs; instead, we use DAOs to provide persistence services, and place business logic within servlets and other classes.
Data access objects (DAOs) are used to provide persistence functionality for objects that can not be stored in main memory indefinitely. For example, the application may have a class called User that is used to represent users of the system. The User object will contain username, password, email and other attributes needed by the application. This information obviously should survive the restart of the system, and so must be preserved in the non-volatile memory such as a hard disk drive. Objects that are instances of such classes as User are termed persistent objects. Although the state of persistent objects can be stored directly in the file system, they are usually stored in a database.
Some databases are object-oriented, which makes storing persistent objects in them straightforward. However, object-oriented databases have not gained wide acceptance; instead, developers use relational databases in which data is stored in tables of rows and columns. The transference of data between objects in memory and tables in the database is not a straightforward process. Consequently, a lot of code needs to be written to implement this functionality. In fact, there are frameworks (such as Hibernate or iBatis) that can be used to simplify the addition of this capability to an application. Use of these frameworks is not covered in this book. Instead, we will develop our own persistence mechanism by placing such code within our data access objects.
There is an alternative to using persistent objects, which is to operate directly on the data within the database without encapsulating it within objects. This approach removes a lot of complexity from the code for small projects, however, larger projects benefit from the use of persistent objects because it simplifies the expression of the application's business logic. This book focuses on an approach to web application design that is based on persistent objects.
In the Java Web development world, there are several ways to implement security features in an application. This book uses a simple approach to authentication and authorization. A unified process of validating user input is also presented.
Java Web development is usually done using Servlets and JSP. Both Servlets and JSP are standard APIs that were developed through the Java Community Process, which is a mechanism by which the Java language is subject to review and modification by the community. As a result of being developed as standards, there are many competing implementations of what are called Servlet/JSP containers. Some implementations are proprietary and licensed for a fee, such as IBM WebSphere and Oracle Application Server. Others are free, open source, such as Tomcat and Resin. In this book, we use the Tomcat web container. The code that is developed in this book, will generally run unmodified in any Web container. However, certain details related to deploying the application in a particular web container are not subject to the standard, and thus vary between implementations.
A database server is an essential tool to Web developers, no matter which language, framework or operating system they use. Typically, web developers use relational database servers, although alternatives exist, such as object-oriented database servers. Currently, the most widely used database server in industry is probably Oracle. However, there are some good quality alternatives to Oracle, such as MySQL and PostgreSQL. In this book, we use the MySQL database server, because it is the most widely used free, open source database server.
One important concept that students are not usually aware of is the distinction between a development environment and a production environment. The book describes how to set up and use a development environment. Specifically, this book details the steps needed to set up a Windows machine to be used for development of Web applications. However, the book does not cover the set up of an environment in which to run Web applications to be used in a business context.
Although it is possible to use Windows as the operating system in a production environment, the Linux operating system is perhaps a more common platform for production servers. Details of installation and configuration of a production environment for Java web applications is not covered. Also not covered are methods used to distribute load across multiple servers, which is a common neccessity in production environments.