2

My understanding of JSP is that each line in the java code is run step by step (in sequence). E.g. if I have a code below, doSomething("apple") will be executed first until it returns a value, then doSomething("orange") will be executed next until it returns a value, then finally doSomething("pear") will be executed until it returns a value and the whole page is displayed.

<table border="1">
    <thead>
        <tr>
            <th>Test</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Column A</td>
            <td><%=javaBean.doSomething("apple")%></td>
        </tr>
        <tr>
            <td>Column B</td>
            <td><%=javaBean.doSomething("orange")%></td>
        </tr>
        <tr>
            <td>Column C</td>
            <td><%=javaBean.doSomething("pear")%></td>
        </tr>
    </tbody>
</table>

What is the best way to make these calls parallel e.g. run doSomething("apple") & doSomething("orange") & doSomething("pear") concurrently? Thank you.

Tiny
  • 27,221
  • 105
  • 339
  • 599
Antoni
  • 63
  • 1
  • 8
  • 2
    The first thing to learn is to [avoid](http://stackoverflow.com/q/3177733/1037210) this hateful Scriptlet which is **highly** discouraged, for over a decade. – Lion Mar 24 '13 at 08:41
  • 1
    The word is 'sequentialized', not 'serialized'. – user207421 Mar 24 '13 at 09:09
  • 4
    @EJP - According to "dictionary.com" (and anyone with a modicum of literary good taste!) ... "sequentialized" is not a real English word. A more appropriate description would be "run in sequence". – Stephen C Mar 24 '13 at 09:31

3 Answers3

3

JSP creates dynamic html. So you are essentially placing the result of your business logic from javaBean to your html table. As it is you can not make it concurrent as you need the result of each method to be placed in the row.
You should restructure your code so as to calculate everything you need (perhaps using concurrency) and then retrieve the results to place them in the row.

Cratylus
  • 52,998
  • 69
  • 209
  • 339
  • Hi, thanks for your reply. Do you mean I should create a new method that will give me the results of doSomething("apple") & doSomething("orange") & doSomething("pear") in one go (perhaps parallelize this method)? – Antoni Mar 24 '13 at 09:13
  • With `JSP` you build `HTML` dynamically.So it can not be faster than getting all the results and putting them in one page and sending it to client.So you could try to calculate the results in another method (redirect from another servlet perhaps?) and get the results in one go e.g. from a `hashtable` – Cratylus Mar 24 '13 at 09:32
2

As i understood your question you need to create three task(three thread) for your method which will run independently irrespective of their order to complete.

mannu4u
  • 272
  • 3
  • 10
  • @Lion Nobody said anything about 'by hand', but regardless of your policy on it, creating threads is the only way to meet the OP's requirement as stated, flawed though it almost certainly is. – user207421 Mar 24 '13 at 09:11
2

You shouldn't do this in JSP, it's designed to render in a single thread. If the page is too slow, the usual method these days is to have a fast loading page with three placeholders. Then load the slow parts with AJAX. These can make concurrent calls back to the server to load the rest.

artbristol
  • 32,010
  • 5
  • 70
  • 103