Java function in jsp

Java Guides

Now, in the previous article, I mentioned that you wanted to minimize the scriptlets and declarations in a JSP.

You wanna avoid dumping thousands of lines of code in your JSP. Now, it’s okay to add small bits of script, small bits of declarations, but don’t overdo it.

So, in order to kinda help with this problem, you can refactor your code into a separate Java class or make use of MVC.

So the Java class will have all of our code, all of our business logic, and so on, and the JSP can simply make a call, let the Java code or the Java class do the heavy lifting, and then the JSP can get the results and continue on with its processing.

Development Steps

  1. Create a Java class
  2. Create a JSP page
  3. Call Java methods from Java class into JSP page.

1. Create a Java class — Calculator.java

I assume that you have already JSP application. Now you can create a Java class named Calculator.java and keep following code into it:

package net.javaguides.jsp.tutorial; public class Calculator < public int addition(int num1, int num2) < return (num1 + num2); > public int substraction(int num1, int num2) < return (num1 - num2); > public int multiplication(int num1, int num2) < return (num1 * num2); > public int division(int num1, int num2) < return (num1 / num2); > >

Create a JSP page — calculator.jsp

page import="net.javaguides.jsp.tutorial.Calculator"%>

Here is a complete example which is calling Calculator Java class methods and displaying results on brower.

page import="net.javaguides.jsp.tutorial.Calculator"%> page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> html> head> meta charset="ISO-8859-1"> title>Insert title heretitle> head> body>  Calculator calculator = new Calculator(); %> Addition of 20 + 10 =  calculator.addition(20, 10) %> br>br> Subtraction of 20 - 10 =  calculator.substraction(20, 10) %> br>br> Multiplication of 20 * 10 =  calculator.multiplication(20, 10) %> br>br> Division of 20/10 =  calculator.division(20, 10) %> body> html>

Источник

Java function in jsp

The JSP expression language allows you to define a function that can be invoked in an expression. Functions are defined using the same mechanisms as custom tags (see Using Custom Tags and Chapter 8, Custom Tags in JSP Pages).

At first glance, functions seem similar to method expressions, but they are different in the following ways:

  • Functions refer to static methods that return a value. Method expressions refer to non-static, arbitrary public methods on objects.
  • Functions are identified statically at translation time, whereas methods are identified dynamically at runtime.
  • Function parameters and invocations are specified as part of an EL expression. A method expression only identifies a particular method. The invocation of that method is not specified by the EL expression; rather, it is specified in the tag attribute definition of the attribute using the method expression, as described in Defining a Tag Attribute Type.

Using Functions

Functions can appear in static text and tag attribute values.

To 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.

For example, the date example page index.jsp imports the /functions library and invokes the function equals in an expression:

In this example, the expression referencing the function is using immediate evaluation syntax. A page author can also use deferred evaluation syntax to reference a function in an expression, assuming that the attribute that is referencing the function can accept deferred expressions.

If an attribute references a function with a deferred expression then the function is not invoked immediately; rather, it is invoked whenever the underlying technology using the function determines it should be invoked.

Defining Functions

To define a function, program it as a public static method in a public class. The mypkg.MyLocales class in the date example defines a function that tests the equality of two Strings as follows:

package mypkg; public class MyLocales < . public static boolean equals( String l1, String l2 ) < return l1.equals(l2); >>

Then map the function name as used in the EL expression to the defining class and function signature in a TLD (see Chapter 8, Custom Tags in JSP Pages). The following functions.tld file in the date example maps the equals function to the class containing the implementation of the function equals and the signature of the function:

 equals mypkg.MyLocales boolean equals( java.lang.String, java.lang.String ) 

No two functions within a tag library can have the same name.

Источник

Читайте также:  What is application context in java web application
Оцените статью