JSP script elements, commands, and predefined Variables

Source: Internet
Author: User

12.1 jsp (preferred for SUN Enterprise Applications) script elements

Jsp (preferred for SUN Enterprise Applications) script elements are used to insert Java code, which will appear in the Servlet generated by the current jsp (preferred for SUN Enterprise Applications) page. There are three types of script elements:

Expression format <% = expression %>: calculates the expression and outputs its results.
Scriptlet format <% code %>: inserts the code into the Servlet service method.
Declaration format <%! Code %>: add the declaration to the Servlet class (except any method ).
The following describes their usage in detail.

12.1.1 jsp (preferred for SUN Enterprise Applications) Expressions

Jsp (preferred for SUN Enterprise Applications) expressions are used to insert Java data directly to the output. The syntax is as follows:
<% = Java Expression %>



The result obtained by calculating the Java expression is converted to a string and inserted to the page. Computing is performed at runtime (when the page is requested), so you can access all the information related to the request. For example, the following code shows the date/time requested by the page:
Current time: <% = new java. util. Date () %>



To simplify these expressions, jsp (the preferred choice for SUN Enterprise Applications) predefines a set of object variables that can be used directly. These implicitly declared objects will be described in detail later, but for jsp (preferred for SUN Enterprise Applications) expressions, the most important objects and their types are as follows:

Request: HttpServletRequest;
Response: HttpServletResponse;
Session: The HttpSession associated with the request
Out: PrintWriter (buffer-based version, jsp (preferred for SUN Enterprise Applications) Writer), used to send the output to the client
The following is an example:
Your hostname: <% = request. getRemoteHost () %>



Finally, if you use xml (standardization is getting closer and closer), jsp (preferred for SUN Enterprise Applications) expressions can also be written in the following form:
<Jsp (preferred for SUN Enterprise Applications): expression>
Java Expression
</Jsp (preferred for SUN Enterprise Applications): expression>



Remember that xml (standardization is getting closer and closer) elements are different from HTML elements. Xml (standardization is getting closer and closer) is case sensitive, so it must be in lower case. For more information about the xml (standardization is getting closer and closer) syntax, see the xml (standardization is getting closer and closer) tutorial.

12.1.2 jsp (preferred for SUN Enterprise Applications) Scriptlet

If you want to complete a task more complex than inserting a simple expression, you can use jsp (preferred for SUN Enterprise Applications) Scriptlet. Jsp (preferred for SUN Enterprise Applications) Scriptlet allows you to insert arbitrary Java code into Servlet. Jsp (preferred for SUN Enterprise Applications) Scriptlet syntax is as follows:
<% Java Code %>



Like jsp (preferred for SUN Enterprise Applications) 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 Scriptlet will be copied to the Servlet, and the static HTML (template text) before and after the Scriptlet will be converted to the println statement. This means that the Java statements in the Scriptlet are not necessarily complete. blocks that are not closed will affect static HTML outside the Scriptlet. For example, the following jsp (preferred for SUN enterprise-level applications) piece combines template text with Scriptlet:
<% If (Math. random () <0.5) {%>
Have a <B> nice </B> day!
<%} Else {%>
Have a <B> lousy </B> day!
<% }%>



The preceding jsp (preferred for SUN Enterprise Applications) code will be converted into the following Servlet code:
If (Math. random () <0.5 ){
Out. println ("Have a <B> nice </B> day! ");
} Else {
Out. println ("Have a <B> lousy </B> day! ");
}



To use the character "%>" inside the Scriptlet, you must write it as "% \> ". In addition, note that the equivalent expression of <% code %> xml (standardization is getting closer and closer) is:
<Jsp (preferred for SUN Enterprise Applications): scriptlet>
Code
</Jsp (preferred for SUN Enterprise Applications): scriptlet>



12.1.3 jsp (first choice for SUN Enterprise Applications) Statement

The jsp (preferred for SUN Enterprise Applications) Declaration defines the methods and member variables for inserting Servlet classes. Its syntax is as follows:
<%! Java Code %>



Since the Declaration does not have any output, they are often used together with jsp (preferred for SUN Enterprise Applications) expressions or Scriptlet. For example, the following jsp (preferred for SUN Enterprise Applications) code snippet outputs the number of requests on the current page since the server is started (or the Servlet class has been changed and reloaded:
<%! Private int access (favorite of small websites) Count = 0; %>
Page access times since the server is started:
<% = ++ Access (favorite of small websites) Count %>



Like Scriptlet, to use the string "%>", replace it with "% \>. Last, <%! The equivalent expression of code %> xml (standardization is getting closer and closer) is:
<Jsp (preferred for SUN Enterprise Applications): declaration>
Code
</Jsp (preferred for SUN Enterprise Applications): declaration>



12.2 jsp (preferred for SUN Enterprise Applications) commands

Jsp (preferred for SUN Enterprise Applications) commands affect the overall structure of Servlet classes. Its syntax is generally as follows:
<% @ Directive attribute = "value" %>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.