Using Java Methods in JSP Example


Note: It is advised to go through "JSP Scriptlet, Expression and Declaration – all in one example" to know their scope and usage in JSP coding before proceeding this topic.

We know earlier, JSP Declaration represents a global area for the whole JSP file where programmer can declare variables and methods that can be used throughout JSP code. That is, global variables and methods are declared in Declaration tag. Let us see one example and then discuss small problems that may arise while using methods.

Example on Java Methods JSP

A simple client HTML file that takes client’s name.

File Name: LearningMethods.html

Using Java Methods in JSP

Enter Your Name

This JSP uses all the four – Scriptlets, Expressions, Declaration and Methods.

JSP File Name: UsingMethods.jsp

 This JSP shows how to use Java method in JSP  

<%! // declaration used for String str; // declaring variable and method public int calculate(int x, int y) // Java Methods JSP calculate() { return x * y; } %> <% str = request.getParameter("t1"); %> Scriptlet using str and method
<% out.println("Client name is: " + str); // printing client name int k = calculate(10, 20); // calling method calculate() out.println("
Product of two numbers is: " + k); %>

First expression using str

Name of the client: <%= str %>

Second expression using method
Two numbers product is <%= calculate(10, 20) %>

HTML file when value is entered.

Java Methods JSP

Response of JSP

Java Methods JSP

getParameter() is the method of request, one of the 9 implicit objects, JSP supports.

Method calculate() is declared in Declaration and used from Scriptlet and Expression. Declaration represents global area.

<%!				
  String str;			      

   public int calculate(int x, int y)
   {
     return x * y;
   }
%>

The above single Declaration can be split into two Declarations also.

<%!				
  String str;			      
%>

and

<%!				
   public int calculate(int x, int y)
   {
     return x * y;
   }
%>

1. Can we declare a method like this?

<%!				
   public int calculate(int x, int y)
   {
     out.println("Hello 1
"); String clientName = request.getParameter("t1"); return x * y; } %>

No we cannot use. It is compilation error. The reason is scope problem of out and request objects. Read the following note.

Important Note: The code of Scriptlet and Expression is placed in the service() method of the generated Servlet. That is, the implicit objects out, request and response are local to Scriptlet and Expression. These three objects can be used by the Scriptlet and Expression straightaway. But Declaration code is placed outside the service() method of the generated Servlet. For this reason, Declaration code cannot use these three objects.

All the 9 implicit objects are created by the container in the service() method of the generated Servlet. As Scriptlet and Expression code is placed in service() method, Scriptlet and Expression can use out, request and response without any problem. But as Declaration code is placed outside the service(), the scope of request, response and out is not available to declaration.

This is very important to note in coding, else, compiler raises error.

2. Then, how to use out and request objects (which are local to service() method) from Declaration?

Redesign the calculate method like this.

<%!				
   public int calculate(int x, int y, JspWriter jw, HttpServletRequest r1) throws Exception
   {
     jw.println("Hello 1
"); String clientName = r1.getParameter("t1"); return x * y; } %>

Now it is correct declaration. Let us call the above method from a Scriptlet.

<%
   int k = calculate(10, 20, out, request);
   out.println(k);
%>

Now everything works fine. We are passing local out and request objects from Scriptlet to the method declaration.

This scope problem should always be taken care of by the Programmer.

Leave a Comment

Your email address will not be published.