JSP Advanced Programming 4

zhaozj2021-02-08  235

Chapter 4 JSP Syntax and Semantic This chapter will detail the syntax and semantics of J S P (J S P 1. 1). If the reader is in contact with the ASP or PHP, the syntax of JSP will be discovered slightly more complicated; on the other hand, if the reader has the experience of the J ava language program design, it will feel that JSP is quite simple. In fact, as a member of J 2 EE, JSP Originally became part of J Ava. In J S P, the following contents are mainly included: instructions. The instruction provides global information of this page, for example, an important state, error handling, whether it is part of S e s I O N, and the like. statement. Page range variables and methods of methods. script. The J a V A code is embedded in the page. expression. Turn the expression into a S t R I n G type to facilitate the output of the page. Here will be described below. 4.1 General Syntax Rule JSP Page is made of many "elements", the syntax rules described in this section are set up for these "elements", so that "universal" syntax rules, that is, these elements Features. 4.1.1 Most of the grammar rules of the element is based on the "XML" grammar. These "XML" syntax-based JSP elements are generally a starting flag containing element name, which may also contain attribute settings. , Some options, and an end flag. Of course, the starting tag and end tag of the JSP page must be in the same page, some elements have only one flag containing the property settings, for example: body The reader will find that this syntax form has been used in the previously described TO MCAT. XML and Web. XML has been used. Script elements are used as ASP-like syntax: <% .........%> In fact, each JSP page should be converted to an equivalent XML page, in the next chapter The JSP as XML will be described in detail. The attribute of the JSP element is also complied with the same principles, and the attribute value of the JSP page must be enclosed using quotation marks. In addition, as an alternative, Use & apos; and & quTo; to represent dual quotation marks and single quotes. 4.1.2 The relative paths in JSP In JSP, you can use a relative path to replace the absolute path, called "URI" in the syntax specification of JSP, Interested readers can find a relatively complete description in RFC 2 3 9 6. Here is a few examples to illustrate the concept of relative path: "jsprelativetest.jsp" / jsprelativetest.jsp "../ jsprelativetest.jsp" In this three lines of code, it is assumed to use the above relative paths in C: / Tomcat / J Akarta - Tomcat / WebApps / Examples / JSP / TEST. JSP.

For the first line of code, it is obvious that the position of the file JSP R Elative TE St. JSP should be: c: / tomcat / j akarta -tomcat / webapps / examples / jsp / jsp R Elative TE ST. JSP, this is easier Understandable. But for the second line of code, you should pay attention. In JSP, when the relative path starts with "/", it is not the root of the website, but relative to the root directory containing the WE B application containing this JSP file. It should also be: c: / tomcat / j akarta - tomcat / webapps / examples / jsp R Elative TE ST. JSP. If the reader is not clear about the concept of a WE B application, see the relevant chapters in the previous chapters. The third line of code should be unfamiliar with the reader familiar with the UNIX or DOS command line. In this case, the location of the file JSP R Elative TE ST. JSP should be: C: / Tomcat / J Akarta - Tomcat / WebAPPS / Examples / JSP / JSP R ELATIVE TE ST. JSP. 4.2 Note In general, it can be considered that there are two different types of comments in the JSP page: one is a JSP itself, used to describe the JSP program code, the other is the page generated by JSP, that is, HTML (or Note for WML...), Used to describe the functionality of the results page executed after the JSP page. Note here is here, the following is an example of a jSP itself: <% - this is a JSP comment -%> In fact, due to the JSP's "Script Code" section, The "S CripTlet" that will be mentioned later uses "<% ...%>" can be used in the part of J ava, so the following forms of comments can be used for granted: <% / * This is a comment in a scriptlet * /%> <% / ** This is also a comment in a scriptlet. You can use javadoc to extract annotations from the generated Java file * /%> 4.3 instructions have been told before The status in JSP, the instructions generally have the following form: <% @ Directive {Attr = "Value"}%> The syntax form of the instruction is simple, but not in line with XML, Chapter 5 will tell The XML syntax of the instruction. 4.3.1 "P A G E" instructions P A g e instructions describe the instructions related to the page. In a J s P page, the P A g e command can occur multiple times, but the grammar and semantics of Chapter 4 J S P 91

