Instantiating classes between jsp scriptlets

2019-09-06 12:31发布

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

2条回答
老娘就宠你
2楼-- · 2019-09-06 12:49

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

<%@ page import="sample.Foo" %>
查看更多
爷的心禁止访问
3楼-- · 2019-09-06 12:50

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();
%>
查看更多
登录 后发表回答