Basic knowledge of Jsp development + nine implicit objects + 11 built-in objects + tag Library (typical in actual web Development)

Source: Internet
Author: User
Tags tld

I. jsp Basics
1. JSP Concept
Servlet is a dynamic resource development technology provided by j2ee and is developed in the form of java programs. It is a headache to write HTML tags in java. Therefore, JSP is developed, it looks like HTML, but the Servlet can be generated through server compilation.
2. JSP Components
2.1 template Elements
HTML content directly written in JSP looks as convenient as HTML content, but will be directly output in the process of being translated into Servlet.
2.2 script expression
<% = Expression %> A java expression is accepted. During JSP translation to Servlet, the value of the expression is calculated and Output Using out. write ().
2.3 script snippet
<%> You can directly write the java source code in the script fragment. The source code is directly copied to the response location in the translated servlet.
2.4JSP Declaration
<%! %> You can write java code. The source code is copied to the service method in the servlet. You can use it to add member methods, member variables, and static code blocks to the servlet.
2.5JSP comments
<% -- %> The content surrounded by jsp comments will not be translated to servlet by the server. Be sure to distinguish between html comments and java comments.
Jsp comments will not be translated into servlet and will be discarded during translation
Java annotations are not compiled into class files and are discarded during compilation.
Html comments are directly output to the browser as template elements, and the content in the html comments is not displayed in the browser.
2.6JSP commands
2.6.1page command
Used to notify the Translation Engine. If you translate the current JSP
[Language = "java"] current JSP development language
[Extends = "package. class"] the class to be inherited after jsp is translated into servlet. Note that this value must be a subclass of servlet. Generally, do not change it.
[Import = "{package. class | package. *},... "] java. lang. *; javax. servlet. *; javax. servlet. jsp. *; javax. servlet. http. *;
[Session = "true | false"] specifies whether to use the session on the current page. If it is set to true, the translated servlet will reference the session object, therefore, you can directly use the session implicit object in jsp. However, once the jsp is accessed, the request. getSession () method will be called, which may result in unnecessary space waste. If you are sure that the jsp does not require a session, you can set it to false.
[Buffer = "none | 8kb | sizekb"] size of the buffer used by the out implicit object
[AutoFlush = "true | false"] whether the out implicit object automatically refreshes the buffer. The default value is true and does not need to be changed.
[IsThreadSafe = "true | false"] whether the translated servlet implements SingleThreadModel
[ErrorPage = "relative_url"] If a page error occurs, the page to be redirected can be found on the web page, except for the error page specified by using this attribute in jsp. configure the error page of the entire web application in xml. If both are set, this attribute in jsp takes effect.
[IsErrorPage = "true | false"] If this attribute is set to true, the translated servlet contains an implicit Exception object, which encapsulates the Exception object thrown on the previous page.
[ContentType = "mimeType [; charset = characterSet]" | "text/html; charset = ISO-8859-1"] commands related to jsp garbled, used to specify jsp output, the Content-Type response header is used to specify the encoding that the browser opens.
[PageEncoding = "characterSet | ISO-8859-1"] sets of metrics used when the server translates jsp. to prevent jsp garbled characters, ensure that the file storage encoding is consistent with the servlet-translated jsp code and the encoding opened by the browser after the file is output to the browser. once this attribute is set, the Translation Engine will indirectly help us set the content-type attribute.
[IsELIgnored = "true | false"] whether the el expression is used on the current page. If it is set to false, el is enabled. It is supported by j2ee4.0 by default. When j2ee4.0 is used for development, to use an el expression, set this attribute to fals.


2.6.2include command
<% @ Brief Ede file = "" %> introduce other page content statically
* Static Introduction: the source file level is merged. Multiple JSPs generate a servlet and the servlet generates a response. Recommended.
* Dynamic Introduction: Multiple outputs are merged at runtime. Multiple JSPs generate multiple servlets respectively, and the servlets generate responses to form an output stream and provide responses. The execution efficiency is not highly static.

2.6.3taglib command
<% @ Taglib uri = "" prefix = "" %> is used to introduce the tag library.
Uri specifies the namespace of the. tld file to be introduced.
Prefix

