| Java FAQ | ||
| JSP FAQ | ||
| Servlet FAQ | ||
XyzWs JSP FAQ:
How to define Java mehtods in JSP pages?
Printer-friendly version |
Mail this to a friend
|
Advertisement
|
How to define Java mehtods in JSP pages?Each JSP compiles to a separate class, so there are classes involved. If you are reusing the code chunks in the same JSP, then you can declare a method to appear in the JSP class. The JSP Declarations are used to declare variables and methods in the scripting language used in a JSP page. A declaration should be a complete declarative statement, or sequence thereof according to the syntax of the scripting language specified. The JSP declaration syntax is: <%! declaration(s) %>
For example,
<%! int x=0; } %>
<%! public void f(int i) { if (i<3) out.println(?...?); } %>
You don't get any direct handles to the implicit objects like
<%!int foo(){out.println("--- message in declaration ---"); int i = 10; return i;}%>
You will get this error:
Undefined variable or class name: out
out.println("--- message in declaration ---")
To work arround this problem, you need to pass
<%!int foo(Writer out){out.println("--- message in declaration ---"); int i = 10; return i;}%>
and then would invoke it within your JSP body as <% foo(out) %> |