Each attribute can only appear once, and repeated property settings will overwrite the previous settings. The basic page command syntax is as follows: <% @ page page_directive_attr_list%> page_directive_attr_list :: = {language = "scriptingLanguage"} {extends = "className"} {import = "importList"} {session = "true | false"} { Buffer = "none | sizekb"} {autoflush = "true | false"} {isthreadsafe = "true | false"} {info = "info_text"} {errorPage = "error_URL"} {ISERRRPAGE = "true | false"} { Table 4 - 1 is the explanation of these properties: Table 4 - 1 Property name and optional value Description Language = "java" said that the language variable tells Server which language will be used in the file, current in JSP In the specification (JSP 1. 1), Java is the only supported syntax extends = "package. Class" saying the EXTENDS variable defines the parent class of the servlet generated by the JSP page. This property will not be used. But when you need to implement some special functions, it is also more convenient to say that the import variable is the same as the first part of any Java program. Similarly, it is always placed at the top of the J s P file. The value of the import variable is a list of subsets separately, indicating that the packets and classes you want to introduce: Java.lang. * Say javax. servlet. * Say Javax.Servlet.jsp. * javax.servlet.http. * Say that the default by the JSP engine has been default, the default value of the session variable is TRUE, indicating that there is a default object called "session" to represent the current session. The type of "S e S I o n" object is: Javax.Servlet.http.httpsessionB u ffer = "N o N e | 8 K B | S i z e k b" said that it is determined whether the output stream (O U T object) requires buffering, the default value is 8 k b. Use autoflush = "true | false" with Auto F Lush to determine if the output buffer is automatically refreshed, if you set up, when the buffer is full, refresh the buffer rather than throw an exception isthreadsafe = "true | false "Say the default value is true, if multiple clients request to send to the JSP engine, it can be processed once. The J s P programmer has to handle the status shared when synchronization to ensure that it is really safe when synchronization.

Say that if IS T HREAD S AFE is set to false, use a single-threaded mode to control client access to this page saying that this does not give you out of anomalous branch, however, in the case of Server, create this page through it Multiple instances run to handle multiple loaded client requests. Moreover, this cannot guarantee that the continuous request of the same client is positioned on the same J S P page, and the shared resources and status between multiple page requests must be synchronized 92 first part JSP entry.

Import = "package. *, package. Class", for example: <% @ page import = "java. util. *"%> (continued) attribute name and optional value Description INFO = "text" said page information page information SERVLET Get SERVLET I NFO () Type ErrorPage = "Path TO E RRRRPAGE" to get the page can be accessed. ErrorPage = "path to e rrorpage" shows a relative path of a JSP file, which is used to handle exceptions that are not processed. This J S P file is to set I S E R R O R P A g E to T R u e. It is necessary to note that: J S is implemented by using the SE-T T T R I B U t () method using S E R V L E T R e q u e () method to store name J A X. S E R V L E T. J S P. J S P e x c ​​e P T I O n's object stored. Say additional, when A UTO F Lush is set to True, after the data in the J SP WR ITER is refreshed in the SERVLET R ESPONSE, no capture exception from the JSP file to the error handling file will not be transmitted normally ISERRORPAGE = "TRUE | FALSE" said that a page is an error handling page. When set to T R u E, the default object E x c e x c ​​e x c ​​e x c ​​e x c ​​e-page in this J S P page will be defined whose value will be set to the error object of the J S P page that calls this page. The default is F a L s e to set the type of M i M e type and character set of J s P files and final files. This must have appeared in the front of the file, any other character appears in the file. The default is: c ontent type = "text / html; charset = ISO - 8859 -1" 4.3.2 The role of the Include Include instruction is to include another file, which is quite simple to syntax: <% @ include file = ".. .... "%> The relative path representation of the JSP described above should be used in this instruction. It should be noted that the JSP has another method containing other files: Table 4 - 2 Compare the difference between the two: Table 4 - 2 Syntax Status Object Description <% @ include File = "................. .S. The file containing the file syntax analysis 4.3.3 Taglib instruction Taglib instructions to indicate the label library used by this JSP page, the specific usage of the label library belongs to the JSP and more advanced content, here is not discussed, first tell the basic syntax first: <% @ Taglib Uri = "taglibraryuri" prefix = "tagprefix"%> Table 4 - 3 is an explanation for each attribute. Chapter 4, J S P, Semantic 93

C ontent type = "text / html; charset = ISO - 8859 -1" Table 4 - 3 Property Description URI Describes the URI of this label location, can be a relative path or absolute path Tag P Refix specifically defined a indication using this The prefix of the label library, for example, when setting the Tag P Refix to My P Refix, you can use the following statement to use the My TA G tag in this tag library: Draw below these prefixes have been retained: Drawing JSP :, JSPX:, Java :, Javax :, servlet: The pre-declaration can be used freely in script code and expression. The previous SESSION and Exception are two built-in objects, Table 4 - 4 details these built-in objects in JSP: Table 4 - 4 Object Type Description Scope Requestjavax. Sub-class for REQUESTJAVAX. Subclass for Request (User Request) Response Javax. Servlet. SERVLET R ESPONSE Page (Page Performing Period) Page C ontextJavax. servlet. JSP. P Age C ontext Page (Page Performing Period) SessionJavax. Servlet. http. h TTP S Ession made session (session period) ApplicationJavax. servlet. SERVLET C ontext makes Application (Whole WE B application running period) OUT JAVAX. servlet. JSP. J SP WR ITER CONFIGJAVAX. Servlet. SERVLET C Onfig makes Page (Page Performing Period) Page Java. O Bject For P Age (Page Performing Period) Exceptionjava. Lang. T Hrowable Page (Page) Perform period) 94 Part 1 JSP Getting Started