2.7 nine implicit objects
Config
Application
Request
Response
Session
Out
Page
PageContext
Exception

2.7.1out object
It can be understood as the PrintWriter obtained by response. getWriter.
It comes with a buffer, and its size is limited by the buffer settings in the page instruction. When the buffer is full or the buffer is closed or the current jsp page ends, the content in the buffer is refreshed to the buffer zone of response. getWriter.
2.7.2PageContext object
(1) obtain the other eight implicit objects, which can be considered as an entry object.
(2) obtain data from all its domains
PageContext
Public java. lang. Object? GetAttribute (java. lang. String? Name, int? Scope)
Public void setAttribute (java. lang. String? Name, java. lang. Object? Value, int? Scope)
Public void? RemoveAttribute (java. lang. String? Name, int? Scope)

Constant representing the domain in pageContext
PageContext. APPLICATION_SCOPE
PageContext. SESSION_SCOPE
PageContext. REQUEST_SCOPE
PageContext. PAGE_SCOPE

The findAttribute method searches for attributes in four domains. The search sequence is the page domain, request domain, session domain, and application domain. The findattri method searches from a small domain to a large domain, if this value is found, it is obtained directly. If no domain is found, a null value is returned (unlike the el expression, null is returned here, which is unfriendly to the webpage)


(3) Use as a domain object
Scope: a jsp page is the smallest of the four scopes.
Lifecycle: generated when a jsp request starts and destroyed when the response ends.

(4) Jump to other resources
It provides the forward and include methods to simplify redirection and forwarding operations.


========================================================== ========================================================== ======================================
Ii. Tag: JSP can be used to generate HTML pages or directly write the java source code processing logic. As a result, many developers only use JSP for development in the early stages of JSP development, this JSP page is very huge, full of java source code and HTML tags, many percent signs, chaotic logic structure, not suitable for debugging programs and page beautification. So people want to extract the java source code from the JSP page as much as possible, but it is unrealistic to extract all the java source code. The most basic way to obtain attributes and simple page logic is still needed, as a result, sun provides the tag Development Technology in JSP, and uses a tag to represent a functional java code. The whole jsp looks more like an HTML, and does not lose the JSP logic processing function.
1. JSP Tag: Provided by sun, which is in the jsp specification and does not require the introduction of a third-party tag Library
Used to replace request. getRequestDispatcher (). include ()
Used to replace request. getRequestDispatcher (). forward ()
When used with the first two tags, some parameters can be included or forwarded.

Search for the bean whose name is set as id in the specified domain. If no value is found, create
{
Property = "propertyName" value = "{string | <% = expression %>}" |
Property = "propertyName" [param = "parameterName"] |
Property = "*"
}/> Specifies the bean name for the name of the negative value of the bean property, the name of the property to be set, and the value to specify the value of the property, param is used to specify which request parameter is used to set this attribute. property can be set to *. It is used to automatically set all request parameters.

Gets the attribute value and outputs it to the output stream. The name specifies the bean name, and the property specifies the name of the bean.



