12.1 JSP scripting elements JSP script elements are used to insert Java code, which will appear in servlets generated by the current JSP page. Script elements have three formats: expression format <% = expression%>: Calculate expressions and output its results. Scriptlet format <% Code%>: Insert the code into the service method of the servlet. Declaration format <%! Code%>: Add the declaration to the servlet class (outside of any method). Let's explain their usage in detail. 12.1.1 JSP Expression JSP expressions are used to insert Java data directly into the output. Its syntax is as follows: <% = java expression%> Calculate the result obtained by the Java expression is converted into a string and then inserted into the page. Calculates when running (when the page is requested), all information related to the request can be accessed. For example, the following code display page is requested by date / time: current time: <% = new java.util.date ()%> To simplify these expressions, JSP predefined a set of object variables that can be used directly. Later, we will detail these implied declarations, but for JSP expressions, the most important objects and their types are as follows: Request: httpservletRequest; response: httpservletResponse; session: and request associated HttpSession Out: PrintWriter With buffered version, jspwriter, is used to send the output to the client below: Your Hostname: <% = Request.getRemoteHost ()%> Finally, if you use XML, JSP expressions can also be written as the following Form: Java Expression jsp: Expression> Remember that XML elements are different from HTML. XML is sensitive, so it is sure to use lowercase. For instructions on XML grammar, see "XML Tutorial" 12.1.2 JSP Scriptle If you want to complete, you can use JSP Scriptlet than inserting simple expressions than inserting simple expressions. JSP Scriptlet allows you to insert any Java code into the servlet. The JSP Scriptlet syntax is as follows: <% Java Code%> and JSP expressions, Scriptlet can also access all predefined variables. For example, if you want to output content to the result page, you can use the OUT variable: <% string querydata = request.getQueryString (); out.println ("Attached Get Data: querydata);%> Note that the code in the scriptlet will be Move into the servlet, while the Scriptlet front and the static HTML (template text) will be converted into a PrintLn statement. This means that the Java statement in the scriptlet is not a complete, no closing block will affect the static HTML outside the Scriptlet.
For example, the following JSP fragment mixes template text and scriptlet: <% IF (Math.random () <0.5) {%> Have a nice b> day! <%} Else {%> Have a < B> Lousy b> day! <%}%> The above JSP code will be converted to servlet code: if (Math.random () <0.5) {Out.Println ("Have a nice b > Day! ");} Else {outputln (" Have a Lousy B> DAY! ");} If you want to use characters"%> "inside the Scriptlet, you must write"% \> ". Also, please note that <% code%> XML equation is: Code jsp: Scriptlet> 12.1.3 JSP declaration JSP declaration to define methods and member variables inserted into the servlet class, whose syntax is as follows : <%! Java code%> Since the declaration does not have any output, they tend to be used with JSP expressions or scriptlets. For example, the following JSP code segment output has been requested by the server start (or the servlet class is changed and reloaded): <%! Private int accessCount = 0;%> Since the server startup, the number of page access has been: < % = AccessCount%>, like Scriptlet, if you want to use strings "%>", you must use "% \>" instead. Finally, <%! Code%> XML equation is: Code jsp: Declaration> 12.2 JSP instruction JSP instruction affects the overall structure of the servlet class, its syntax is generally as follows: <% @ Directive Attribute = "value"%> Alternatively, multiple properties of the same instruction can also be combined, for example: <% @ Directive Attribute1 = "value1" Attribute2 = "Value2" ... attributen = "VALUEN"%> JSP instruction points For two types: The first is the Page instruction to complete the following tasks: Import the specified class, custom servlet's superclass, etc .; the second is the introduction of the Include command to introduce when the JSP file is converted to servlet Other files.
The JSP specification also mentioned the Taglib directive, its purpose is to let JSP developers can define their own tags, but JSP 1.0 does not support this instruction, and it is hoped that it will become one of the main improvements of JSP 1.1. 12.2.1 Page instructions The role of the Page instruction is to define one or more attributes below, which are sensitive. Import = "package.class", or import = "package.class1, ..., package.classn": is used to specify which packages, such as: <% @ page import = "java.util. *"%>. Import is the only attribute that allows you to appear. ContentTyPE = "MIME-TYPE" or ContentTyPE = "MIME-TYPE; Charset = Character-SET": This property specifies the output MIME type. The default is Text / HTML. For example, this instruction below: <% @ page contenttype = "text / place"%>. And the following Scriptlet effect is the same: <% response.setContentType ("text / plain");%> isthreadsafe = "true | false" default TRUE indicates that servlet processing according to standard, that is, the developer has synchronized the instance variable. Access, handle multiple requests simultaneously by a single servlet instance. If the value FALSE is valued, it indicates that the servlet should implement SingLethReadModel, request, or an entry one by one, or multiple parallel requests are processed by different servlet instances. Session = "true | false" default TRUE indicates that the predefined variable session (type httpsession) should be bound to an existing session, and if there is no existing session, create a newly created SESSION variable. If the value False indicates that the session will not be used, trying to access the variable session will cause the JSP to convert to servlet. Buffer = "Size KB | NONE" This property specifies the cache size of JSPWRITE OUT. The default value is related to the server, but at least 8 kb should be 8 KB. Autoflush = "true | false" default TRUE indicates that it is refreshed if the cache is full. Autoflush rarely takes a false value, and the false value indicates that the exception is thrown if the cache is full. If buffer = "none", AutoFlush does not take a false value. Extends = "package.class" This property indicates which superclass uses the servlet to be generated. Use this property should be very careful because the server may already be used in a custom super class. Info = "message" This property defines a string that can be extracted by the GetServletInfo method. ErrorPage = "URL" This property specifies a JSP page, all of which are not captured by the current page. iSerrorPage = "true | false" This property indicates whether the current page can be used as an error handling page for another JSP page. The default value FALSE. Language = "java" This property is used to indicate the language used. There is no need to pay attention to this property because the default Java is the currently available language.
The XML syntax of the command is: , below this instruction: <% @ page import = "java.util. *"%> Its XML equation is: 12.2.2 The include instruction instruction is used to introduce other files when the JSP page is converted to servlet. The instruction syntax is as follows: <% @ include file = "relative URL"%> The URL specified here is the URL relative to the JSP page issued by the reference instruction, however, the same as the relative URL in the usual sense, you can use " / "The beginning of the URL tells the system to treat the URL as starting from the root of the web server. The content containing the file is also a JSP code, that is, the included file can contain static HTML, scripting elements, JSP instructions, and actions. For example, each page of many websites has a small navigation bar. Because there are many problems in the HTML framework, navigation bars often produce with a table top or on the left, and the same HTML code repeatedly appears on each page of the entire website. The include directive is an ideal way to implement this feature. Using the include directive, developers don't have to copy navigation HTML code into each file, so that maintaining maintains more easily. Since the include instruction is introduced when the JSP is converted to servlet, if the navigation bar changes, all JSP pages that use the navigation bar must be re-converted into servlet. If the navigation strip changes are not frequent, and you want to include the operation with as good efficiency, using the include directive is the best choice. However, if the navigation strip changes very frequent, you can use the JSP: Include action. JSP: Include Action The specified file will be referenced when the JSP page request appears, refer to the specific instructions later in this article. 12.3 Example: The application of scripting elements and instructions is a simple example of using JSP expressions, scripTlets, declarations, and instructions.
javaserver pages title> head>
JSP Application example table> center>
The following is some dynamic content generated by various JSP functions:
expressions. B> Your host name: <% = Request.getRemoteHost ()%>.
JSP scripTlet. B> <% out.println ("query string:" Request.getQueryString ());%> < Li> Declaration (and expression). B> <%! Private int accessCount = 0;%> Server starts since the server starts: <% = accesscount%>
Instructions (and expressions). B> <% @ page import = "java.util. *"%> Current date: <% = new date ()%> ul> body> < / Html> 12.4 JSP predefined variables To simplify the JSP expression and Scriptlet code, JSP provides 8 predefined variables (or implied objects). These variables are Request, Response, Out, Session, Application, Config, PageContext, and Page.
12.4.1 Request This is the HTTPServletRequest that requests associated, through it to view request parameters (call getParameter), request type (GET, POST, HEAD, etc.), as well as the requested HTTP header (Cookie, Referer, et al.). Strictly speaking, if the request is used by other protocols other than HTTP, the request can be a subscar class of ServletRequest (not httpservletRequest), but it is hardly used in practice. 12.4.2 Response This is the HTTPSERVLETRESPONSE associated with the answer. Note that due to the output stream (see the OUT below) is buffered, the output content has been sent to the client, and the normal servlet does not allow the HTTP status code, but it is legal in JSP. 12.4.3 OUT This is a PrintWriter used to send content to the client. However, in order to make the Response object more practical, OUT is PrintWriter with cache function, ie jspwriter. JSP allows the cache to be adjusted by the buffer attribute of the PAGE instruction, or even shut down the cache. OUT is generally only used within the scriptlet because the JSP expression is automatically sent to the output stream, rarely needs to reference the OUT. 12.4.4 SESSITION This is the HTTPSession object that requests the request. We have already introduced the automatic creation of the session, and we know, this object is also automatically bound. Even if there is no session reference. But there is an exception, this is if you use the session property of the PAGE instruction to turn the session, at which point the reference to the session variable will cause the JSP page to convert to servlet. 12.4.5 Application This is a servletContext, or it can be obtained through GetServletConfig (). GetContext (). 12.4.6 Config This is the servletconfig object of the current page. 12.4.7 PageContext is mainly used to manage the properties of the page. 12.4.8 Page It is the synonym of this, which is not currently used. It is placeholder for Java is no longer a unique JSP programming language. 12.1 JSP scripting elements JSP script elements are used to insert Java code, which will appear in servlets generated by the current JSP page. Script elements have three formats: expression format <% = expression%>: Calculate expressions and output its results. Scriptlet format <% Code%>: Insert the code into the service method of the servlet. Declaration format <%! Code%>: Add the declaration to the servlet class (outside of any method). Let's explain their usage in detail. 12.1.1 JSP Expression JSP expressions are used to insert Java data directly into the output. Its syntax is as follows: <% = java expression%> Calculate the result obtained by the Java expression is converted into a string and then inserted into the page. Calculates when running (when the page is requested), all information related to the request can be accessed.
For example, the following code display page is requested by date / time: current time: <% = new java.util.date ()%> To simplify these expressions, JSP predefined a set of object variables that can be used directly. Later, we will detail these implied declarations, but for JSP expressions, the most important objects and their types are as follows: Request: httpservletRequest; response: httpservletResponse; session: and request associated HttpSession Out: PrintWriter With buffered version, jspwriter, is used to send the output to the client below: Your Hostname: <% = Request.getRemoteHost ()%> Finally, if you use XML, JSP expressions can also be written as the following Form: Java Expression jsp: Expression> Remember that XML elements are different from HTML. XML is sensitive, so it is sure to use lowercase. For instructions on XML grammar, see "XML Tutorial" 12.1.2 JSP Scriptle If you want to complete, you can use JSP Scriptlet than inserting simple expressions than inserting simple expressions. JSP Scriptlet allows you to insert any Java code into the servlet. The JSP Scriptlet syntax is as follows: <% Java Code%> and JSP expressions, Scriptlet can also access all predefined variables. For example, if you want to output content to the result page, you can use the OUT variable: <% string querydata = request.getQueryString (); out.println ("Attached Get Data: querydata);%> Note that the code in the scriptlet will be Move into the servlet, while the Scriptlet front and the static HTML (template text) will be converted into a PrintLn statement. This means that the Java statement in the scriptlet is not a complete, no closing block will affect the static HTML outside the Scriptlet.
For example, the following JSP fragment mixes template text and scriptlet: <% IF (Math.random () <0.5) {%> Have a nice b> day! <%} Else {%> Have a < B> Lousy b> day! <%}%> The above JSP code will be converted to servlet code: if (Math.random () <0.5) {Out.Println ("Have a nice b > Day! ");} Else {outputln (" Have a Lousy B> DAY! ");} If you want to use characters"%> "inside the Scriptlet, you must write"% \> ". Also, please note that <% code%> XML equation is: Code jsp: Scriptlet> 12.1.3 JSP declaration JSP declaration to define methods and member variables inserted into the servlet class, whose syntax is as follows : <%! Java code%> Since the declaration does not have any output, they tend to be used with JSP expressions or scriptlets. For example, the following JSP code segment output has been requested by the server start (or the servlet class is changed and reloaded): <%! Private int accessCount = 0;%> Since the server startup, the number of page access has been: < % = AccessCount%>, like Scriptlet, if you want to use strings "%>", you must use "% \>" instead. Finally, <%! Code%> XML equation is: Code jsp: Declaration> 12.2 JSP instruction JSP instruction affects the overall structure of the servlet class, its syntax is generally as follows: <% @ Directive Attribute = "value"%> Alternatively, multiple properties of the same instruction can also be combined, for example: <% @ Directive Attribute1 = "value1" Attribute2 = "Value2" ... attributen = "VALUEN"%> JSP instruction points For two types: The first is the Page instruction to complete the following tasks: Import the specified class, custom servlet's superclass, etc .; the second is the introduction of the Include command to introduce when the JSP file is converted to servlet Other files.
The JSP specification also mentioned the Taglib directive, its purpose is to let JSP developers can define their own tags, but JSP 1.0 does not support this instruction, and it is hoped that it will become one of the main improvements of JSP 1.1. 12.2.1 Page instructions The role of the Page instruction is to define one or more attributes below, which are sensitive. Import = "package.class", or import = "package.class1, ..., package.classn": is used to specify which packages, such as: <% @ page import = "java.util. *"%>. Import is the only attribute that allows you to appear. ContentTyPE = "MIME-TYPE" or ContentTyPE = "MIME-TYPE; Charset = Character-SET": This property specifies the output MIME type. The default is Text / HTML. For example, this instruction below: <% @ page contenttype = "text / place"%>. And the following Scriptlet effect is the same: <% response.setContentType ("text / plain");%> isthreadsafe = "true | false" default TRUE indicates that servlet processing according to standard, that is, the developer has synchronized the instance variable. Access, handle multiple requests simultaneously by a single servlet instance. If the value FALSE is valued, it indicates that the servlet should implement SingLethReadModel, request, or an entry one by one, or multiple parallel requests are processed by different servlet instances. Session = "true | false" default TRUE indicates that the predefined variable session (type httpsession) should be bound to an existing session, and if there is no existing session, create a newly created SESSION variable. If the value False indicates that the session will not be used, trying to access the variable session will cause the JSP to convert to servlet. Buffer = "Size KB | NONE" This property specifies the cache size of JSPWRITE OUT. The default value is related to the server, but at least 8 kb should be 8 KB. Autoflush = "true | false" default TRUE indicates that it is refreshed if the cache is full. Autoflush rarely takes a false value, and the false value indicates that the exception is thrown if the cache is full. If buffer = "none", AutoFlush does not take a false value. Extends = "package.class" This property indicates which superclass uses the servlet to be generated. Use this property should be very careful because the server may already be used in a custom super class. Info = "message" This property defines a string that can be extracted by the GetServletInfo method. ErrorPage = "URL" This property specifies a JSP page, all of which are not captured by the current page. iSerrorPage = "true | false" This property indicates whether the current page can be used as an error handling page for another JSP page. The default value FALSE. Language = "java" This property is used to indicate the language used. There is no need to pay attention to this property because the default Java is the currently available language.
The XML syntax of the command is: , below this instruction: <% @ page import = "java.util. *"%> Its XML equation is: 12.2.2 The include instruction instruction is used to introduce other files when the JSP page is converted to servlet. The instruction syntax is as follows: <% @ include file = "relative URL"%> The URL specified here is the URL relative to the JSP page issued by the reference instruction, however, the same as the relative URL in the usual sense, you can use " / "The beginning of the URL tells the system to treat the URL as starting from the root of the web server. The content containing the file is also a JSP code, that is, the included file can contain static HTML, scripting elements, JSP instructions, and actions. For example, each page of many websites has a small navigation bar. Because there are many problems in the HTML framework, navigation bars often produce with a table top or on the left, and the same HTML code repeatedly appears on each page of the entire website. The include directive is an ideal way to implement this feature. Using the include directive, developers don't have to copy navigation HTML code into each file, so that maintaining maintains more easily. Since the include instruction is introduced when the JSP is converted to servlet, if the navigation bar changes, all JSP pages that use the navigation bar must be re-converted into servlet. If the navigation strip changes are not frequent, and you want to include the operation with as good efficiency, using the include directive is the best choice. However, if the navigation strip changes very frequent, you can use the JSP: Include action. JSP: Include Action The specified file will be referenced when the JSP page request appears, refer to the specific instructions later in this article. 12.3 Example: The application of scripting elements and instructions is a simple example of using JSP expressions, scripTlets, declarations, and instructions.
javaserver pages title> head>
JSP Application example table> center>
The following is some dynamic content generated by various JSP functions:
expressions. B> Your host name: <% = Request.getRemoteHost ()%>.
JSP scripTlet. B> <% out.println ("query string:" Request.getQueryString ());%> < Li> Declaration (and expression). B> <%! Private int accessCount = 0;%> Server starts since the server starts: <% = accesscount%>
Instructions (and expressions). B> <% @ page import = "java.util. *"%> Current date: <% = new date ()%> ul> body> < / Html> 12.4 JSP predefined variables To simplify the JSP expression and Scriptlet code, JSP provides 8 predefined variables (or implied objects). These variables are Request, Response, Out, Session, Application, Config, PageContext, and Page.