Instantiating classes between jsp scriptlets

被刻印的时光 ゝ 提交于 2019-12-25 08:48:23

问题


Is it possible to instantiate a class and then invoke its methods between scriptlets in JSP? I am getting errors and I don't know why (java class and methods are fine). Any other way to do the same (i just want a string from a method in a class)?


回答1:


Yes of-course. You can create objects for your classes and access their methods between scriptlets in JSP like this.

<%
     Foo foo = new Foo();
     foo.method1();
%>

Another way of doing this is using useBeans of jsp to instantiate the class and access its method in scriptlet

<jsp:useBean id = "foo" class = "Foo" />
<%
     foo.method1();
%>



回答2:


You must include the page import in the header like so:

<%@ page import="sample.Foo" %>


来源:https://stackoverflow.com/questions/2889018/instantiating-classes-between-jsp-scriptlets

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