By Qusay H. Mahmoudjuly 2003
JavaServer Pages (JSP) technology, which abstracts servlets to a higher level, is an open, freely-available specification developed by the Java Community Process (JCP) for generating dynamic content, and a key component of the Java 2 Enterprise Edition (J2EE) Spectation. Many Commercially Available Application Servers (Such As BEA WebLogic, IBM WebSphere, Live Jrun, and ORION) Support JSP Technology.
JSP technology is being used everywhere on the Web including airline reservation systems, banking systems, and shopping. The new release, Version 2.0, is an upgrade to JSP 1.2 with several interesting new features. The objective of JSP 2.0 is to make the task of Dynamic Web Pages Easier Than Ever WITHOUT HAVING to Learn The Java Programming Language.
This Article:
Provides a fast track code intensive tutorial to get started with JSP 2.0 Describes the new features in JSP 2.0 Offers a flavor of the effort involved in developing applications using JSP 2.0 Provides sample code that you can adapt for your own applications
If you are new to jsp, IT Might Be a good idea to start directly with jsp 2.0. However, if you wish to Learn About JSP 1.2 You May Want To Start With this JSP Tutorial.
JSP 2.0
JSP 2.0 is an upgrade to JSP 1.2 with several new interesting features that make the lives of Web application designers and developers easier. The objective of JSP 2.0 is to make JSP easier to use than ever, and more importantly to be used without having to learn ........................
In Addition To Several Other Improvements, The New Key Features That Have Been Introduces IN JSP 2.0 Are:
A simple expression language (EL), which can used to easily access data from JSP pages. The expression language simplifies writing scriptless JSP-based applications without using Java scriptlets or Java expressions. New syntax for defining reusable custom actions using JSP technology directly. The syntax is delivered into .tag and .tagx files which can be written by developers and page authors. The XML syntax has been improved substantially. The new standard filename extensions (.tagx for tag files and .jspx for JSP files) have been added. In this Article I Concentrate On The Expression Language, The Simplified Tag API, AND TAG FILES. I Believe Existing JSP Developers Will Find these Key Features To Be Very Interesting and useful.
Why the jump from 1.2 to 2.0?
The version number was originally listed as 1.3 in the Java Specification Request (JSR 152). However, given that the new features would have a deep impact on the development model of JSP applications as you will see later, the Expert Group felt it was necessary to upgrade the major version number to 2.0 since this would reflect the impact more appropriately. Also, the new version number will help draw the attention of developers to these new interesting features. The good news is that all valid JSP 1.2 pages are also valid JSP 2.0 pages.
Getting Started with JSP 2.0
In order to get started with JSP 2.0, you need a JSP container that supports the JSP 2.0 specification and the Servlet 2.4 specification Java. Luckily, Jakarta Tomcat 5.0 (Alpha release) supports the new JSP 2.0 and Servlet 2.4 specifications. Download and install Tomcat 5.0.
The JSP Expression Language
Information to be passed to JSP pages is communicated using JSP scoped attributes and request parameters. An expression language (EL), which is designed specifically for page authors, promotes JSP scoped attributes as the standard way to communicate information from business logic to JSP pages. note, however, that while the EL is a key aspect of the JSP, it is not a general purpose programming language. Rather, it is simply a data access language, which makes it possible to easily access (and manipulate) application data without having To use scripTlets or request-time expression value.prior to JSP 2.0, a page author had to use the expression <% = aname%> To access the value of a system, as in the folowing example:
Or The Value of a Custom JavaBeans Component:
<% = acuStomer.getaddress (). getCountry ()%>
An Expression Language Allows A Page Author To Access An Object Using A Simplified Syntax. For Example, To Access A Simple Variable, You CAN Use Something Like:
And to Access A Nested JavaBeans Property, You Would Use Something Like:
$ {acuStomer.address.country}
But, you might ask, is not this JavaScript syntax? You are absolutely right! If you've worked with JavaScript, you will feel right at home, because the EL borrows the JavaScript syntax for accessing structured data.
Note: The Expression Language was originally developed as part of the JavaServer Pages Standard Tag Library (JSTL) 1.0, which is a standard tag library that provides support for common, structural tasks, such as iteration and conditionals, processing XML documents, internationalization, and Database Access Using The Structured Query Language (SQL). The JSTL Specification IS Being Developed by The JSR 52 Expert Group. For a Tutorial ON JSTL, PLEASE Seeter development with jstl.
Accessing Application Data
You CAN Access Application Data Either As a Property of An Object, Using The Dot (.) Operator, or a named Array Element Using The Bracket ['Name'] Operator.
The expression $ {data} represents the scoped variable name from collections using each dot (.) Or bracket ([]) Operator:
The dot (.) Operator is used to retrieve a named property. For example, the expression $ {customer.name} indicates the name property of the customer scoped variable. The bracket operator ([]) can be used to retrieve a named property AS IN $ {Customer ["name"]}. The Bracket Operator Can Also Be Used AS $ {Customers [0]} TO REFER TO The First Item In The Customers Collection.
THE EXPRESSION LANGUAGE UNIFIES The Treatment of Dot (.) Operators. Therefore, $ {Customer.Name} Is Equivalent to $ {Customer ["Name"]}. As you can see, All el Expressions Must Be ENCLOSED BETWEEN $ {and}.
The el evatates an identifier by looking up its value as an attribute using pagecontext.Findattribute (string). If the attribute is not found, Null is returned.
Operators
The EL supports arithmetic, relational, and logical operators to handle the most common data manipulations. In addition, a special operator for testing if an object is empty is provided. The operators are shown in Table 1. You can use the empty operator to determine whether a collection or a string is empty or null. For example, $ {empty param.name} will be true only if the request parameter named param is not present. The empty operator can be combined with the! operator, as in the expression $ {! empty param.name}, which evaluates to true if the request parameter named param is present.Table 1: Expression language operatorsOperatorDescription Addition-Subtraction * Multiplication / or divDivision% or modModulus (Remainder) == or = Equality =! ! or = Inequality
Implicit Objects
In addition to operators, the EL defines implicit objects to support access to application data that is of interest to page authors. The implicit objects defined by the EL are shown in Table 2. An example of how to use some of these implicit objects is provided Later.
Table 2: Implicit objects provided by the expression languageImplicit ObjectContentapplicationScopeA collection of scoped variables from applications scopecookieA collection of all cookiesheaderHTTP request headers as stringsheaderValuesHTTP request headers as collections of stringsinitParamA collection of all application parameter namespageContextThe javax.servlet.jsp.PageContext object for the current pagepageScopeA collection of all page scope objectsparamA collection of all request parameters as stringsparamValuesAll request parameters as collections of stringsrequestScopeA collection of all request scope objectssessionScopeA collection of all session scope objectsEL Example
As you can Tell, Web Page Authors Can Use The Expression Language WITHOUT HAVING to Learn Java. Code Sample 1 Shows Some El Expressions As Well as The Use of Explicit Objects.
Code Sample 1: EX1.JSP
The following table illustrates some EL expressions and implicit objects:
Expression b> td> | value b> td> thead> / $ {2 5} td> | $ {2 5} td> td> | / $ {4/5} td> | $ {4 / 5} td> tr> | / $ {5 div 6} td> | $ {5 div 6} td> tr> | / $ {5 MOD 7} td> | $ {5 mod 7} td> tr> | / $ {2 <3} td> | $ {2 <3} td> tr> | / $ {2 gt 3} td> | $ {2 gt 3} td> tr> | / $ {3.1 Le 3.2} td> | $ {3.1 Le 3.2} td> tr> | / $ {(5> 3)? 5: 3} Td> | $ {(5> 3)? 5: 3} td> tr> | / $ {header ["host"]} td> | $ {header ["Host"]} td> tr> | / $ {header ["user-agent"]} td> | $ {header ["User-Agent "]} Td> tr> table> body> html> in Order to Run this, do the following. Here I Assute Tomcat 5.0 IS Installed At C: /TOMCAT5.0
| Change directory to c: /Tomcat5.0/webapps/jsp-examples Create a directory and name it whatever you like, let's say jsp2-tutorial Change directory to jsp2-tutorial Copy the ex1.jsp from this article and save it there Start the tomcat 5 server by going to Start-> Programs-> Apache tomcat 5.0-> Start tomcat In your Web browser, enter http: // localhost: 8080 / jsp-examples / jsp2-tutorial / ex1.jspYou should something similar to Figure 1 It is what simple to use the expression language @ ip. Figure 1: JSP Expression Language and Implicit Objects NOTE: IN this article, All JSP Pages Will Be Saved Under C: / Tomcat5.0/webapps/jsp-examples/jsp2-tutorial. Fill-Out-Form Example . Code Sample 2: form.jsp
Fill-out-form h3>
|