JSP calls java method: first introduce the package where the Java method is located in the JSP page; then write [<%int result="Dy.Sub(5,4"><% in the page =result>] and save and run.
is actually very simple, because Java programs can be written directly in jsp. So the method of calling Java only needs a little change.
Recommended course: Java Tutorial.
Let’s take a look at an example:
Method written in JAVA
package doc; //定义一个包 public class Dy { //定义一个类 public static int Sub(int x,int y){ //定义函数Sub()为静态函数 return x-y; } }
JSP page to call
First introduce the class to be used
<%@ page import="doc.*"%> //引入所需要的函数所在的包,如果没有导入使用包,系统则认为所引用的类与当前类在同一个包中
Then Write the code in :
<% int a=5; int b=4; int result=0; %> <% result=Dy.Sub(a, b); //调用定义的方法 System.out.println(result); %> <%=result %>
Run the JSP page
Enter http://localhost:8080 in the browser address bar /Test/index.jsp
The page displays the results
1
Function call successful!
PS: Function calling method: class name. function name ()
The above is the detailed content of How to call java method in jsp. For more information, please follow other related articles on the PHP Chinese website!