The labeling technology of JSP

Source: Internet
Author: User
Tags session id tld

JSP label technology: In the JSP page is best not to appear Java code, then we can use the label technology to replace the Java code with a label to represent

1.jsp Tags: Sun native tags are available directly from the JSP page
<jsp:include>--Implement page inclusion, dynamic inclusion
<jsp:forward>-Implement Request forwarding
<jsp:param>-With two tags above, used to stitch some request parameters behind the path when request inclusion and request forwarding
2.EL expression: The original purpose is to replace the JSP script expression in the JSP page. But with El's development El's function is not limited to this.
${el-expression}

(1) Get the data:
A dot substitution can be used where the brackets are used, except that the brackets are numbers or the brackets contain special characters (-.). Except in the case of
In brackets if you do not enclose the double quotation mark is a variable, first find the value of the variable and then use the value of the variable. If you use double quotation marks, the values of the constants are used directly.

~ Get constant
string/Number/Boolean type, directly written in El expression, El Direct output
~ Get variables in a domain
If the El is written in the name of a variable, then El will call PageContext's Findattribute method, in the four scopes in the given name to find the corresponding attribute value, found after the output, if not found in four domains, nothing output
~ Get the data in the array
~ Get the data in the collection
~ Get data from a map
~ Get Properties of JavaBean

(2) Perform the operation:
Arithmetic operations
+-*/
Logical operations
Comparison operation

Ternary operators
Empty operator

(3) Get common development object: El has built-in 11 built-in objects, these objects El Built-in, do not need to be defined in advance can be used directly in El
!pagecontext-it's easy to get 9 of the most implicit objects in a JSP page

!pagescope--A map of attributes in the page field
!requestscope--A map of attributes in the request domain
!sessionscope--A map of attributes in the session field
!applicationscope a map of attributes in the--application domain

!param--Map<string,string> of all request parameters
Paramvalues--Map<string,string[]> of all request parameters

Header--Map<string,string> of all request headers
Headervalues--Map<string,string[]> of all request headers

!cookie--Map<string,cookie> of all cookie information

Initparam--The initialization parameters of all Web applications make up a map



(4) Call the Java method:-------------no need to write the procedure of calling method, as long as the tag library that someone else has written will be called FN Tag library
~ Write a class that contains a method to be called by El, this method must be a static method
~ Write a TLD file in which to describe the static method to be called
~ taglib directives in JSP pages to introduce TLD files into the current JSP page so that the described method can be called in the JSP page

3.JSTL:
<c:out> labels are used to output a piece of text to an "out" object currently saved by the PageContext object. The
<c:set> tag is used to place an object within a specified domain scope, or to set properties of a Property object of type Java.util.Map or JavaBean of type in a web domain.
<c:remove> tags are used to delete properties in various web domains
<c:catch> tags are used to capture the exception that is thrown in the contents of the tag body, with the following syntax: <c:catch [var= "VarName" ]>nested actions</c:catch>
!! <c:if test= "" > tags can construct a simple "if-then" structure conditional expression
!! <c:choose> labels are used to specify the combined boundaries of multiple criteria selections, which must be used with <c:when> and <c:otherwise> tags. Using <c:choose>,<c:when> and <c:otherwise> three tags, you can construct complex conditional judging structures similar to "If-else If-else".
!! The <c:forEach> tag is used to iterate through the elements in a collection object, or to repeat iterations of the contents of the tag body at a specified number of times.
!! <c:forTokens> is used to browse through all the members of a string whose members are the
<c:param> tags delimited by the definition symbol, and frequently append some parameters to the URL address when the URL is associated with the JSP page. <c:param> tags can be nested within <c:import>, <c:url>, or <c:redirect> tags, attaching parameters to the URL addresses used by these tags.
<c:import> tags, implementing include actions
<c:url> tags are used to construct a URL address in a JSP page whose primary purpose is to implement URL rewriting. URL rewriting is simply attaching the session ID as a parameter to the URL address
<c:redirect> label for implementing request redirection

4. Custom Label Technology:

Tags: jsp can be used to generate HTML pages, or directly write Java source processing logic, which led to a lot of developers in the early days of JSP, only with JSP development, this JSP page is very large, full of Java source code and HTML tags, many hundred percent, logical structure confusion, It is not advisable to debug programs and page beautification. So people want to Java source code from the JSP page as far as possible, but it is unrealistic to remove all Java source code, the most basic access to properties, simple page logic or need, so sun Company provides the JSP tag development technology, with a label to represent a function of Java code, Is that the entire JSP looks more like an HTML, and does not lose the functionality of the JSP for logical processing.
1.JSP Tags: provided by sun, content within the JSP specification, no need to introduce a third-party tag library
<jsp:inclue> to replace Request.getrequestdispatcher (). Include ()
<jsp:forward> used to replace Request.getrequestdispatcher (). Forward ()
<jsp:param> with the first two tags used, can be included or forwarded, with some parameters in the past

<jsp:usebean id= "Beanname" class= "Package.class" scope= "Page|request|session|application"/> Searches for a bean with the name set value in the specified field, if one is not created in the domain
<jsp:setproperty name= "Beanname"
{
Property= "PropertyName" value= "{string | <%= expression%>}" |
property= "PropertyName" [param= "ParameterName"] |
property= "*"
The}/> is used to specify the name of the bean for the Bean's attribute negative name, the property specifies the name of the attribute to set, value specifies the Set property, and Param is used to specify which request parameter to use to set the property, which can be set to *, Used to set all the request parameters automatically understand the properties of the bean corresponding to the

<jsp:getproperty name= "Beaninstancename" property= "PropertyName"/> is used to get the value of the property output to the output stream, where name specifies the name of the Bean, property specifies the name of the attribute on the bean

