开发者

Instantiating classes between jsp scriptlets

开发者 https://www.devze.com 2022-12-31 21:38 出处:网络
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).

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 met开发者_C百科hod in a class)?


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();
%>


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

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号