Function declaration in JSP?

I came from the world of PHP, where declaring a function in the middle of a php page is pretty simple. I tried to do the same in JSP:

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; } 

I get the following error:

 An error occurred at line: 20 in the jsp file: /headers.jsp Illegal modifier for the variable getQuarter; only final is permitted return; 
+59
function jsp
May 05 '09 at 9:13 p.m.
source share
2 answers

You need to conclude 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; } %> 

Then you can call the function in scripts or expressions:

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

or

 <%= getQuarter(17) %> 
+100
May 05 '09 at
source share

You fixed your code in <%! %>

 <%! public void search() { } %> 
0
Jan 29 '19 at 17:14
source share



All Articles