Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling functions in JSp

 
Mary Cole
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can anybody tell me how to call functions in JSP's.
Pl reply with an example
Thanx in advance
 
John Wetherbie
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you mean calling a bean function vs just having some Java code in your JSP or using a custom tag. I think it looks something like this:
Let the JSP know about the bean:
<jsp::useBean id="hello" class="com.blah.blah.HelloBean"/>
If you had a get (or a set) function like getName:
<jsp:getProperty name="hello" property="name"/>
Or you can do something that looks a little more like traditional Java:
<%@ page import="javax.servlet.jsp.JspFactory" %>
<% JspFactory factory = JspFactory.getDefaultFactory(); %>
These examples are from Web Development with JavaServer Pages by Fields & Kolb.
John
 
Mary Cole
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,
I want to call a java function which will be used repeatedly in a particular JSP
For ex-
Lemme say that I want to add 2 numbers. For that I want to write a function as I don't want to repat my code again and again is say add.jsp
How do you write a function and call it multiple times in a single JSP( don't mistake it for beans function).
Hope you got my point
 
Madhu Juneja
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can define a function say add in a declaration in a .jsp file as follows :
<%! public int add (int a, int b) {}<br /> %>
And then call this function wherever you require in a scriplet / expression as :
<%= add (5,6) %>
Having a function called add.jsp is confusing ...
 
Madhu Juneja
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can define a function say add in a declaration in a .jsp file as follows :
<%! public int add (int a, int b) {}<br /> %>
And then call this function wherever you require in a scriplet / expression as :
<%= add (5,6) %>
Having a function called add.jsp is confusing ...
 
Madhu Juneja
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You can define a function say add in a declaration in a .jsp file as follows

And then call this function wherever you require in a scriplet / expression as

Having a function called add.jsp is confusing ...
 
Madhu Juneja
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Click on the reply hyper link. The code is displayed
 
Mary Cole
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanx a lot
 
ismail ismail
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if the function returns and object:
ConnectionClass getConnectio()
{
ConnectionClass conn = new ConnectionClass();
// the open connection code here;
return conn;
}
Later in the JSP page can I create a connection object and assign it the value returned from getConnection().
Thanks for your help;
 
Matt Midcap
Sheriff
Posts: 440
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm moving this to our "Servlets/JSP" forum where it is more appropriate.
Matt
[This message has been edited by Matt Midcap (edited June 29, 2001).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!