2.el expression: Replace <%=%> script expression, before j2ee1.4 the default is not support El, if required to specify the page directive [iselignored= "true | false"] is false,j2ee4.0 after the default support El
2.1 Getting properties in a domain
${propname} searches for the Proname attribute in four domains, outputting the value to the output stream
${pagescope/requestscope/sessionscope/applicationscope.proname} Gets the properties in the specified domain
${attr[0]} Gets the specified element of the collection in the domain
${list[0]} Gets the specified position element in the list
${map.keyname} Gets the value of the specified key in the map
${bean.propname} Gets the properties of the JavaBean, which can be thought of as a getxxx method that calls JavaBean.
~ The most important application scenario: it is best not to write the Web app name to death when writing the path, and Java should use Request.getcontextpath to get it. Use El in JSP to get: ${pagecontext.request.contextpth}
~. and [] differences between the use of points can be used in brackets, if the property name is a number or contains a special symbol (.-) You must use the brackets. Example: ${map["First.name"]}
2.2 For simple operations
(1) Arithmetic operation: All elements involved in the meta-calculation will be converted to a number, if the error cannot be transferred, empty elements participate in the operation as not involved.
(2) Relational operations:
(3) Logical operation:
(4) Empty/not empty determines whether an object or collection or array is empty or has a length of 0
(5) ternary expression name = = null? "Zhang San": name;
2.3 Getting web development common objects
PageContext: Represents PageContext object, note and Pagescope are differentiated
Pagescope: Represents the page field, which can be used to get properties in the page field
Reqeustscope: Represents the Reqeust domain, which can be used to obtain properties in the Reqeust domain
Sessionscope: Represents the Session field, which can be used to get the properties in the Session field
Applicationscope: Represents the application domain, which can be used to obtain properties in the application domain
Param the Map collection representing the request parameters ${param.username}
The paramvalues represents a map collection that requests a host, but the value of this collection is string[], which is used to obtain a multivalued param
Header gets a map of the request header
Headervalues gets a map that consists of a request header but value is a string[] to get a multi-valued head
Cookie gets a map object consisting of a cookie, and the value of this map is a cookie object ${cookie.cookiename.cookievalue}
Initparam the initialization parameters for the entire Web application configured in the map package. xml
2.4 Calling the Java method
The EL expression can call a static method in Java, complete as follows:
(1) Write a class that should contain a static method to use the EL Call
(2) write a TLD file describing the invocation of the method, and use the 2.0 version of the JSP configuration when creating the TLD file, specifying the namespace URI and abbreviation prefix
(3) Configuring method information in TLD files
<function>
<name>encodeurl</name>el the method name to use when calling
<function-class>cn.itheima.util.EncodeURL</function-class> the class full path name of the static method
<function-signature>
Java.lang.String Encodurl (java.lang.String)//Description of the method: return value type method name (parameter type)
</function-signature>
</function>
(4) using <%@ taglib uri= "" prefix= "PPP"%> in JSP to introduce TLD files
(5) Call in JSP using ${ppp:encodeurl ("XXXX")}
The 2.5SUN provides the El function library, basically is the function which carries on the operation to the string, please refer to teacher Zhang writes "8th Chapter standard Tag Library _0519.doc"







==========================================
3.JSTL Tag Library, in javaee4.0 need to import JSTL related jar package, beginning with javaee5.0, this jar package is already included by default. Also need to use <%@ taglib%> directive to introduce tag library
3.1 Introduction
JavaServer Pages Standard Tag Library
Standard specified by JCP (Java Community Process)
Provided to Java WEB developers a standard common label function library
and EL mates to replace the traditional practice of embedding Java programs directly on the page (Scripting) to improve program readability, maintainability, and convenience
Generally we use versions above JSTL1.1 and should support El expressions starting from this version
JSTL1.0 does not support El expressions by default, it is not recommended to use
3.2JSTL Tag Library
Core Tag Library (CORE)---C
Internationalization label FMT
Database Label SQL--servlet
XML tag XML
Jstl function (el function) El
3.3JSTL Core Tag Library
The <c:out> tag is used to output a piece of text to an "out" object currently saved by the PageContext object.
The <c:set> tag is used to place an object within a specified domain scope, or to set properties of a Property object of type Java.util.Map of a web domain or a Property object of type JavaBean.
<c:remove> tags for deleting properties in various web domains
The <c:catch> tag is used to capture the exception that is thrown in the contents of the tag body, and its syntax is formatted as follows: <c:catch [var= "VarName"]>nested actions</c:catch>
<c:if test= "" > tags can construct a simple "if-then" Structure of conditional expressions
<c:choose> labels are used to specify the combined boundaries of multiple criteria selections, which must be used with <c:when> and <c:otherwise> tags. Using <c:choose>,<c:when> and <c:otherwise> three tags, you can construct complex conditional judging structures similar to "If-else If-else".
The <c:forEach> tag is used to iterate through the elements in a collection object, or to repeat iterations of the contents of the tag body at a specified number of times.
<c:forTokens> is used to browse all the members of a string, whose members are delimited by the definition symbol
<c:param> tags when you make a URL-related operation on a JSP page, you often append some parameters to the URL address. <c:param> tags can be nested within <c:import>, <c:url>, or <c:redirect> tags, attaching parameters to the URL addresses used by these tags.
<c:import> tags for include operations
The <c:url> tag is used to construct a URL address in a JSP page whose main purpose is to implement URL rewriting. URL rewriting is to append the session ID to the URL address as an argument
<c:redirect> tags for request redirection

4. Custom Labels

The labeling technology of JSP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.