• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

can i call a function of servlet using jstl

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: <c:out value="${head.key}"/><br>
values:
<c:forEach var="val" items="${head.value}">
<c:out value="${val}"/>
</c:forEach>
<br />

</c:forEach>
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vishal Ashtaputre
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlet's method is as follows :


public ArrayList getReply(){

ArrayList ar = new ArrayList();

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

return ar;

}
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!