Differences between JSP declarations, expressions, scripts, and comments

statement

  In a JSP page, one or more legal variables and methods can be declared. The declared variables and methods can be used anywhere in the JSP page and will be initialized when the JSP page is initialized.

  The syntax format is as follows:

  <!%declaration; [declaration;] ...%>

Note when declaring:

  1. Declarations start with "<%!" and end with "%>",  "%!" must be followed by
  2.  
  3. The naming rules for variables and methods should be the same as Java's naming rules
  4. The declared variables and methods contained in "<@page %>" can be used directly without redeclaring them
  5. A declaration is only valid on one page. If you want to use some declarations for each page, you can write them into a separate file and include them with the "<%@include%>" directive or the "<jsp:include>" action

E.g :

<%! int i = 0;%>
<%! int x,y,z;%>
<%! String str="Awesome, my brother";%>
<%! Date date = new Date();%>

expression

  The expression in JSP can convert the data into a string and output it directly on the web page, or generate a dynamic link address, or dynamically specify the From form processing page

  The syntax format is as follows:

  <%=expression%>

requires attention :

  1. No semicolons in JSP expressions!
  2. No spaces between "%="

E.g :

<%! String path="http://www.cnblogs.com/yuanmiemie/";%>
    <a href="<%=path%>">Click to jump</a>
    <form action="<%=path%>">
        <input type="submit" value="Click to jump"/>
    </form>

script

  Script is Scriptlet, that is, the code part in JSP. It is a piece of Java code, which can use almost any Java syntax. It is executed at the request time and can use the variables, methods, expressions or JavaBeans defined by the JSP page. Script Definition The variables and methods are valid in the entire current page, but will not be shared by other threads. The user's action on the variable will not affect other users. When the page where the variable is located is closed, the variable will be destroyed.

  The syntax format is as follows:

  <%scriptlet%>

E.g :

<%! String str = "Reading for the rise of China";%>
<%
    if(true){
%>
        <%=str%>
<%
    }
%>

Notes

The annotations of JSP programs include hidden annotations, HTML annotations, and Java language annotations.

  Hidden comments are standard comments in JSP. In writing JSP programs, they are completely ignored when publishing web pages and are not sent to the client.

  The syntax format is as follows:

  <%-- comment --%> comment is the commented content

  HTML comments HTML comments can be seen in the browser file window when publishing web pages. In this comment, JSP expressions can also be used.

  The syntax format is as follows:

  <!-- comment [<%=expression%>]--> comment is the commented content

  Java comments are similar to hidden comments, and the comment content cannot be seen in the source file window of the browser

  The syntax is as follows:

  <%// single line comment%>

  <%/* 多行注释 */%>

  <%/** 文档注释 */%>

例如 :

<!-- HTML注释: 这一行会在源文件中显示 -->
<%-- 隐藏注释: 这一行不会在源文件中显示 --%>
<% //Java 单行注释 : 这一行不会在源文件中显示 %>
<% /*Java 多行注释 : 这些行不会在
                     源文件中显示*/%>
<% /**Java 文档注释 : 这些行不会在
                      源文件中显示*/%>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324739038&siteId=291194637