The new version of J2EE1.4 provides the foundation for developing Web Services in J2EE, makes some important enhancements to development tools, and provides new standards in Application Deployment and server management, enhanced integration and security, enhancing J2EE's ability to develop Web applications. Important changes in programming models include JSP Expression Language and simplified tag library; the new timer service is provided in EJB 2.1, and the query language QL is also enhanced. Jdbc3.0 API combines the common Jdbc API with the extended API; j2EE ors specifications and message-independent ejbs provide two-way communication support. The following describes the new features of JSP2.0, Servlet2.4, Jdbc3.0, and EJB2.1 contained in J2EE1.4.
JSP 2.0 is a J2EE 1.4 platform. It adds new functions Based on JSP 1.2. It ensures downward compatibility. the previously used JSP technology is supported in JSP 2.0. The new functions of JSP 2.0 mainly include the following parts:
1) runtime environment changes
1. web. xml format changes
We know that JSP 1.2 can be run in Java 2 standard version 1.3, while JSP 2.0 requires Java 2 standard version 1.4 or an updated version. JSP 2.0 uses the Web program deployment description format specified by Servlet 2.4.
In the Web application description file web. xml, you must use the xml schema Header Format. The main change in web. xml is that all JSP settings should be placed in the <jsp-config> tag. The following example shows a general web. xml.
Example 1:
- <?xml version="1.0" encoding="IS0-8859-1"?>
- <web-app xmlns=http://java.sun.com/xml/ns/j2ee
- xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
- xsi:schemaLocation=http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd
- version="2.4">
- .......
- <jsp-config>
- <taglib>
- <taglib-uri>
- http://www.icconcept.com/ics/sample-taglib
- </taglib-uri>
- <taglib-location>
- /WEB-INF/jsp/sample-taglib.tld
- </taglib-location>
- </taglib>
- ........
- <jsp-property-group>
- <description>
- For config the ICW sample application
- </description>
- <display-name>JSPConfiguration</display-name>
- <url-pattern>/jsp/datareset.jsp</url-pattern>
- <el-ignored>true</el-ignored>
- <page-encoding>ISO-8859-1</page-encoding>
- <scripting-invalid>true</scripting-invalid>
- <include-prelude>/jsp/prelude.jspf</include-prelude>
- <include-coda>/jsp/coda.jspf</include-coda>
- </jsp-property-group>
- </jsp-config>
- </web-app>
2. JSP settings
The <jsp-config> TAG provides settings for JSP programs in Web programs. <Jsp-config> includes <taglib> and <jsp-property-group>. <Taglib> defines the M tag used by the Web program. Its usage is the same as that in JSP 1.2. <Jsp-property-group> defines the characteristics of a set of JSP. These features actually correspond to the features defined by JSP page ctictive. Through <jsp-property-group>, you can easily define multiple JSPs with the same attributes.
<Jsp-property-group> defines one or more URL styles. The attributes defined in <jsp-property-group> apply to all JSP files that match these URL styles. In <jsp-property-group>, you can define the following settings:
(1) allow or forbid the use of Expression Language (EL)
In <jsp-property-group>, you can set whether JSTL Expression Language (EL) can be used for JSP corresponding to <url-pattern> ). If the <el-ignored> attribute tag is set to false, the EL expression in JSP is processed. If it is true, the Web Container ignores the EL expression when converting JSP.
(2) allow or prohibit the use of scripting
The <scripting-invalid> attribute allows or disables the use of JSP scripting ). If this attribute tag corresponds to true, that is, the scripting element is forbidden, the scriptlet, scripting expression, and declaration cannot be used in JSP; otherwise, a conversion error occurs. When this attribute is marked as false, JSP can use the script language as before version 1.2.
(3) Declare JSP Encoding
You can use the <page-encoding> flag to set the encoding of the JSP page corresponding to <url-pattern>. This attribute corresponds to the pageEncoding attribute in each JSP. The Web Container will encode the JSP content based on this attribute.
(4) Corresponding implied include (Implicit Includes des)
In <jsp-property-group>, you can add preludes and coda to the corresponding JSP ), you can use the <include-prelude> and <include-coda> attributes to set the preludes and coda jspf files included in the JSP webpage. The location of these files is relative to the context of the current Web program. When more than one preludes or coda element is in <jsp-property-group>, JSP is added to the content in order.
2) Introduce Expression Language (EL)
One of the main features of JSP 2.0 is that it supports expression language ). The JSTL expression language can use the tag format to easily access JSP's hidden objects and JavaBeans components. The core tag of JSTL provides the process and loop control functions. Self-made tags also have the function of user-defined functions, so basically all functions that seriptlet can implement can be replaced by JSP. In JSP 2.0, we recommend that you use EL whenever possible to make the JSP format more consistent.
In <jsp-property-group> of web. xml, you can control whether a group of JSPs use EL. In each JSP, you can also specify whether the JSP uses EL. The isELIgnored attribute in page ctictive is used to specify whether to ignore the attribute. Format:
- <%@ page isELIgnored="true|false"%>
If it is set to true, the expressions in JSP are treated as strings. For example, the following expression <p >$ {2000% 20} </p> outputs $ {2000% 20} When isELIgnored = "true }, when isELIgnored = "false", the output is 100. The default isELIgnored = "false" for Web containers ".
Although JSP 2.0 can completely use the expression language in JSP to avoid scriptlet, in actual programming, you should select the appropriate method according to the functional requirements of the program and the programming personnel's own conditions. JSP with the expression language is more convenient and regular, but it is slow to be called for the first time because tags need to be converted. Some programmers have a better understanding of Java, therefore, we are more accustomed to the programming method before JSP 1.2. Therefore, we should select appropriate programming methods in use.
3) SimpleTag
JSP 2.0 adds a new API for creating self-made tags. javax. servlet. jsp. tagext. SimpleTag defines an interface for implementing simple tags. Unlike existing interfaces in JSP 1.2, SimpleTag provides a simple doTag () method instead of using the doStartTag () and doEndTag () methods. This method is used only once when the tag is called. All the logical processes, loops, and evaluation of the TAG body that need to be implemented in a self-made tag are implemented in this method. In this respect, SimpleTag and IterationTag can play the same role. However, SimpleTag's method and processing cycle are much easier. In SimpleTag, The seUspBody () and getJspBody () methods are used to set JSP content. The Web Container uses the setJspBody () method to define a JspFragment object that represents the JSP content. The program implementing SimpleTag can call the getJspBody (). invoke () method multiple times in the doTag method as needed to process JSP content.
For example, program example 2 SimpleTag loops based on the specified times and outputs the current sequence number (sequence ). The program structure is relatively simple, and all logic is implemented in the doTag method.
Example 2:
- packageICW.taglib;
- importjavax.servlet.jsp.JspException;
- importjavax.servlet.jsp.tagext.SimpleTagSupport;
- importjava.util.HashMap;
- importjava.io.IOException;
- public class IterationSimpleTag extends SimpleTagSupport{
- privateint times;
- blic void setTimes(int_times){
- this.times=_times;
- }
- public void doTag() throws JspException,IOException{
- HashMapparams=new HashMap();
- for(inti=0; i<times;i++){
- params.put("sequence",String.valueOf(i+1));
- getJspBody().invoke(null,params);
- }
- }
- }
The content of the tag TLD file is as follows. It uses the XML schcma to define the tag usage.
Example 3 is as follows:
- <?xml version="1.0" encoding="UTF-8" ?>
- <taglibxmlns=http://java.sun.com/xml/ns/i2ee
- xmlns:xsi=http://WWW.w3.org/2001/XMLSchema-instance
- xsl:schemaLocation=http://java.sun.com/xml/ns/j2ee web-jsptaglihrary_2_0.xsd
- version="2.0">
- aglib>
- <tiib-version>1.0</tlib-version>
- <short-name>Jwad book simple tag</short-name>
- <uri>/JwadSimpleTag</uri>
- <description>Simple Tag Handler</description>
- <tag>
- <name>iteration</name>
- <tag-class>ICW.taglib.IterationSimpleTag</tag-class>
- <body-content>scriptless</body-content>
- <description>Iteration Tag</description>
- <variable>
- <description>Current iterationnumber</description>
- <name-given>sequence</name—given>
- </variable>
- <attribute>
- <name>times</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- </tag>
- /taglib>
JSP in Example 4 uses the IterationSimpleTag defined in Example 3 above, which loops a certain number of times based on the value of the "times" specified in the Web request parameters. The value of "sequence" is output in each loop.
Example 4:
- <%@ taglib prefix="ictag" uri="/WEB-INF/ics-jsp2.tld" %>
- <HTML><HEAD><TITLE>Simple Tag Sample</TITLE></HEAD>
- <BODY>
- <CENTER>
- <FONT COLOR='#009999' SIZE='4' face='Arial'>
- <STRONG>Interation Simple Tag</STRONG>
- </FONT>
- </CENTER>
- <HR>
- <c:setvar="time" value="${param.times}"/>
- <p><B>Reminder:</B></p><br>
- <ictag:iteration times="${times}">
- This is the ${sequence} Of ${times} times of reminder<br>
- </ictag:iteration>
- </body>
- </html>
4) use JSP fragment
One of the main functions of JSP2.0 is JSP fragment. Its basic feature is that it can delay the JSP tag attribute Evaluation for JSP containers. We know that JSP first evaluates the attributes of JSP tags and then uses these attributes when processing JSP tags. JSP fragment provides dynamic attributes. That is to say, these attributes can be changed when JSP processes the TAG body. JSP needs to define this property as the javax. servlet. jsp. tagext. JspFragment type. When the JSP tag is set to this form, the actual processing of this tag attribute is similar to the TAG body. In programs that implement tag, tag attributes can be evaluated multiple times. This method is called JSP fragment. JSP fragment can also define self-made tag actions used in a SimpleTag handler. As described in the preceding example, getJspBody returns a JspFragment object and can be used multiple times in the doTag method. Note that JSP fragment can only have common text and JSP actions, but cannot have scriptlet or scriptlet expressions.
We can simply think that JSP fragment is a piece of JSP that can be reused. A piece of JSP fragment can be passed to another JSP and used. Unlike JSP include, JSP fragment generally features short and simple, and has a high reuse rate.
JSP fragment is generally defined in the <jsp: attribute> tag body or <jspcbody> tag body. Each time a tag containing JSP fragment is used, the Web Container generates a JspFragment object, which is associated with the current JSP page scope. At the same time, this JspFragment object establishes a connection with its parent tag. The JspFragment object can be called in two ways: a tag handler written in Java or a tag file ). You can use <jsp: invoke> or <jsp: doBody> to mark a file. JSP tag files can work with JSP fragment. CustomTag is implemented programmatically. JSP tag files are self-made tags using the text file format (JSP syntax), which is also a major new feature of JSP 2.0. A Tag file can correspond to a tag. You do not need a tld file to define the TAG method. In this way, even if programmers are not familiar with Java, they can use JSP syntax to define their own tags. A tag file typically uses. tag as a suffix and is placed under the/WEB-INF directory of the Web program.
In Example 5, taskstatus. jsp uses two JSP fragment. The JSP function is to display the name and completion date of a group of tasks. It uses <jsp: attribute name = "... "> two JSPfragment segments (onSehedule and delayed) are defined ). The jsp in the <JSP: attribute> tag is JSPfragment, And the <jsp: attribute> tag is surrounded by a <ietag: listTasks> tag. This tag is a self-made tag defined by the Tag file, and its definition file is under the/WEB-INF/tags directory. The name of the tag file is consistent with the tag name "listTasks. tag ". This tag uses the two JSP fragment defined above.
Example 5:
- <%@ taglib prefix="ictag" tagdir="/WEB-INF/tags" %>
- <HTML><HEAD><TITLE>JSP Fragment Sample</TITLE></HEAD>
- <BODY>
- <CENTER>
- <FONT COLOR='#009999' SIZE='4' face='Arial'>
- <STRONG>JSP Fragment Sample Using Tag Files</STRONG>
- </FONT>
- </CENTER>
- <HR>
- <h2>Tasks</h2>
- <ietag:listTasks>
- <jsp:attribute name="onSchedule">
- <td>
- Name:${name}<br/>
- </td><td>
- Date:${date}
- </td>
- </jsp:attribute>
- <jsp:attribute name="delayed">
- <td>Name:${name}<br/>
- </td><td>
- <font color="red">Plan:<strike> ${pianDate}</strike></font><br/>
- <b>Actural:${actDate}</b>
- </td>
- </jsp:attribute>
- </ictag:listTasks>
- </BODY>
- </HTML>
5) Other features
JSP2.0 also has some other feature changes, such as strictly correcting the I18N syntax rules, improving the XML syntax of JSP to allow namespaces. These are not core functions. You can refer to java.sun.com to learn more about the functions.
Related Articles]
- Use EL Expression Language in JSP2.0
- Briefly describe the three features of the JSP2 Mode
- JSP2.0 features of basic JSP tutorial knowledge
- At the beginning of JSP Servlet Development
- JSP programming skills: Database Utilization