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

假装没事ソ 提交于 2019-12-25 18:49:29

问题


I have a doubt:

  1. 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)?

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

please need some explanation.


回答1:


Since a typical main() method has return type void, this cannot be done:

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

But you call any static method on that class and return a String and output that to your JSP:

Class

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

JSP:

<%= Util.doSomething() %>

this prints out the return value of your static doSomething() method at where the JSP output tag is included.




回答2:


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)?

Any hack is possible, but Servlet, JSP, and JSTL is best suited here

Checkout tutorial



来源:https://stackoverflow.com/questions/29085976/is-there-a-way-to-call-java-class-with-main-from-jsp-and-print-the-value-in-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!