In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about what JSP2.0 features are about. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
SUN's new version of J2EE1.4 provides the basis for developing Web Service in J2EE, makes some important enhancements to development tools, provides new standards for application deployment and server management, enhances J2EE's ability to develop Web applications in terms of integration and security, and important changes in programming models include JSP expression language, simplified tag libraries, etc. New timer services are provided in EJB 2.1, and the query language (QL) has been enhanced; Jdbc3.0 API combines the usual Jdbc API with extended API; and the J2EE Connectors specification and EJB independent of message types provide support for two-way communication. This JSP tutorial focuses on the new features of JSP2.0 included in J2EE1.4.
JSP 2.0 belongs to the J2EE 1.4 platform, which adds new features to JSP 1.2. It ensures backward compatibility, and the previously used JSP technology can be supported in JSP 2.0. The new features of JSP 2.0 mainly include the following parts:
one。 Change of operating environment characteristics
1. Web.xml format feature change
We know that JSP 1.2 can run in Java 2 Standard Edition 1.3, while JSP 2.0 requires Java 2 Standard Edition 1.4 or later, and JSP 2.0 uses the Web program deployment description format specified by Servlet 2.4.
The format starting with xml schema is required in the Web program description file web.xml. The main change in web.xml is that all setting information about JSP is placed in the < jsp-config > tag. Example 1 below shows a rough look of a 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 setting characteristics
The < jsp-config > tag provides setting information for the JSP program in the Web program. < jsp-config > includes < taglib > and < jsp-property-group > elements. < taglib > defines the custom tag used by Web programs, which is used in the same way as in previous JSP 1.2. < jsp-property-group > defines a set of features of JSP. These features actually correspond to those defined by JSP's page directive. You can simply uniformly define multiple JSP with the same attributes through < jsp-property-group >.
< jsp-property-group > defines one or more URL styles, and the properties defined in < jsp-property-group > apply to all JSP files that match these URL styles. The properties in < jsp-property-group > can define the following settings:
(1) allow or prohibit the use of expression languages (EL)
In < jsp-property-group >, you can set whether the JSP corresponding to < url-pattern > is allowed to use the JSTL expression language (EL). If the < el-ignored > attribute tag is set to the EL expression in false,JSP, it will be processed; if it is the true,Web container, the EL expression will be ignored when converting JSP.
(2) allow or prohibit the use of scripting
The < scripting-invalid > attribute allows or disables the use of JSP's scripting language (scripting). If this attribute tag corresponds to true, that is, the scripting element is prohibited, scriptlet,scripting expressions and declaration cannot be used in JSP, otherwise there will be a conversion error. When this attribute is marked false, JSP can use the scripting language as it did before version 1.2.
(3) declare JSP encoding
The < page-encoding > tag allows you to set the encoding of the JSP page corresponding to < url-pattern >. This property corresponds to the pageEncoding attribute in each JSP, based on which the Web container encodes the JSP content.
(4) corresponding implied including (Implicit Includes)
In < jsp-property-group >, you can add preludes and coda to the corresponding JSP, and use the < include-prelude > and < include-coda > properties to set the jspf files of preludes and coda included in the JSP page. The location of these files is relative to the context of the current Web program. When there is more than one preludes or coda element in < jsp-property- group >, JSP is added to the content in that order.
two。 Introduction of expression language (EL)
One of the main features of JSP 2.0 is that it supports expression language (expression language). The JSTL expression language can easily access the implicit objects and JavaBeans components of JSP using the tag format, and the core markup of JSTL provides flow and loop control functions. Homemade tags also have the function of custom functions, so basically all the functions that seriptlet can achieve can be replaced by JSP. In JSP 2.0, it is recommended to use EL as much as possible to make the format of JSP more consistent.
You can control whether a group of JSP uses EL in the < jsp-property-group > of web.xml, and you can specify whether the JSP uses EL in each JSP. The isELIgnored property in page directive is used to specify whether to ignore it. The format is:
<% @ page isELIgnored= "true | false"% >
If set to true, the expression in JSP is treated as a string. For example, the following expression < p > ${2000 20} < / p > outputs ${2000 20} when isELIgnored= "true" and 100 when isELIgnored= "false". The Web container defaults to isELIgnored= "false".
Although JSP 2.0 can make full use of expression language in JSP and avoid scriptlet, in actual programming, the appropriate way should be chosen according to the functional requirements of the program and the conditions of the programmer. It is more convenient and orderly to use JSP in the expression language, but it will be slow to be called * * times because of the need to convert tags. Some programmers are more accustomed to the programming method before JSP 1.2because they know more about Java, so they should choose the applicable programming method according to local conditions.
III. SimpleTag
JSP 2.0 adds a new API,javax.servlet.jsp.tagext.SimpleTag to create homemade tags and defines interfaces to implement simple tags. Unlike the existing interfaces in JSP 1.2, the SimpleTag interface does not use the doStartTag () and doEndTag () methods, but provides a simple doTag () method. 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 homemade tag are implemented in this method. In this respect, SimpleTag and IterationTag can achieve the same effect. But the method and processing cycle of SimpleTag are much simpler. There are also seUspBody () and getJspBody () methods for setting JSP content in SimpleTag. The Web container uses the setJspBody () method to define a JspFragment object that represents JSP content. The program that implements the SimpleTag tag can call the getJspBody (). Invoke () method as many times as necessary in the doTag method to process the JSP content.
For example, the program example 2 SimpleTag loops according to the specified number of times (times) and outputs the current sequence number (sequence). The structure of the program is relatively simple, and all the 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 (iTun1)) GetJspBody () .invoke (null,params);}
The contents of the TLD file for this tag are as follows, and it uses XML schcma to define how the tag is used.
Example 3:
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" > < taglib > < tiib-version > 1.0 < / tlib-version < short-name > Jwad book simple tag < / short-name > < uri > / JwadSimpleTag < / uri > < description > SimpleTag 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 >
The JSP in example 4 uses the IterationSimpleTag defined in example 3 above, which loops a certain number of times according to the value of "times" given in the Web request parameter. 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 > < CGV 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 >
four。 Use the JSP fragment feature
One of the main features of JSP 2.0 is JSP fragment, whose basic feature is that it allows containers that process JSP to defer the evaluation of JSP tag attributes. We know that JSP first evaluates the attributes of JSP tags and then uses them when dealing with JSP tags, while JSP fragment provides dynamic attributes. That is, these attributes can be changed when JSP processes its tag body. JSP needs to define such an attribute as a javax.servlet.jsp.tagext.JspFragment type. When the JSP tag is set to this form, this tag attribute is actually handled similar to the tag body. In programs that implement tags, tag attributes can be evaluated repeatedly. This usage is called JSP fragment. JSP fragment can also define homemade tag actions used in a SimpleTag handler. As shown in the previous example, getJspBody returns a JspFragment object and can be used multiple times in the doTag method. It is important to note that JSP using JSP fragment can only have plain text and JSP action, not scriptlet and scriptlet expressions.
We can simply think of JSP fragment as a JSP that can be reused. A piece of JSP fragment can be passed to another JSP and used. Unlike the concept of JSP include, the general function of JSP fragment is relatively short and simple and has a high reuse rate.
JSP fragment is generally defined in the body of the < jsp:attribute > tag or the < jspcbody > tag. Each time a tag containing JSP fragment is used, the Web container generates a JspFragment object that is associated with the page scope of the current JSP. At the same time, the JspFragment object is associated with the parent tag that contains it. The JspFragment object can be called in two ways: a tag handler written in Java, or a tag file (tag file). Tag files can use < jsp:invoke >, or < jsp:doBody > actions can use JSP fragment. JSP tag files can work with JSP fragment. CustomTag is implemented programmatically. JSP tag files implement homemade tags in text file format (JSP syntax), which is also a major new feature of JSP 2.0. A tag file can correspond to a tag, and the method of the tag can be defined without the need for a tld file. In this way, even if programmers are not familiar with Java, they can use JSP syntax to define their own tags. Tag files are typically suffixed with .tag and placed in the / WEB-INF directory of the Web program.
The taskstatus.jsp in example 5 uses two JSP fragment. The function of this JSP is to display the name and finish date of a set of Task, which defines two segments of JSPfragment (named onSehedule and delayed) through < jsp:attribute name= "..." >. The JSP within the < jsp:attribute > tag is JSPfragment, and the < jsp:attribute > tag is surrounded by a < ietag: listTasks >. This tag is a homemade tag defined by the tag file, which is in the / WEB-INF/tags directory. The name of the tag file and the tag name are the same as "listTasks.tag". This tag uses the two JSP fragment defined earlier.
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 > < h3 > Tasks < / h3 > < 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 >
five。 Other JSP2.0 features
JSP2.0 also has some other feature changes, such as strictly correcting the syntax rules of i18N, improving the JSP corresponding XML syntax to allow the use of namespaces, and so on.
Thank you for reading! This is the end of this article on "what are the features of JSP2.0?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.