I come from PHP world, 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;
Using FunctionsTo use a function in a JSP page, you use a taglib directive to import the tag library containing the function. Then you preface the function invocation with the prefix declared in the directive. In this example, the expression referencing the function is using immediate evaluation syntax.
No you cannot call that JSP magically from JS. However you can send an Ajax request or post the form to jsp. BTW, I strongly suggest you to move the java code to a servlet and use it.
A JSP declaration is used to declare variables and methods in a page's scripting language. The syntax for a declaration is as follows: <%! scripting-language-declaration %>
Declaration tag is one of the scripting elements in JSP. This Tag is used for declare the variables. Along with this, Declaration Tag can also declare method and classes.
JSP Declaration A declaration tag is a piece of Java code for declaring variables, methods and classes. If we declare a variable or method inside declaration tag it means that the declaration is made inside the servlet class but outside the service method.
Jsp Declaration Tag. The jsp scriptlet tag can only declare variables not methods. The jsp declaration tag can declare variables as well as methods. The declaration of scriptlet tag is placed inside the _jspService() method. The declaration of jsp declaration tag is placed outside the _jspService() method.
To use a function from any tag library (standard or custom), you must install that library on your server and must include the library in your JSP using the <taglib> directive as explained in the JSTL chapter. The JSP expression language supports the following implicit objects − You can use these objects in an expression as if they were variables.
We will understand the basic use of simple syntax (i.e, elements) involved with JSP development. A scriptlet can contain any number of JAVA language statements, variable or method declarations, or expressions that are valid in the page scripting language. Any text, HTML tags, or JSP elements you write must be outside the scriptlet.
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) %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With