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:
Provide tutorials with rich sample code to make you familiar with JSP2.0
Introduce the new features of JSP2.0
Provide some experiences with JSP2.0 development applications
Some sample code is given, and these codes can be used for your own 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:
1. A simple expression language (EL) that can easily access data from JSP pages, which simplifies the writing of JSP-free-free applications, no need to use Java scriptlet or Java expression;
2. Used to directly use JSP technology to define new grammar for reusable custom behavior, using .tag and .tagx files, such files can be written by developers or web authors;
3. 3XML syntax has been substantially improved, adding 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 expressing $ {Customer.name} represents the name attribute of the Scoped Variable Customer;
Square bracket operators can be used to retrieve naming properties, such as in $ {Customer ["Name"]}. 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
-
Less
*
Multiply
/ Or DIV
except
% Or MOD
Module (surplus)
== or =
equal
! = Or! =
not equal to
Be less than > Or GT more than the <= Or le Less than or equal > = Or GE greater or equal to && or and Logic || OR OR Logic or ! Or not Logical EMPTY Check if it is empty a? b: c Conditional operator 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 Include object content ApplicationsCope Collection of scoped variables within the application range cookie Collection of all cookies HEADER HTTP request head, string HEADERVALUES HTTP request head, string collection Initparam Collection of all application parameter names PageContext Current page javax.servlet.jsp.pagecontext object PageScope Collection of all objects within the page PARAM Collection of all request parameter strings Paramvalues All request parameters as a string collection Requestscope Collection of objects for all requests Sessionscope Collection of all sessions 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. 1. Switch the catalog to C: /tomcat5.0/webapps/jsp-examples 2. Create a new directory, the name can be arbitrary, such as JSP2-Tutorial 3. Switch to JSP2-TUTORALIAL 4. Copy the ex1.jsp and save it to this directory. 5. Start Tomcat 5 Server: Point to "Start" -> Programs -> "Apache Tomcat 5.0", click "Apache Tomcat 5.0" to start Tomcat 6. Enter http: // localhost: 8080 / jsp-example.jsplers / JSP2-Tutorial / 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
HEAD>