Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a java method in jsp

Tags:

I have a java class which performs some operations on files. Since the java code is huge I don't want to write this code in jsp. I want to call the methods in jsp whenever required.

Please tell me the path where I need to keep this file. Also some example code how to use it would be helpful.

like image 812
Hara Chaitanya Avatar asked Mar 01 '10 07:03

Hara Chaitanya


People also ask

Can we call a Java method from JSP?

JSP 2.0 has no syntax for calling a public nonstatic JavaBean method from a scriptless JSP page. This article solves that issue by providing a JSP 2.0 simple tag with dynamic attributes. Note: You can download this article's source code from Resources.

How can we call Java method from JSP using JSTL?

Simply create an object of the class using <jsp:useBean> and call the method using JavaServer Pages Standard Tag Library or Expression Language that is more easy to use and less error prone. Read more about Implicit Objects that might help you.


1 Answers

In the servlet (which runs before the JSP):

Person p = new Person(); // instantiate business object
p.init(...); // init it or something
request.setAttribute("person", p); // make it available to the template as 'person'

In the template you can use this:

your age is: ${person.age}  <%-- calls person.getAge() --%>
like image 50
cherouvim Avatar answered Sep 20 '22 08:09

cherouvim