Twelve, scripting elements, instructions, and predefined variables

zhaozj2021-02-16  145

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 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 day! <%} Else {%> Have a < B> Lousy day! <%}%> The above JSP code will be converted to servlet code: if (Math.random () <0.5) {Out.Println ("Have a nice Day! ");} Else {outputln (" Have a Lousy DAY! ");} If you want to use characters"%> "inside the Scriptlet, you must write"% \> ". Also, please note that <% code%> XML equation is: Code 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 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> <body bgcolor = "# fdf5e6" text = "# 000000" LINK = "# 0000e" VLINK = "# 551a8b" alink = "# ff0000"> <center> <table border = 5 bgcolor = "# EF8429"> <tr> <TH class = "title"> JSP Application example </ table> </ center> <p> The following is some dynamic content generated by various JSP functions: <ul> <li> <b> expressions. </ B> <br> Your host name: <% = Request.getRemoteHost ()%>. <Li> <b> JSP scripTlet. </ B> <br> <% out.println ("query string:" Request.getQueryString ());%> < Li> <b> Declaration (and expression). </ B> <br> <%! Private int accessCount = 0;%> Server starts since the server starts: <% = accesscount%> <li> <b> Instructions (and expressions). </ B> <br> <% @ 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.</p> <p>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.</p> <p>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: <JSP: Expression> 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.</p> <p>For example, the following JSP fragment mixes template text and scriptlet: <% IF (Math.random () <0.5) {%> Have a <b> 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 <b> nice </ b > Day! ");} Else {outputln (" Have a <b> Lousy </ B> DAY! ");} If you want to use characters"%> "inside the Scriptlet, you must write"% \> ". Also, please note that <% code%> XML equation is: <JSP: Scriptlet> 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: <JSP: Declaration> 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.</p> <p>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.</p> <p>The XML syntax of the command is: <jsp: Directive.directiveType Attribute = value />, below this instruction: <% @ page import = "java.util. *"%> Its XML equation is: <JSP: Directive.page import = "java.util. *" /> 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.</p> <p><! DOCTYPE HTML PUBLIC "- // W3C // DTD HTML 4.0 Transitional // En"> <html> <head> <title> javaserver pages </ title> </ head> <body bgcolor = "# fdf5e6" text = "# 000000" LINK = "# 0000e" VLINK = "# 551a8b" alink = "# ff0000"> <center> <table border = 5 bgcolor = "# EF8429"> <tr> <TH class = "title"> JSP Application example </ table> </ center> <p> The following is some dynamic content generated by various JSP functions: <ul> <li> <b> expressions. </ B> <br> Your host name: <% = Request.getRemoteHost ()%>. <Li> <b> JSP scripTlet. </ B> <br> <% out.println ("query string:" Request.getQueryString ());%> < Li> <b> Declaration (and expression). </ B> <br> <%! Private int accessCount = 0;%> Server starts since the server starts: <% = accesscount%> <li> <b> Instructions (and expressions). </ B> <br> <% @ 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.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-8975.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="8975" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.035</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'cLXbj37ywffxt9uL_2FmFMEaRziTdQR7UB0xRwmQHYNdm5_2FIHLWv0g5LK0aD_2FU34mv05z_2BF5dOioVWfIqJ6GucVw_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>