The request of the guest client is usually the subclass of the H TTP SERVLET -R EQUEST. If there is a parameter in the customer's request, the object has a response to the list of the list passenger JSP page, which is h TTP SERVLET R ES -PONSE. The properties of the subclass guest page and related objects that need to be accessed through standard APIs (essentially some objects that make JSP runs) so that the JSP engine is compiled. However, different S e r v e r different ways of implementation of these attributes and objects are the implementation method of the PageContext class that the J S P engine compiles the server returned by the Factory class. P Age C ontext class and request, response objects, and some attributes of the Page instruction (ErrorPage, Session, Bu Ff Er, Autoflush) are initialized while providing Request requests. HTTP session is a combined object servlet The environment passes the SERVLET C onfig object, which is the SERVLET C onfig object, which is the Object Guest page representative of the output stream, by calling the Get SERVLET C onfig (). Get c ontext () method points to the page's own way (more this is replaced in the Java code) There is no error captured by T hrowable. The U R I4.5 scripting element passing over E R R O R P A g e In J S P, the main program part is a scripting element, including three parts: declaration (DE C L A R A Ti O N), expression (Ex P R E S I O n), and code (SC R I P L e t). From functional, the declaration is used to declare one or more variables, the expression will be a complete language expression, and the code section will be some program blocks. The basic syntax of three script elements is to start with a "<%", and at the end of a "%>". An example of a statement: <%! This is a declaration%>: <% this is a scriptle%> Example of expression: <% = this is an expression%> Script elements also have corresponding XML compatible syntax, will Introduced in Chapter 6. 4.5.1 Declaring Declaration in J S P is used to declare one or more variables and methods, and do not output any text to O U t output flow. The variables and methods declared in the declaration element will initialize when the J s P page is initialized. The syntax is: <%! Declaration (s)%> Examples are as follows: <%! INT i = 0;%> <%! Public string f (INT i) {IF (i <3) return ("...") ; ...}%> In fact, statements that declare variables and methods can be fully placed in s criptlet, what is the difference between the two? The declaration statement in <%!...................................................... ..

4.5.2 Expression J S P can be seen as a simple output form, it is important to note that the expression must have a value that can be output. The syntax is: <% = expression%> For example, as follows: <% = (new java.util.date ()). TOLOCALESTRING ()%> 4.5.3 Script code so-called script code, the s criptlet, that is, the code in JSP Part of this section can use almost any of the grammatical syntax and semantic 95J AVA of the JSP. The syntax is: <% scriptlet%> For example: <% IF (Calendar.GetInstance (). Get (Calendar.am_PM) == Calendar.am) {%> Good morning <%} else {%> Good afternoon <%} %> 4.6 Action Action can affect the output text stream, use, edit, and build an object. In J S P, there are some basic actions, and users can add their own actions, which requires knowledge of label libraries. The action in J S P is completely based on X m L, which is to see which standards of J S P are in the JS P, and which properties have. 4.6.1 ID and S C O P E Attribute I D Attributes and S C O P E Attributes are attributes having each J S P action, where I D represents a name of an action, and S C O P e represents a function of action. The value of the S c O P E action domain is shown in Table 4 - 5. Table 4 - 5 Scope Value Range Page Page Page by Javax. Servlet. JSP. P Age C Ontext Get Valid REQEST During the User Request This page is enabled by SERVLET R Equest. Get a ttribute (name) Get the entire user During the request, the effective session is obtained by the H TTP S Ession. Get value (name) Get a valid application in the entire session period of the user is active by servletContext. GetAttribute (name) Valid for the WE B application execution 4.6.2 Standard Action JSP Specification Some standards are specified. Any JSP engine that meets JSP specifications should implement these standards, and the standard action specified in JSP 1. 1 will be described below. 1. Probably the most important action in JSP, using this action, JSP can dynamically use the JSP function, due to J Ava B EANS And Simple and clear, make JS P96 first part of JSP

