Script element of Jsp page element

     The scripting language of Jsp is the Java language. The use of jsp is similar to HTML and Java code segments. The success of Jsp lies in the encapsulation of dynamic code , such as the use of Jsp elements such as instruction tags, action tags, and built-in objects, to achieve page display and data. separation of processing.
        Jsp element classification:
        1) Script element: Script element refers to the Java code embedded in the Jsp page;
        2) Instruction element: The instruction element is designed for the JSP engine, and it controls how the JSP engine processes the code;
        3) Action element: The action element mainly connects the components to be used, and can also control the action of the JSP engine;


        in order to increase the readability of the program, we generally add certain comments to the JSP page.


        Well, let's get to the point:

        JSP script element

        JSP script is used to insert Java code that will appear in the servlet generated by the current JSP page.
        Jsp pages can be static classes, directives, expressions, scripts, declarations, annotation actions, and comments, where expressions, scriptlets, and declarations are collectively referred to as script elements.
        All script elements start with <% and end with %> To distinguish between expressions, scripts, declarations, declarations use "!" Expressions use "=" while scripts do not use any symbols.

        1. Declaration <%! declaration%>

        

          Jsp declarations are used to define one or more variables or methods. The declarations do not have any output. They are generally used in conjunction with scripts. Multiple declarations can be inserted into a Jsp page, and multiple Java declarations can be inserted into one declaration.

<%!
      String userName="yang"
      String password="yang123"
%>
(1), the declaration of the method
        
<%!
   public static int tt(int n){
      if(n==0){
        return 0;
      }else if(n==1){
        return 1;
      }else{
         return tt(n-2)+(n-1)
      }
   }
%>

(2) Class declaration The declaration class is the inner class of the Servlet class corresponding to the Jsp page, and all script elements on the page can create objects of this class
 
 
<%!     public class square{     double r;     square(double r){
        this.r=r;     }
    double getArea(){         return r*r;     }     double getLength(){         return r*4;     }     } %>

2. Expression<%=expression%>

        JSP expressions are used to output Java data to the page, the container will convert the results obtained by the Java expressions into strings, and then insert them into the page

<body>
   Current time:<%=new java.util.Date()%>
</body>
All expressions, simple or complex, are evaluated as a single result or value. JSP pages rely on the JavaWriter object to output Jsp expressions, which can accept any java expression result and convert it to String type and output it to the response buffer.
3. Script program <%scriptlet%>


Scripts are arbitrary Java code segments. If Java code is required to achieve more complex operations and control, JSP scripts can insert arbitrary Java code into Servlet

Guess you like

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