Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring functions in JSP?

Tags:

function

jsp

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; 
like image 562
Nathan H Avatar asked May 05 '09 21:05

Nathan H


People also ask

Can we write functions in JSP?

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.

How do you call a function in JSP?

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.

What is declaration in JSP?

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 %>

Which is used to write declaration in JSP?

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.

What is JSP declaration in JSP?

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.

How do you declare a method in JSP?

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.

How to use function in JSP expression language?

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.

What is syntax in JSP development?

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.


1 Answers

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) %> 
like image 118
karim79 Avatar answered Oct 06 '22 06:10

karim79