GeeCON Prague 2014*
The moose likes JSP and the fly likes JSTL forEach tag taking longer time than scriplets ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "JSTL forEach tag taking longer time than scriplets ???" Watch "JSTL forEach tag taking longer time than scriplets ???" New topic
Author

JSTL forEach tag taking longer time than scriplets ???

Karthik Kottur
Greenhorn

Joined: Oct 19, 2002
Posts: 7
Hi Gurus,

My STRUTS action class setsup a collection object [roughly 900 + records stored as java beans] to be displayed on my view [JSP]. The problem is, when I iterate over this collection in JSP using <c:forEach> tag, it takes around 4-5 minutes to build up the page.

I coded the same looping logic through java scriplets embedded within JSP and the processing came down to 1 minute. Should I assume that <c:forEach> tag will have a processing overhead over plain java code in JSP?

FYI, I checked the server generated SERVLET code under WORK directory of TOMCAT for this JSP with <c:forEach> tag and find lot of code being generated for this <c:forEach> tag implementation. Could it be that the processing of these generated code takes longer than plain java using scriplets?

Please Help.

Thanks,
Karthik.

<b>JSP using <c:forEach></b>

<c:forEach var="refurb" items="${BOMDataForm.refurbSummary}" varStatus="loopStatus">
<tr valign="top" bgcolor="#FFFFFF">
<td>
<div align="center"><span class="modulecontent"><c ut value="${refurb.configName}" /></span></div>
</td>
<td>
<div align="center"><span class="modulecontent"><c ut value="${refurb.itemName}" /></span></div>
</td>
</tr>
</c:forEach>

<b>JSP using embedded scriplets</b>

<%
BOMDataForm bomDataForm = (BOMDataForm) request.getAttribute("BOMDataForm");
ArrayList arrayList = (ArrayList) bomDataForm.getRefurbSummary();
int loop = arrayList.size();
for (int i=0;i<loop;i++) {
out.println("<tr valign=\"top\" bgcolor=\"#FFFFFF\">");
out.println("<td>");
out.println("<div align=\"center\"><span class=\"modulecontent\">");
out.println(((RefurbSearchResult)arrayList.get(i)).getConfigName());
out.println("</span></div>");
out.println("</td>");
out.println("<td>");
out.println("<div align=\"center\"><span class=\"modulecontent\">");
out.println(((RefurbSearchResult)arrayList.get(i)).getItemName());
out.println("</span></div>");
out.println("</td>");
out.println("</tr>");
}
%>
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
You're trading a potential small drop in performance for a massive increase in maintainability and easier development.
That's a slightly increased hardware spec for the app for a large decrease in development time and cost.

Having created over a hundred JSTL based pages I can tell you the performance of them is excellent.
What takes slightly longer is compilation but that happens only when you change the page on the server, something that won't happen often on a development machine.


42
Karthik Kottur
Greenhorn

Joined: Oct 19, 2002
Posts: 7
Dear Mr. Wenting,

I completely understand your point about code maintenability and easier development. Infact, given a choice between performance and maintenability, I would go for maintenability any day.

But, in my real-time application, I am facing this issue even after JSP is compiled for the first time on the server side. Could it be that numerous method calls for <c:forEach> tag implementation within JSP servlet be taking more time when the resultset is *HUGE*. As I understand, it has to make the method calls N number of times depending on the number of elements in the resultset.

When I write a scriplet code, I avoid this overhead on method calling and do looping within the same segment of code.

Again, I should not be displaying 900+ records in a single JSP page, but this is something not under my control as my PM wants it that way.

Thanks for your reply,
Karthik.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
It's a design decision only you can make. In your place I'd try to get some good arguments to convince the PM that his decision is fundamentally flawed (as we both agree it is).

Not only does the JSP processing now take an inordinate amount of time (it should still at 900 records be more than a few seconds over the time it takes with a scriptlet) but the amount of data (most of it completely useless) you're piping to your clients (browsers) is enormous.
This will bog down your network, causing general delays all over the place as well as causing a massive increase in hosting cost due to the increased bandwidth consumption.

If those financial concerns don't convince the beancounters maybe the usability argument that your end users don't want to wait a minute or more for the page to render and then having to scroll through nearly a thousand lines of results will.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

In addition to Jeroen's comments.

Unless this is being run on a fast intranet, the gains you make by dumping JSTL and writing this in scriptlet might not be noticable for users with medium to slow connections. IOW: the network will be the bottleneck, not the JSTL tags.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
There must be something else going on here. I ran a simple test.

***** File: with-jstl.jsp *****
<%@taglib uri="/WEB-INF/c.tld" prefix="c" %>
<html>
<body>
<c:forEach items="${beans}" var="bean">
<c ut value="Hello, ${bean.first} ${bean.last}!" /><br />
</c:forEach>
</body>
</html>