The development process and other dynamic web development tools have an essential difference. Although the dynamic web technology such as A S P can also use component technology, but because of the complexity and use of the A c t i v e x control, the components technology is not used in the actual development work. The syntax is as follows: typespec :: = Class = "classname" | class = "classname" TYPE = "TypeName" | type = "type" class = "classname" | beanname = "beanname" type = "typename" | type = "typeename" beanname = "beanname" | type = "typename" if in , then use the following syntax: body there are several grammar Example: / jsp: UseBean> In this example, this Javabeans object has a session period and has existed in the current session. If this object does not exist, a C L A S c a S t e x C e p T I O N abnormality will be thrown. 2. Action is used to assign a value to a JAVA B Ean, you need to note that the value of the Name property of the name will be a front already in this action. The name of J Ava B Ean introduced using . Table 4 - 6 illustrates the type conversion when using , but uses settings when requesting the client request, any type, JSP file Type conversion is not automatically performed.

Table 4 - 6 Property Type The method used by the S TRING type Booleanjava. Lang. B oolean. Value O F (s TRING) B OOLEA NB Ytejava. Lang. B Yte. Value O F (S TRING) B YT Ei Ntjava Lang. I ntege r. Value O f (s TRING) I Ntege RC Harjava. Lang. C Haracte R. Value O F (S TRING) C Haaracter Chapter 4 JSP Syntax and Semantic 97 (Continued) Properties Types of S TRING Type Conversion Method Doublejava. Lang. D OUBLE. Value O F (S TRING) D Oubl Ef Loatjava. Lang. f Loat. Value O F (S TRING) F LOA TL ONGJAVA. Lang. l ONG. Value O F (S TRING) L ONG The syntax is as follows: prop_expr :: = proty = "*" | Property = "PropertyName" | Property = "PropertyName "param =" parameters "| Property =" PropertyName "value =" PropertyValue "PropertyValue :: = String Table 4 - 7 is Property and Its Interpretation Table 4 - 7 Attribute Name Description Property This property indicates that J The name of the AVA B Rean property. This here is a very interesting special Property setting: When a property is set to "*", the JSP interpreter will enumerate one of the parameters in the system S Ervlet R Equest object, check this J Ava B Ean. The attribute is the same name as the parameters in the SERVKET R Equest object. If there is, it is automatically transmitted to the corresponding JR value value in the SE R Vl e t R e q u e s T object to the corresponding J a V A B E a n attribute P A r a m This property indicates which of the RE Q u e s T is specifically employed when the parameter is transmitted to J a V A B e a n by the system.

Specifically to the WE B page, which F or MV Alue This property indicates that the value you need to set to the J Ava B Ean property can be directly assigned, or the side of the SERVLET R EQUEST object is below. Actions Reverse: All the parameters in the SERVLET R Equest object Request are all entered in J Ava B Ean named request,: Enter the parameter usernme in the SERVLET R Equest Object USER to the JAVA B Ean named User: "i" i 1 "Calculate the properties of J ava b ean named Results after the Name: 3 Action is used to get a value of an attribute from a J ava b, whether the original attribute is in what type, will be converted to a value of a S TRING type. . The syntax is as follows: 98 Part 1 JSP entry

For example: 4. is used to introduce a static or dynamic page to a JSP file, this action is only and J The SP WR ITE object has a relationship. The action can contain one or more acting to transmit parameters to the page to be introduced. The syntax is as follows: or {} Property FLUSH Set whether the buffer is automatically refreshed, in fact, in the current JSP version (1. 1), Flush is set to false without any meaning. The following is an example: 5. is used to boot the client's request to another page or another S E R v L e t goes. The action can contain one or more sub-action for transmitting parameters to the page to be booted. It should be noted that when the occurs, if the text already writes to the output stream and the page does not set buffer, the abnormality of I L E G A L S t a t e E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E E EE.. The following is the grammar of : or {} * Examples are as follows: <% string whereto = "/ templates /" SomeValue;%> 6. actually available A one-one correspondence of name and value, often used as sub-action in , and . The syntax is: Chapter 4 J S P's grammar and semantic 99

转载请注明原文地址:https://www.9cbs.com/read-3323.html

New Post(0)