4 basic syntax for JSP

Source: Internet
Author: User

1. JSP annotations

2. JSP Declaration

3. JSP expression

4. JSP Script

JSP Comments:

Comment Format:

<%--Comment Content--%>

Note that the comments of the JSP are not exported to HTML.

JSP declaration:

JSP declarations are used to declare variables and methods. Declaring a method in a JSP declaration looks special, and it seems that you can define a method without defining a class, and that the method seems to exist independently from the class.

In fact, the JSP declaration will be converted into a member variable or method of the corresponding Servlet.

Declaration syntax: (using the <%! ...%> "This format, the first percent sign after a"! ")

<%!//declares an integer variable public int count;//declares a method public String info () {    return "Browse Times:" + count;} %>

  

JSP expression:

The output expression cannot be followed by a semicolon <%= expression%><%=count++%><%=info ()%>

Open multiple browsers, we will find that the value of the count variable is continuous because the JSP page is compiled into a Servlet instance,
Each Servlet has only one instance in the container; the variable declared in the JSP is a member variable, and the member variable is initialized only when the instance is created.
The value of the variable is persisted until the instance is destroyed.

JSP script:

In general, all executable Java code can be embedded in an HTML page through a JSP script.

For example, output a list

<ul>    <% for    (int i = 0; i < 3; i++) {    %>        <li><%=i%></li>    <%    }    %></ul>

  

We can look at the compiled Java code:

for (int i = 0; i < 3; i++) {          out.write ("\ n");      Out.write ("        <li>");      Out.print (i);      Out.write ("</li>\n");      Out.write ("    ");}

The above code fragment is located in the Servlet's _jspservice method.

That is, any Java code that we write in the JSP script tag will appear as-is in the Servlet's _jspservice method.

Also, because the Java code within the JSP scripting Syntax (<%.%>) tag will appear inside the method, it means that we cannot use keywords such as public, and if I want to declare it, use the declarative syntax.

Because JSP scripts can place any executable statement, you can take advantage of the Java language's capabilities, such as connecting to a database.

4 basic syntax for JSP

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.