This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes JSP and the fly likes can i call a function of servlet using jstl Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "can i call a function of servlet using jstl" Watch "can i call a function of servlet using jstl" New topic
Author

can i call a function of servlet using jstl

Vishal Ashtaputre
Greenhorn

Joined: Nov 18, 2006
Posts: 14
I want to call a function using jstl.
This function is in my servlet. can i do this. if yes how.

Hav a look at following which is in my jsp

<c:forEach var="head" items="${getReply}" <<<<< I want to call a function here instead of getReply >>>>>>> >
param: <cut value="${head.key}"/><br>
values:
<c:forEach var="val" items="${head.value}">
<cut value="${val}"/>
</c:forEach>
<br />

</c:forEach>


SCJP 1.4 , SCWCD 5.0
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Short answer, no.

There are no functions in servlets (or Java classes for that matter), there are methods but you wouldn't call a servlet's methods from a JSP or another servlet (leaving inheritance out for simplicity's sake).

In a well written app with scriptless JSPs these method calls would be performed before forwarding to a JSP. The results would be stored in either a bean's property or in a list or map bound to one of the scope objects.

Tell us what the servlet's method would do and return and someone will probably be able to show you a better way to do what you want to accomplish.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Vishal Ashtaputre
Greenhorn

Joined: Nov 18, 2006
Posts: 14
Servlet's method is as follows :


public ArrayList getReply(){

ArrayList ar = new ArrayList();

ar.add("xyz");
ar.add("abc");

return ar;

}
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

OK, the method itself is fine. It conforms to the javabean property syntax.

The problem is that your JSPs can't access methods in servlets.
What you will want to do, is create a Java bean, add that method to it, bind it to one of the scope objects (request, unless you need to keep it around longer) and then forward to your JSP. The JSP will then be able to read that property using EL.

If you'd like an example of this, the SimpleMVC example in our codebarn does this; not with ArrayLists, but the concept is the same.
http://www.javaranch.com/codebarn/codebarn-servlets-simplemvc.jsp

If you want an example that does use ArrayLists, look at the SessionMonitor demo app on my site:
http://simple.souther.us

It has a servlet that reads tabular information, places it in a bean as nested ArrayLists, and forwards to a JSP which uses EL and JSTL to read from the bean and iterate over the lists to build the table.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: can i call a function of servlet using jstl