Develop web applications using JSP 2.0
Author: Qusay H. Mahmoud July 2003
JSP (JavaServer Pages) technology is a further abstraction of Servlet, which is developed by Java Community Process, which is an open-ended, free acquisition specification, which is also a J2EE (Java 2 Enterprise Edition). An important part of. Many commercial application servers are supported by BEA WebLogic, IBM WebSphere, Live Jrun, and Orion support JSP technology.
From the ticket reservation system, banking system to the shopping system, the Web is applied to the application JSP technology. The new release version 2.0 is an upgrade to JSP1.2, which has added some interesting new features. The goal of JSP2 is to make dynamic web pages easier and do not need to learn Java programming languages.
This article includes:
Provides a tutorial with rich sample code to make you familiar with JSP2.0's new feature of JSP2.0 provides some sample code to use JSP2.0 development applications, and you can use this code to be slightly modified for you. s application.
If you are not familiar with JSP, you are a good idea from JSP2.0. But if you want to learn JSP1.2, maybe you need to learn from the JSP tutorial.
JSP 2.0
JSP 2.0 is an upgrade to JSP 1.2, increasing a few interesting new features that make it easier for web application designers and developers. The goal of JSP 2.0 is more easier to use than before, but more importantly, it is necessary to use it without learning the Java language itself. It adds a new extension mechanism called SimpleTAG to simplify the label API (TAG API).
In addition to some other improvements, the most important new features introduced by JSP 2.0 include:
A simple expression language (EL) can be used to easily access data from the JSP page, which simplifies the writing of JSP-free-free applications, do not need Java Scriptlet or Java expression For direct use of JSP technology to define reusable custom behaviors, this syntax uses .tag and .tagx files, such files can be written by developers or web authors; 3XML syntax has been substantially improved, increased A new standard file extension (.tagx is used for label files, .jspx is used for JSP files).
This article mainly discusses expression language, simplified label API and label files. I believe that the current JSP developers will find that these important features are not only interesting, but also very useful.
Why is it from 1.2 to 2.0?
The version number in the Java Specification (JSR 152) is initially set to 1.3. But as you will see later, since these new features have such a profound impact on the development model of JSP applications, the expert group is necessary to upgrade the primary version number to 2.0, so that this effect can be fully reflected. In addition, the new version number also helps to attract the attention of developers to these interesting new features. It is gratifying that all legitimate JSP1.2 pages is also a legal JSP2.0 page.
JSP 2.0 starts
Before studying JSP 2.0, you need a JSP environment that supports JSP2.0 and Java Servlet 2.4 specification. Fortunately, Jakarta Tomcat 5.0 (alpha beta) supports new JSP 2.0 and servlet 2.4 specification, you can download from http://jakarta.apache.org/builds/jakarta-tomcat/release/v5.0.4-alpha/ And install Tomcat 5.0. JSP expression language
Passing information to JSP web pages is done through the JSP Scope Dature and the request parameters. An expression language (EL) specifically tailored to the web page, enhances the JSP Scope attribute to deliver information from the business logic to the JSP page. However, it is important to note that although this expression language is an important feature of JSP, it is not a general program language. It is just a data access language that can easily access and process application data without using the value of the Scriptlet or Request-Time.
Before JSP2.0, the web page can only use the expression <% = aname%> to access the system, such as the example below:
Or use the value of custom JavaBeans components:
<% = acuStomer.getaddress (). getCountry ()%>
Expression Language allows web authors to use simple syntax access objects. For example, to access a simple variable, you can write like this:
And access the nested JavaBeans property, you can write like this:
$ {acuStomer.address.country}
You may ask, is this not a syntax of JavaScript? Completely correct! If you have used JavaScript, it will be very easy because the expression language is borrowing JavaScript syntax that accesses structured data.
Note: The expression language is initially developed as part of the JSP standard tag library (JSTL) 1.0. JSTL is a standard tag library that provides support for universal, structured tasks, such as iterations, and conditions, XML documents Treatment, internationalization, and use SQL access databases. The JSTL specification is developed by the JSR 52 expert group. For an introduction to JSTL, see Faster Development with JSTL (using JSTL acceleration development).
Access application data
You can use a dot marker (.) To access application data for object properties, you can also use the square bracket operator ['Name'] to access named array elements.
Expression $ {data} represents a scope variable named DATA. You can use the order number (.) Or square bracket ([]) operator to retrieve the attribute value from the collection:
Point number operator is used to access named properties, such as expression $ {Customer.name} represents the name attribute of the scotum Customer; square bracket operator can be used to retrieve naming properties, such as $ {Customer ["name"]} in. You can also access the first item in the collection Customers through the form of $ {CUSTOMERS [0]}.
The expression language is unified to process the point and square bracket operator, so $ {Customer.Name} is equivalent to $ {Customer ["name"]} is equivalent. As you can see, the expression must be surrounded by $ {and}. The identifier of the EL is to use PageContext.FindAttribute (String) to look for its value as an attribute as an attribute. Returns NULL if this property is not found.
Operator
Expression language supports arithmetic operators, relational operators, and logical operators to complete most of the data processing operations. In addition, it also provides a special operator for testing if an object is empty. The operator is shown in Table 1. You can use the EMPTY operator to determine if a collection or string is empty. For example, only when the required naming parameter param is not given, the expression is {Empty param.name} returns true. The EMPTY operator can be used with the "!" Operator, such as $ {! Empty param.name} Returns when the required named parameter param exists.
Table 1: Expression Language Operator Operator Description Plus - Reduce * Multiply / or DIV De Inns Defolio or MOD Module (Removal) == or = Or equals! = Or! = Not equal to
Include object
In addition to the operator, the expression language also defines some implied objects to support web page authors to access the needed application data. The implicit object defined by the expression language is shown in Table 2, and an example of using these implicit objects will be given later.
Table 2: Implied objects in the expression language implicit object content ApplicationsCope application SCOPED variable consisting of all cookies All cookies Compose Header HTTP Request Headquarters, String HeaderValues HTTP Request Headquarters, String Collection InitParam All application parameter names set PageContext javax.servlet.jsp.PageContext Objects All Objects All Objects All Objects All Request parameters All request parameters All as a string collection RequestScope All request scope Collection of objects for the SESSIONSCOPE of all session ranges
Expression language example
As you say, the web page does not need to learn Java and can also use this expression language. Sample Code 1 shows some examples of expressions and the use of implicit objects.
Code Example 1: EX1.JSP
To run this example, please refer to the following steps, here we assume that Tomcat 5.0 is installed under C: /Tomcat5.0.
Switching directory to C: /TOMCAT5.0/WebApps/jsp-examples Create a new directory, name can be arbitrary, such as JSP2-Tutorial switches to JSP2-Tutorial to copy the ex1.jsp and save to the Tomcat 5 server: Point to "Start" -> Programs -> "Apache Tomcat 5.0", click "Apache Tomcat 5.0" to start Tomcat Enter http: // localhost: 8080 / jsp-examples / JSP2-Tutorial / EX1. JSP
You will see the screen similar to Figure 1. It is as simple as using an expression language!
Figure 1: JSP expression language and implicit object
Note: In this article, all JSP files are saved in the c: /tomcat5.0/webapps/jsp-examples/jsp2-tutorial directory. Fill table single case
Include object $ paran [var] can be used to read the data of the populated form. Code Example 2 gives a simple form that prompts the user to enter a name.
Code Example 2: Form.jsp