JSP syntax detailed

xiaoxiao2021-03-06  81

JSP syntax is taken from: http://www.cnjsp.org/view.jsp? Column = 2 & id = 734 HTML Comment Displays a comment on the client. JSP syntax ] - > Example 1 Generated in the client's HTML source code and the same data as above: Example 2 -> Displayed in the client's HTML source code as: Description This annotation and HTML are very similar, that is, it can be seen in "View Source /". The only thing is that you can use expressions in this comment (shown in Example 2) This expression is not different, different from the page, you can use a variety of expressions, as long as it is legal. More please see the expression hidden comment written in the JSP program, but not send it to the customer. JSP syntax <% - Comment -%> example:

CODE:

<% @ Page language = "java"%> a comment test </ title> </ head> <body> <h2> a test of comments </ h2> <% - THIS Comment will not be visible in the page source -%> </ body> </ html></p> <p>Describe the characters with hidden comment markers to be ignored when JSP compiles. This comment is useful when you want to hide or annotate your JSP program. The JSP compiler does not compile the statement between <% - and -%>, which does not display in the customer's browser, nor does it see the declaration in the JSP program in the source code. Variables and methods JSP grammar <%! Declaration; [declaration;] ...%> <%! Int i = 0;%> <%! Int A, b, c;%> <%! Circle a = NEW CIRCLE (2.0);%> Description Declare the variables and methods you want to use in the JSP program. You must also do this, or you will make mistakes. You can declare multiple variables and methods in one time, as long as you end, you will be legal in Java. When you declare methods or variables, please pay attention to some of the following rules: The statement must be ";" end (Scriptlet has the same rules, but the expression is different). You can use it directly in <% @ Page%> Contains the already declared variables and methods, do not need to be re-declared. A statement is only valid in one page. If you want each page to use some statements, it is best to write them into a separate file, then use <% @ include%> or <jsp: include> element. The expression contains a JSP syntax expression JSP syntax <% = expression%> example <font color = "blue"> <% = map.size ()%> </ font> <b> <% = Numguess.Gethint ()%> </ B>. Description expression elements represent a expression defined in a scripting language, which is automatically converted to a string after running, and then inserted into the location displayed in the JSP file. Because the value of this expression has been converted into a string, you can insert this expression in a line of text (the form and ASP is exactly the same). Please remember the following points when you use an expression in JSP: You can't Use a semicolon (";") as an end of the expression. But the same expression is used in a scripTlet! View Scriptlet This expression element can include any expression that is valid in Java Language Specification. Sometimes expressions can also be attribute values ​​for other JSP elements. One expression can become complicated, it may be made of one or more Expression composition, the order of these expressions is from left to right.</p> <p>[/ b] scripTlet [/ b] contains a valid block. JSP syntax <% code fragment%> example <% string name = null; if (Request.getParameter ("name") == null) {%> < % @ Include file = "error.html"%> <%} else {foo.setname ("name")); if (foo.getname (). EqualsignoreCase ("Integra")) Name = "Acura "; If (name.equalsignorecase (" acura ")) {%> Description A scripTlet can include multiple JSP statements, methods, variables, expressions because we can do the following things: Declaration of variables you want to use or Method (Reference Declaration). Write a JSP expression (reference expression). Writing a JSP statement using any implicit object and any object (if you are using Java language, these statements must follow Java) LANGUAGE Specification,). Any text, HTML tag, JSP element must be executed when JSP receives a customer request outside of Scriptlet, if the scriptlet has the content displayed, these displayed content is existing in OUT objects . The include instruction contains a static file in the JSP, parsing the JSP statement in this file. JSP syntax <% @ include file = "relativeURL"%> example include.jsp: Code:</p> <p><html> <head> <title> An incrude test </ title> </ head> <body bgcolor = "white"> <font color = "blue"> The current date and time all <% @ include file = "Date .jsp "%> </ font> </ body> </ html></p> <p>Date.jsp:</p> <p>CODE:</p> <p><% @ Page import = "java.util. *"%> <% = (New java.util.date ()) .tolocalestring ()%></p> <p>Displays in the page: The Current Date and Time Are Aug 30, 1999 2:38:40 Description <% @ include%> The instruction will insert a file containing text or code when the JSP is compiled, when you use <% @ include %> When you do your life, this included process is static. Static container means that this file containing files will be inserted into the JSP file. This containable file can be a JSP file, an HTML file, a text file. If included is a JSP file, the code in this file containing the JSP will be executed. If you just use INCLUDE to include a static file. The result of this containing files will be inserted into the JSP file.</p> <p>Once the included file is executed, the process of the primary JSP file will be recovered, continue to perform the next line. This is included in the HTML file, JSP file, text file, or just a Java code, but you have to pay attention to this included <HTML>, </ HTML>, <body>, </ body> tag can not be used in the file, as this will affect the same tag in the original JSP file, which sometimes causes errors. Some <% @ include% > The behavior of the command is based on special JSP compilation conditions, such as: This file must be open to all customers and must be valid, or it has secure restrictions if this containment is changed, including this file The JSP file will be recompiled attributes: file = "relativeurl" This containing file name is generally referring to relative paths, no port, protocol, and domain name, as follows: "Error.jsp" Templates / Onlinestore.html "/beans/calendar.jsp" If this path starts with "/", then this path is primarily referring to the up and down relational path of the JSP application. If the path starts with a file name or directory name, then this path is JSP that is being used. The current path of the file. Page instruction defines global properties in the JSP file. JSP syntax <% @ page [language = "java"] [EXTENDS = "package.class"] [import = "{package.class | package. *} , ... "] [session =" true | false "] [buffer =" none | 8KB | SIZEKB "] [Autoflush =" true | false "] [isthreadsafe =" true | false "] [info =" text " ] [ErrorPage = "RelativeURL"] [contenttype = "MIMETYPE [; charset = characterset]" | "text / html; charSet = ISO-8859-1"] [ISERRPAGE = "true | false"]%> example <% @% @ Page Import = "java.util. *, java.lang. *"%> <% @ page buffer = "5kb" Autoflush = "false"%> <% @ Page ErrorPage = "Erro R.Jsp "%> Description <% @ Page%> The instruction applies to the entire JSP page, and also includes static include files. But <% @ page%> The instruction does not act on dynamic containing files, such as <JSP: include> You can use multiple <% @ PAGE%> instructions in a page, but the properties can only be used once, but There is also an exception, that is the import property.</p> <p>Because the Import property and the Import statement in Java are almost (refer to Java Language), you can use this property a few times. Whether you put <% @ Page%> instructions in JSP files, its role range It is the entire JSP page. However, for the readability of the JSP program, and good program habits, it is best to put it on the top of the JSP file. Attribute language = "java" declares the type of scripting language, temporarily only "java" extends = " Package.class "Missing the full name of the Java Class, which needs to be added, but you can use it with caution, which will limit the compilation power of JSP. Import =" {package.class | package. *}, ... " A list of imported Java packages, these packages act on blocks, expressions, and declarations. The following packages have been imported at JSP compile, so you don't need to specify: java.lang. * Javax.servlet.jsp. * Javax.servlet.http. * Session = "true | false "Set whether the customer needs http session. (Soced with ASP, it should be unfamiliar with it) if it is true, then Session is useful. If it has false, then you can't use the Session object, and define the <JSP: UseBean> element of Scope = Session. Such use can cause errors. The default is true. Buffer = "None | 8KB | SIZEKB" Buffer size is used by OUT objects to process the executionful JSP to the client browser output. The default is 8KB autoflush = "true | false" setting If the buffer overflows, if it is necessary to force the output, if its value is defined as true (default), the output is normal, if it is set to false, if this buffer overflows, It will cause an unexpected error. If you set the buffer to none, you can't set autoflush to false. Isthreadsafe = "true | false" Set whether the JSP file can be used multithreaded. The default value is true, that is, JSP can handle a request for multiple users at the same time. If set to false, a JSP can only process a request info = "text" once in execution JSP will be joined JSP In, you can retrieve it using the servlet.getServletInfo method. ErrorPage = "RelativeURL" Sets the JSP file that handles exception events. ISERRORPAGE = "true | false" Set whether this page is an error page, if you are set to true, you can use the Exception object. ContentType = "MimeType [; charset = characterset]" | text / html; charset = ISO-8859 -1 "Set the MIME type.</p> <p>The default MIME type is: text / html, the default character set is ISO-8859-1. <Jsp: forward> Redirect an HTML file, JSP file, or a block. JSP syntax <jsp: forward Page = { "RelativeURL" | "<% = expression%>"} /> or <jsp: forward page = {"relativeurl" | <jsp: param name = "}> <jsp: param name =" parametername "value =" { ParameterValue | <% = expression%>} "/> </ jsp: Forward> example <jsp: forward page =" / servlet / login "/> <jsp: Forward page =" / servlet / login "> <jsp: Param name = "username" value = "jsmith" /> </ jsp: forward> Description <JSP: Forward> Tag Passing a request object containing the user request from a JSP file. <jsp: forward> tag The code will not be executed. You can send parameters and values ​​to the target file, in this example, the parameters we pass are username, the value is Scott, if you use the <JSP: Param> tag, the target file must be one Dynamic files can handle parameters. If you use non-buffered output, use <JSP: Forward> to be careful. If you use <JSP: Forward>, the JSP file already has data, then the file execution will errors. Attributes Page = "{relativeURL | <% = express%>}" is an expression or a string Used to illustrate the file or URL you will oriented. This file can be a JSP, block, or other file capable of handling the request object (such as ASP, CGI, PHP). <Jsp: param name = "parameters" value = "{ ParameterValue | <% = expression%>} "/> Sends one or more parameters to a dynamic file, this file must be a dynamic file. If you want to pass multiple parameters, you can use multiple < JSP: param>.</p> <p>Name Specify parameter name, value specifies parameter value. <jsp: getproperty> Gets the property value of the bean, used to display the JSP syntax in the page <jsp: getproperty name = "beanstancename" property = "preordyname" /> example <JSP: usebean ID = "Calendar" Scope = "Page" class = "Employee.calendar" /> <h2> Calendar of <JSP: getProperty Name = "Calendar" Property = "UserName" /> </ h2> Description This <JSP: getProperty > The element will get the property value of the bean, and you can use it or display it in the JSP page. You must create it with <JSP: UseBean> before you use <JSP: getProperty>. <Jsp: getproperty> Elements have some Limit: You cannot use <JSP: getProperty> to retrieve a property that has been indexed. You can use <JSP: getProperty> with the JavaBeans component, but cannot be used with Enterprise Beans. The name of the attribute name = "beaninstancename" bean, by <JSP: UseBean> Specifies the property name specified by Property = "PropertyName".</p> <p>Tip: In the JSP reference in Sun, if you use <JSP: getProperty> to retrieve the value is null, then NullPointersException will appear, and if you use a block or expression to retrieve its value, then in the browser The appearance is null (empty). <Jsp: include> contains a static or dynamic file. JSP syntax <jsp: include page = "{relativeURL | <% = express%>}" flush = "true" /> JSP: incrude Page = "{relativeURL | <% = expression%>}" Flush = "true"> <jsp: param name = "parameters" value = "{parameterValue | <% = express%>}" /> < / jsp: include> Examples <jsp: include page = "scripts / login.jsp" /> <jsp: include page = "copyright.html" /> <jsp: include page = "/ index.html" /> <JSP : include page = "scripts / login.jsp"> <jsp: param name = "username" value = "jsmith" /> </ jsp: include> Description <JSP: Include> element allows you to include dynamic files and static, this The result of two containing files is different. If the file is just a static file, this includes adding the contents of the included file to the JSP file, and if this file is dynamically, then this containing file will be executed by the JSP compiler (this is similar to the ASP You can't judge a file from a file name to be dynamic or static, such as aspcn.asp may just contain some information, without the need. <jsp: include> can handle these two files at the same time, so you don't need to include this file is dynamic or static. If this included file is dynamic, then you can also use <JSP: param> Parameter names and parameter values ​​are also passed.</p> <p>Attributes Page = "{relativeURL | <% = expression%>}" parameter is a relative path, or represents a relative path expression. Flush = "true" Here you must use flush = "true", you can't use false value The default value is false <jsp: param name = "parametername" value = "{parameterValue | <% = express%>}" /> <jsp: param> clause allow you to pass one or more parameters to dynamic Document You can use multiple <JSP: param> to deliver multiple parameters in a page, <JSP: Plugin> Perform an applet or bean, there is a possible to download a Java plugin for execution it. JSP syntax <JSP : plugin type = "bean | applet" code = "classFileName" codebase = "classFileDirectoryName" [name = "instanceName"] [archive = "URIToArchive, ..."] [align = "bottom | top | middle | left | right "] [height =" displayPixels "] [width =" displayPixels "] [hspace =" leftRightPixels "] [vspace =" topBottomPixels "] [jreversion =" JREVersionNumber | 1.1 "] [nspluginurl =" URLToPlugin "] [iepluginurl =" URLTOPLUGIN "]> [<jsp: params> [<jsp: param name =" parameters "value" value = "{parameterValue | <% = express%>}" />] </ jsp: params>] [<jsp: Fallback > Text Message for User </ jsp: Fallback>] <jsp: plugin> example <jsp: plugin type = applet code = "molecule.class" codebase = "/ html"> <jsp: params> <jsp: PA Ram name = "molecule" value = "molecules / benzene.mol" /> </ jsp: params> <JSP: Fallback> <P> Unable to load applet </ p> </ jsp: Fallback> </ jsp: plugin > Description <JSP: PLUGIN> element is used to play or display an object in your browser (typically Applet and Bean), and this display requires the Java plugin of the browser.</p> <p>When the JSP file is compiled, when sending to the browser, the <jsp: plugin> element will replace the "Object> or <Embed> element according to the version of the browser. Note, <Object> is used for HTML 4.0, <Embed> for HTML 3.2. In general, <JSP: plugin> element specifies the object is applet or bean, and also specifies the name of the Class, there is also location, Will specify where to download this Java plugin. The specification is as follows: Properties Type = "bean | applet". The type of plugin object to be executed, you must specify this bean or applet, because this property does not have a default. Code = "classfilename" will be executed by the Java plugin The name of Java Class must end with .class. This file must exist in the directory specified by the CodeBase property. CodeBase = "classfileDirectoryName" will be executed Directory (or path) of the java class file, if you do not provide this property, use the JSP file of <JSP: plugin> The directory will be used. Name = "instancename" this bean or applet instance name, it will call other places in JSP. Archive = "uritoarchive, ..." Some path names separate from comma, these path names Used to pre-install some Class, which will increase the performance of Applets. Align = "bottom | TOP | MIDDLE | LEFT | Right" graphic, object, Applet location, have the following values: bottom top middle left right height = " DisplayPixels "Width =" DisplayPixels "Applet or bean will display the long width value, this value is a number, the unit is pixel. Hspace =" Leftrightpixels "vSpace =" TopBottompixels "Applet or bean is displayed at the screen, up and down The space left, the unit is pixels. JREVERSION = "JreversionNumber | 1.1" Applet or Bean version of the Java Runtime Environment (JRE) required to run the desired version. The default is 1.1. NspluginURL = "URLTOPLUGIN" Netscape Navigator users can use JRE download address, this value is a standard URL, such as http://www.aspcn.com/jsp iepluginurl = "URLTOPLUGIN" IE user download address, this value is a standard URL, such as http : //www.aspcn.com/jsp <jsp: params> [<jsp: param name = "parametername" value = "{parameterValue | <% = express%>}" />] </ jsp: params> you The parameters or parameter values ​​that need to be transmitted to the applet or bean.</p> <p><JSP: Fallback> Text Message for User </ jsp: Fallback> A text is displayed to the user when the Java plugin cannot start. If the plugin can start and Applet or Bean cannot, the browser will have an error message pop-up. < jsp: setProperty> set property values ​​Bean in JSP syntax <jsp:. setProperty name = "beanInstanceName" {property = "*" | property = "propertyName" [param = "parameterName"] | property = "propertyName" value = " {String | <% = expression%>} "} /> example <jsp: setProperty name =" mybean "property =" * "/> <jsp: setProperty name =" mybean "property =" username "/> <jsp: SetProperty Name = "MyBean" property = "username" value = "steve" /> Description <JSP: setProperty> Element Using the setter method given by the bean, set one or more attribute values ​​in the bean. You must use the <JSP: UseBean> to declare this bean before using this element. Because, <JSP: UseBean> and <JSP: setProperty> are associated, and the name of the Bean instance they use should also match. (That is, the value of Name in <jsp: setProperty> should be the same as the value of id in <jsp: usebean> you can use <JSP: setProperty> to set the property value: input by the user All values ​​(in the Request object in parameter storage) to match the properties in the bean to match the specified values ​​of the user to match the attributes specified in the BEAN to match the property of the bean attributes each setting. The method of the attribute value has its specific syntax. Here we will explain the attribute and its usage name = "beaninstancename" indicates the name of the bean instance created in <JSP: UseBean>. Property = "*" Store users in JSP All values ​​input are used to match the properties in the bean.</p> <p>The name of the attribute in the bean is consistent with the parameter name in the Request object. The parameter value transmitted from the customer is generally the character type, which must be converted into other types in the bean. The type of bean property is listed in the table below and their conversion method. Transform strings into other types of methods. Property type method Boolean or boolean java.lang.Boolean.Valueof (string) Byte or byte java.lang. Byte.valueOf (String) char or Character java.lang.Character.valueOf (String) double or Double java.lang.Double.valueOf (String) integer or Integer java.lang.Integer.valueOf (String) float or float java. Lang.float.Valueof (String) long.valueof (String) If there is a nougant value in the parameter value of the Request object, the corresponding bean property will not set any values. Similarly, if there is an attribute in the bean without the corresponding request parameter value, then this attribute is also not set. Property = "PropertyName" [param = "parametername"] Use a parameter value in the request to specify bean A property value. In this syntax, Property specifies the bean's attribute name. PARAM specifies the parameter name in the request. If the bean property is different from the name of the request parameter, then you must specify Property and Param, if they are the same name, then you just need to specify Property is the line. If the value of the parameter is empty (or not initialized), the corresponding bean property is not set. Property = "PropertyName" value = "{string | <% = expression%>}" uses the specified value Set the bean property. This value can be a string or an expression. If this string, it will be converted to the type of the bean property (see the above table). If it is an expression, then its type must be consistent with the type of attribute value to be set. If the parameter value is empty, the corresponding attribute value will not be set.</p> <p>In addition, you can't use Param and Value tips in a <JSP: setProperty>. And specify its name and scope. JSP syntax <JSP: usebean id = "beaninstancename" scope = "Page | Request | session | Application" {class = "package.class" | type = "package.class" | class = "package.class" type = "package.class" | beanname = "{package.class | <% = expression%>}" type = "package.class"} {/> | Ersion} <} {/> |> Other elements </ jsp: usebean> } Example <jsp: usebean id = "cart" scope = "session" class = "session.carts" /> <jsp: setproperty name = "cart" property = "*" /> <jsp: usebean id = "checking" Scope = "session" class = "bank.checking"> <jsp: setProperty name = "checking" property = "balance" value = "0.0" /> <jsp: usebean> Description <JSP: Usebean> Used to position or Example A JavaBeans component. <JSP: Usebean> You will try to locate a bean instance if this bean does not exist, then <JSP: UseBean> is an example from a Class or template. For positioning or examples of a bean, <JSP: UseBean> will perform the following steps, the order is as follows: Try to locate a bean by a given name and scope. Reference variables for this bean object are named at the name you specify. If this bean is found This will be stored in this variable. If you also specify the type, this bean is also set to the corresponding type. If this bean is not found, it will be sampled from the class you specify and this reference is stored in a new variable. If the name of this class represents a template, this bean is sampled by java.beans.beans.instantiate. If <JSP: Usebean> has been sample (not positioning) Bean, <JSP: Usebean> and </ jsp: Usebean> There is element, then the code will be executed. The body of <JSP: UseBean> element is usually included with the <JSP: SetProperty> element for setting the properties of the bean.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-106905.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="106905" 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.048</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 = 'lE5aI_2BGl2YAKrsxsyNzvo0xjLBVgAPzkaBORDujg15D5Bu0lbojACo4DnQe3zruLl61oQECtpDDaVSYQY1X5BQ_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>