问题
I have a doubt:
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)?
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