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:
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:
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:
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.
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.
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
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.
For example: