|
If you have had the opportunity to build web applications using technologies such as Common Gateway Interface (CGI) and servlets, you are accustomed to the idea of writing a program to generate the whole page (the static and the dynamic part) using that same program. If you are looking for a solution in which you can separate the two parts, look no further. JavaServer Pages (JSP) are here. JSP pages allow you to separate front-end presentation from business logic (middle and back-end tiers). It is a great Rapid Application Development (RAD) approach to Web applications. This series of articles provides a hands-on tutorial explaining how to develop modern Web applications for today's and tomorrow's market. This series begins with this article, which explains the concepts and benefits of JSP technology, and then shows you how to utilize this exciting technology, and how to create reusable components for handling forms. The Dynamic WebThe Web has evolved from a network-based hypermedia distributed information system offering static information to a marketplace for selling and buying goods and services. The increasingly sophisticated applications to enable this marketplace require a technology for presenting dynamic information. First generation solutions included CGI, which is a mechanism for running external programs through a web server. The problem with CGI scripts is scalability; a new process is created for every request. Second generation solutions included web server vendors providing plug-ins and APIs for their servers. The problem is that their solutions were specific to their server products. For example, Microsoft provided Active Server Pages (ASP) that made it easier to create dynamic content. However, their solution only worked with Microsoft IIS or Personal Web Server. Therefore, if you wanted to use ASP you had to commit yourself to Microsoft products and you would not be enjoying the freedom of selecting your favorite web server and operating system! Another second generation technology that is quite popular in enterprise computing is servlets. Servlets make it easier to write server-side applications using Java technology. The problem with either CGI or servlets, however, is that you have to follow the write, compile, and deploy life cycle. JSP pages are a third generation solution that can be combined easily with some second generation solutions, creating dynamic content, and making it easier and faster to build web-based applications that work with a variety of other technologies: web servers, web browsers, application servers and other development tools. JavaServer Pages (JSP)The JSP technology is an open, freely available specification developed by Sun Microsystems as an alternative to Microsoft's Active Server Pages (ASP) technology, and a key component of the Java 2 Enterprise Edition (J2EE) specification. Many of the commercially available application servers (such as BEA WebLogic, IBM WebSphere, Live JRun, Orion, and so on) already support JSP technology. JSP versus ASPJSP and ASP deliver similar functionality. They both use tags to allow embedded code in an HTML page, session tracking, and database connection. Some of the trivial differences are:
Beyond these trivial differences, there are a number of important differences that may help you in choosing a technology for your organization:
For a more detailed comparison between JSP pages and ASP pages, see Comparing JSP and ASP. Software EnvironmentTo run JSP pages, you need a web server with a web container that conforms to JSP and servlet specifications. The web container executes on the web server and manages the execution of all JSP pages and servlets running on that web server. Tomcat 3.2.1 is a complete reference implementation for the Java Servlet 2.2 and JSP 1.1 specifications. Download and install binary versions of Tomcat. To configure Tomcat:
How JSP Pages WorkA JSP page is basically a web page with traditional HTML and bits of Java code. The file extension of a JSP page is ".jsp" rather than ".html" or ".htm", and that tells the server that this page requires special handling that will be accomplished by a server extension or a plug-in. Here is a simple example: Sample 1: date.jsp
This example contains traditional HTML and some Java code. The tag
![]() Figure 1: Requesting date.jsp Behind the Scenes
When this page
![]() Figure 2: Request/Response Flow when Calling a JSP The next time the page is requested, the JSP engine executes the already-loaded servlet unless the JSP page has changed, in which case it is automatically recompiled into a servlet and executed. Scripting Elements
In the Sample 2 :date2.jsp
Yet, another way of doing the same thing using the Sample 3:date3.jsp
As you can see, the same thing can be accomplished using different tags and techniques. There are several JSP scripting elements. Here are some conventional rules that will help you use JSP scripting elements effectively:
Handling Forms
One of the most common parts of ecommerce applications is an HTML form where
the user enters some information such as name and address. Using JSP, the
form's data (the information the user enters in the form) gets stored in the
To demonstrate how to handle HTML forms using JSP, here is an example form
with two fields: one for name and the other for email. As you can see, the
HTML form is defined in a JSP source file. The
The Sample 4: process.jsp
If ![]() Figure 3: process.jsp loaded Enter your name and email and click on Process to submit the form for processing, and you see something similar to Figure 4. ![]() Figure 4: Form is processed Reusable ComponentsThe above example form is simple in the sense that there is not much code involved. When more code is involved, then it is important not to mix business logic with front end presentation in the same file. Separating business logic from presentation permits changes to either side without affecting the other. However, production JSP code should be limited to front end presentation. So, how do you implement the business logic part? That is where JavaBeans come in to play. This technology is a portable, platform-independent component model that lets developers write components and reuse them everywhere. In the context of JSP, JavaBeans contain business logic that returns data to a script on a JSP page, which in turn formats the data returned from the JavaBean component for display by the browser. A JSP page uses a JavaBean component by setting and getting the properties that it provides. What are the BenefitsThere are several benefits to using JavaBeans to augment JSP pages:
Example: Using JavaBeans with JSP
Now, let's see how to modify the Good components must be able to interoperate with other components from different vendors. Therefore, to achieve component reuse, there are two important rules (which are imposed by the JavaBeans architecture) to follow:
Sample 5: FormBean.java
In order to use the
Once the properties have been initialized with data retrieved from the form,
property values are retrieved for presentation using
Sample 6: process2.jsp
ConclusionDevelopers interested in developing quality production web applications should familiarize themselves with technologies that are applicable not only for today's market but tomorrow's as well, namely JSP and XML. The next article will discuss the capabilities that the JSP technology provides that are ideally suited for working with XML; and show you how to effectively use JSP with XML. JSP and XML make an excellent combination for web applications that share information, because JSP pages have XML support built right into them in the form of JSP custom tag libraries. Stay tuned for more information on this in the next article in this series.
For more information
JSP
About the AuthorQusay H. Mahmoud provides Java consulting and training services. Qusay has published dozens of articles on Java, and is the author of Distributed Programming with Java (Manning Publications, 1999). | |||||||||||||||||
Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.
|
| ||||||||||||