JSP expressions, scripts, and declarations

Source: Internet
Author: User

JSP script Element
JSP script elements can be used to embed Java code into JSP pages. These Java code will appear in the Servlet generated by the current JSP page, so that JSP can separate static content from dynamic content. Script elements include:

1. Expression
Expression: indicates the data. The system calculates the data as a value.
Syntax: <% = expression %>
Example: <% = user. getName () %>
Expression essence: after converting a JSP page into a Servlet, use out. print () to output the expression value. In this case, if the returned value of user. getName () is "liky", the servlet will actually convert it to out. print ("liky ");
Therefore, pay attention to the following two points:
1) if the expression is to call a method, the method must have a return value instead of void, that is, a method such as void getName () cannot be called.
2) The method cannot be followed by a semicolon. For example, <% = getName (); %> is not allowed.
 
2. Script
Script: embed the Java code in <%>. The Java code here is no different from the general Java code. Therefore, each statement must end, this is different from the expression.
Syntax: <% code %>
Script essence: inserts the code into the Servlet service method.
For example:
<%
If (user! = Null ){
%>
Hello <B> <% = user %> </B>
<%
} Else {
%>
You havent login!
<%
}
%>
Translated:
If (user! = Null ){
Out. println ("Hello <B>" user "</B> ");
} Else {
Out. println ("You havent login! ");
}
 
3. Statement
Declaration: allows users to define variables and methods in Servlet.
Syntax: <%! Code %> example: <! String getName () {return name;}>
Declaration essence: in fact, the declared variables are added to the Servlet class (outside of any method), and the method becomes the Servlet method.

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.