1. 程式人生 > >Declaring functions in JSP?

Declaring functions in JSP?

You need to enclose that in <%! %> as follows:

<%!

public String getQuarter(int i){
String quarter;
switch(i){
        case 1: quarter = "Winter";
        break;

        case 2: quarter = "Spring";
        break;

        case 3: quarter = "Summer I";
        break;

        case
4: quarter = "Summer II"; break; case 5: quarter = "Fall"; break; default: quarter = "ERROR"; } return quarter; } %>

You can then invoke the function within scriptlets or expressions:

<%
     out.print(getQuarter(4));
%>

or

<%= getQuarter
(17) %>