Define the method function in the JSP page

Let’s learn the basics first:

1. The essence of JSP is a Servlet. Before running, JSP will be translated into .java file by Tomcat server, and then the .java text will be compiled into .class file. When we visit JSP, the request is processed by the translation. the type.

2. <% %> is called a script fragment. The content of the code written in it will be translated in the doservice method of the Servlet. Obviously we can define local variables or call other methods in the doservice method, but we cannot define other methods and functions in the Service. , That is, we can define local variables or call methods in <%%>, but we cannot define methods . There can be multiple script fragments in the jsp page, but the structure between multiple script fragments must be complete.

3. <%!%> is called a declaration, the content written in it will be directly translated in the Servlet class, because we can define methods and attributes and global variables in the Servlet class, so we can declare in <%!%> Method functions, attributes, global variables.

4. <%=%> is called a jsp expression, which is used to output the declared variable or expression to the html page.

5. The code written directly in the <body></body> of the jsp page is called a template element, and will be used as the output content in the out.write() in the doservice method of the Servlet. So output content to the page in JSP can use out.write() or out.println()

The syntax of JSP definition method function:

<%!
修饰符 返回值 函数名(参数){
    函数体....
}
%>

1. Example:

<%!
public String test(){
   return "测试"; 
}
%>

2. Call:

<%
String msg=test();
out.println(msg);
%>

 

Guess you like

Origin blog.csdn.net/qq15577969/article/details/112808267