问题描述
我有一个疑问:
-
是否可以在JSP中用main()调用Java类并在控制台或JSP页面中打印值(不使用Servlet类)?
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)?
是否使用main()从Java类中打印JSP页面中的值(不使用Servlet类)?
Similarly print the value in JSP page from Java class with main() (without use of a Servlet class)?
请提供一些解释.
推荐答案
由于典型的main()方法的返回类型为void
,因此无法完成此操作:
Since a typical main() method has return type void
, this cannot be done:
public staic void main(String[] args) { ... }
但是您可以在该类上调用任何静态方法,并返回一个String并将其输出到您的JSP:
But you call any static method on that class and return a String and output that to your JSP:
课程
public class Util {
public static String doSomething() {
// do something and generate a String
return "helloWord";
}
}
JSP :
<%= Util.doSomething() %>
这将打印出包含JSP输出标记的静态doSomething()
方法的返回值.
this prints out the return value of your static doSomething()
method at where the JSP output tag is included.
这篇关于有没有一种方法可以从JSP使用main()调用Java类并在控制台或JSP页面中打印值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!