2. el expression: Replace the <% = %> script expression. el is not supported by default before j2ee1.4. If you need to specify the page Command [isELIgnored = "true | false"] as false, el is supported by default after j2ee4.0
2.1 obtain attributes in the domain
$ {PropName} searches for the proName attribute in four domains and outputs the value to the output stream.
$ {Pagination/requestScope/sessionScope/applicationScope. proName} Get attributes in the specified domain
$ {Attr [0]} gets the specified element of the Set in the field
$ {List [0]}: Obtain the specified position element in the list.
$ {Map. keyName} gets the value of the specified key in map.
$ {Bean. propName} obtains the attributes of the javaBean. It can be considered that the getXXX method of the javaBean is called,
~ The most important Application Scenario: it is best not to write the web application name to death when writing a path. in java, you should use request. getContextPath to get it. Use el in jsp to get: $ {pageContext. request. contextPth}
~. Brackets can be used for vertices. If the attribute name is a number or contains a special symbol (.-), brackets must be used. Example: $ {map ["first. name"]}
2.2 simple operations
(1) arithmetic operation: All elements involved in the metacalculation are converted into numbers. If they cannot be converted, an error is returned. null elements are involved in the calculation as not involved.
(2) relational operation:
(3) logical operations:
(4) empty/not empty determines whether an object, set, or array is empty or the length is 0.
(5) ternary expression name = null? "James": name;
2.3 obtain common web development objects
PageContext: indicates the pageContext object, which must be distinguished from pageContext.
Pagination: indicates the page field. It can be used to obtain attributes in the page field.
ReqeustScope: indicates the reqeust domain. It can be used to obtain attributes in the reqeust domain.
SessionScope: indicates the session domain. It can be used to obtain attributes in the session domain.
ApplicationScope: indicates the application domain. It can be used to obtain attributes in the application domain.
Param indicates the map set composed of Request Parameters $ {param. userName}
ParamValues indicates a map set composed of request parameters, but the value of this set is String [], used to obtain a multi-value param
Header to obtain the map composed of request headers
HeaderValues obtains a map composed of request headers, but the value is a String [], used to obtain a multi-value head.
Cookie: A map object consisting of cookies. The map value is a cookie object $ {cookie. cookieName. cookieValue}
InitParam initializes the parameters of the entire web application configured in web. xml encapsulated by map.
2.4 call the java method
The el expression can call the static method in java by following the steps below:
(1) Compile a class, which should contain the static method to be called using el
(2) Compile a tld file to describe the call of this method. When creating a tld file, use the jsp configuration of Version 2.0 to specify the namespace uri and prefix.
(3) configure method information in the tld File

EncodeURL Method name used by el for calling
Cn. itheima. util. EncodeURL Full class path name of the Static Method

Java. lang. String EncodURL (java. lang. String) // description of the method: Return Value Type method name (parameter type)


(4) use <% @ taglib uri = "" prefix = "ppp" %> in jsp to introduce tld files
(5) use $ {ppp: encodeURL ("xxxx")} in jsp to call
The EL function library provided by 2.5SUN is basically a function for operating strings. For details, refer to Chapter 1 Standard tag library _0519.doc written by instructor Zhang.







========================================================== =
3. The JSTL tag library needs to import jar packages related to JSTL in javaee4.0. This jar package is already included by default starting from javaee5.0. You also need to use the <% @ taglib %> command to introduce the tag Library
3.1 Introduction
Assumerver Pages Standard Tag Library
Standards specified by JCP (Java Community Process)
Provides Java Web developers with a standard and common label function library
Works with EL to replace the traditional practice of embedding Java programs (Scripting) directly on pages to improve program readability, maintainability, and convenience.
Generally, we use Versions later than JSTL1.1. We should support EL expressions from this version.
By default, JSTL1.0 does not support el expressions.
3.2JSTL tag Library
* *** Core --- c
International tag fmt
Database tag SQL-Servlet
XML tag xml
JSTL function (EL function) el
3.3JSTL core tag Library
Label is used to output a piece of text to the "out" object currently saved by the pageContext object.
A tag is used to store an object within a specified domain range, or to set attributes of java. util. Map type attribute objects in the Web domain or attribute objects of the JavaBean type.
Tags are used to delete attributes in various Web domains.
Tags are used to capture exceptions thrown by content nested in the TAG body. The syntax format is as follows: Nested actions
A tag can be used to construct a simple conditional expression of the "if-then" structure.
A tag is used to specify the Combination Boundary of multiple conditions. It must be And Tags are used together. Use , And Three tags can be used to construct a complex condition judgment structure similar to "if-else.
A tag is used to perform cyclic iteration on elements in a collection object, or to repeatedly iterate the content in the TAG body according to the specified number of times.
Used to browse all the members of a string, whose members are separated by the definition symbols.
When tags are used to perform URL-related operations on JSP pages, some parameters are often appended to the URL address. Labels can be nested in , Or Add parameters to the URLs used by these tags.
Label to implement the include Operation
A tag is used to construct a URL address on a JSP page. Its main purpose is to rewrite the URL. URL rewriting is to append the session ID number as a parameter after the URL address
Tags are used to redirect requests.


4. Custom tags

Related Article

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.