2

I have a JSP contains high amount of HTML code. There are many inner divs, spans and h2 tags. The HTML code is generated by scriplets using some for loops.

I measure the scriptlet process by using the following :

   <% long time = System.currentTimeMillis(); %>
     // here is the entire page data
   <% System.out.println("Time : " + (System.currentTimeMillis()-time)); %>

According to this measurement, the process time is 300-350 msecs.

To spot the parts that make the delay, I did something like that :

    <% long time = System.currentTimeMillis(); %>
         // Some HTML Blocks
    <% System.out.println("Time1 : " + (System.currentTimeMillis()-time)); %>
         // Another HTML Blocks
    <% System.out.println("Time2 : " + (System.currentTimeMillis()-time)); %>

However, I realised that sometimes even if I do the following, even there is no html block between printlns, the time value is still changes !

   <% long time = System.currentTimeMillis(); %>
             // Some Blocks
   <% System.out.println("Time1 : " + (System.currentTimeMillis()-time)); %>
   <% System.out.println("Time2 : " + (System.currentTimeMillis()-time)); %>

Output is :

Time1 : 65
Time2 : 208

So what is the thing that slow down my page ? How can I detect the point?

Do the Scriptlets have performance weakness for processing heavy HTML codes?

----- UPDATE ------

enter image description here

Here is the output :

 first : 0
 Sec : 0
 thr : 0
 fr : 180
 Total : 180

There is nothing between thr and fr but fr value is 180 !

anL
  • 1,073
  • 1
  • 11
  • 31
  • No. It's just plain old Java code writing Strings to an HTTP response writer. But they have serious maintainability weaknesses. Read http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files – JB Nizet Apr 02 '16 at 16:13

1 Answers1

1

JSPs are java classes underneath. There shouldn't be any performance impact. Scriptlets are bad practice though, so unless this is just for benchmarking purposes, I wouldn't do it.

There are overhead costs when printing to the console as well. So when you're calculating:

<% System.out.println("Time2 : " + (System.currentTimeMillis()-time)); %>

You're adding all the time it takes to make the println call. If you want a more accurate reading, you should be doing this instead:

<% long time2 = System.currentTimeMillis(); %>

Only calculate the time at the very end of your JSP. Do not calculate/print results in the middle of the JSP as you'll see invalid results.

As an aside, I see you say you're using for loops. You should look at the JSTL core library: https://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/c/tld-summary.html

Christopher Schneider
  • 3,745
  • 2
  • 24
  • 38
  • Thanks for the answer. Using JSTL or any other MVC solution can decrease the html generation time ? As you say, the jsp becomes a java class contains "out.write().."s at the end. Would there be any difference ? – anL Apr 02 '16 at 16:25
  • 1
    JSTL code is Java code. Is there a performance difference? Probably not. You're writing scriptlets, which are Java code. JSTL is just converted to Java code at compile time, and is far more maintainable. – Christopher Schneider Apr 02 '16 at 16:31
  • 1
    I'll also add that I recently spent two weeks re-writing a page heavily using for loops for rendering. The for loops generated roughly 2000 lines of HTML, and in the worst case they could run 20 times. If this sounds like your situation, I'll caution you to rethink your design. In the case of Internet Explorer, the pages took approximately 60 seconds to load when the loop ran only 10 times. This is poor design using scriptlets OR JSTL. – Christopher Schneider Apr 02 '16 at 16:36
  • _Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds._ According to the docs on currentTimeMillis(). You should probably use `System.nanoTime()` – Christopher Schneider Apr 04 '16 at 13:30