有没有办法从 JSP 调用 Java class with main() 并在控制台或 JSP 页面中打印值

Is there a way to call Java class with main() from JSP and print the value in the console or JSP page

我有疑问:

  1. 是否可以在 JSP 中使用 main() 调用 Java class 并在控制台或 JSP 页面中打印值(不使用 Servlet class)?

  2. 类似地使用 main() 打印 Java class 中 JSP 页面中的值(不使用 Servlet class)?

需要一些解释。

Is it possible to call a Java class with main() in JSP and print the value in console or JSP page (without use of a Servlet class)?

Similarly print the value in JSP page from Java class with main() (without use of a Servlet class)?

任何破解都是可能的,但 ServletJSPJSTL 最适合这里

Checkout tutorial

由于典型的 main() 方法具有 return 类型 void,因此不能这样做:

public staic void main(String[] args) { ... }

但是您可以在 class 和 return 上调用任何静态方法并将其输出到您的 JSP:

Class

public class Util {
  public static String doSomething() {
    // do something and generate a String
    return "helloWord";
  }
}

JSP:

<%= Util.doSomething() %>

这会在包含 JSP 输出标记的地方打印出静态 doSomething() 方法的 return 值。