***** File: with-scriptlets.jsp *****
<%@ page import="java.util.Collection,
java.util.Iterator,
test.domain.TestBean"%>
<html>

<body>
<%
final Collection beans = ( Collection )request.getAttribute( "beans" );
for( Iterator i = beans.iterator(); i.hasNext(); )
{
TestBean bean = ( TestBean ) i.next();
%>
Hello, <%=bean.getFirst()%> <%=bean.getLast()%>!<br />
<%
}
%>
</body>
</html>

***** File: SetupFilter.java *****
public class SetupFilter implements Filter
{
private Collection beans;
public void destroy()
{
}

public void doFilter( ServletRequest req, ServletResponse resp, FilterChain chain ) throws ServletException, IOException
{
req.setAttribute( "beans", beans );
chain.doFilter( req, resp );
}

public void init( FilterConfig config ) throws ServletException
{
beans = new LinkedList();

for( int i = 0; i < 10000; ++i )
{
beans.add( new TestBean( "First" + i, "Last" + i ) );
}
}
}

***** File: TimingFilter.java *****
public class TimingFilter implements Filter
{
private FilterConfig filterConfig;

public void init( FilterConfig filterConfig ) throws ServletException
{
this.filterConfig = filterConfig;
}

public void doFilter( ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain ) throws IOException, ServletException
{
final long before = System.currentTimeMillis();
filterChain.doFilter( servletRequest, servletResponse );
final long after =System.currentTimeMillis();
filterConfig.getServletContext().log( "Request for "+ ( ( HttpServletRequest )servletRequest ).getRequestURI() + " took " + ( after - before ) + " milliseconds." );
}

public void destroy()
{
filterConfig = null;
}
}

Here are some of the results I got using JBoss 4.0.1 and JDK 1.4.2_06...

18:46:44,973 INFO [Engine] StandardContext[/test-webapp]Request for /test-webapp/with-jstl.jsp took 601 milliseconds.
18:46:46,985 INFO [Engine] StandardContext[/test-webapp]Request for /test-webapp/with-scriptlets.jsp took 530 milliseconds.
18:46:49,279 INFO [Engine] StandardContext[/test-webapp]Request for /test-webapp/with-jstl.jsp took 521 milliseconds.
18:46:51,722 INFO [Engine] StandardContext[/test-webapp]Request for /test-webapp/with-scriptlets.jsp took 480 milliseconds.
18:46:53,725 INFO [Engine] StandardContext[/test-webapp]Request for /test-webapp/with-jstl.jsp took 531 milliseconds.
18:46:55,868 INFO [Engine] StandardContext[/test-webapp]Request for /test-webapp/with-scriptlets.jsp took 400 milliseconds.
18:46:58,161 INFO [Engine] StandardContext[/test-webapp]Request for /test-webapp/with-jstl.jsp took 520 milliseconds.
18:46:59,644 INFO [Engine] StandardContext[/test-webapp]Request for /test-webapp/with-scriptlets.jsp took 471 milliseconds.


James Carman, President<br />Carman Consulting, Inc.
Karthik Kottur
Greenhorn

Joined: Oct 19, 2002
Posts: 7
Hi,

Thanks for all your helpful comments. Paging was something that was always there on my mind. As I inherited this system at my present workplace from some other group, I have to implement a lot of goody functionalities like paging on most of the screens.

Coming back to my issue, our system does run on intranet and network bandwidth is something which is not a major concern here. As the intranet infrastructure here exists for a 45,000 strong employee base, bandwidth is not a major concern.

Also, thanks to James Carman for performing the runtime test. But, like I said, the only difference in my JSP code is using scriplets in place of <c:forEach> tag and performance increased by atleast 300%. I am sure, I didn't change any other part of the JSP other than this. Also, I ran the tests on the second or third run of JSP call leaving 1st run for JSP compilation.

Could it be that my environment be a factor? I am running all these tests on my local TOMCAT 4.1.31 instance / JDK 1.4.2_06 with PENTIUM 4 box and 512 MB RAM.

Thanks,
Karthik.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Have you altered the amount of ram allocated to the jvm?
By default the jvm only takes 64m of ram.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

"Karthik",

We're pleased to have you here with us on the Ranch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Karthik Kottur
Greenhorn

Joined: Oct 19, 2002
Posts: 7
Hi Ben,

I haven't tried that piece yet. I will try it today and will let you know the results.

Hi Bear,

Have changed my display name per your request.

Thanks,
Karthik.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

Thank you!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JSTL forEach tag taking longer time than scriplets ???