Arch2Arch Tab BEA.com
Syndicate this blog (XML)

Stuff it in the Session

Bookmark Blog Post

del.icio.us del.icio.us
Digg Digg
DZone DZone
Furl Furl
Reddit Reddit

Bill Roth's Blog | June 19, 2007   1:22 AM | Comments (3)


 I was recently coding an app, trying out the nightly build of the forthcoming Workshop 10.1 "Merged" release. This is an application which queries the number of bugs on the unreleased Workshop and sends the result to me via WAP to my Blackberry. The query, which was provided to me via a JDBC control, was taking a long time. So, I wanted to actually start timing it so I could ask for some improvements. Essentially I wanted to start a timer before the queries started, and right after rendering, print out the elapsed time on the results page.

The problem was, I started the time in the Action from the first page, and I wanted to display  the timer in a scriptlet in the rendered result page, and I needed to pass the elapsed time to the new page in a way that could be ready by the scriptlet. I tried a number of things, including add it as an actionOutput, but I could not figure out how to retrieve it. I even asked a few people, none of whom knew off the top of their head. One person gave me a hint, which was "try stuffing it in the session". That did the trick. Here is how I did it:

 

In the Action in the Controller, I do the following:

        long stopTime = System.currentTimeMillis();
        Long l = stopTime - startTime;
        //
        // Now stuff the time in the Session.
        //
        getSession().setAttribute("elapsedTime", l);

 

Ok, so the code ain't pretty but you can't beat the price. The in the final page where I render things, I do the following.

 

<br>Executed in <% out.print(((Long)session.getAttribute("elapsedTime"))/1000.0);%> sec. <br>

 

I will count this as my coding achievement for June. And by the way, the new Workshop 10.1 was rock-solid for me, and I was using a 3 week old build.


Comments

Comments are listed in date ascending order (oldest first) | Post Comment

  • If using JSP2.0:
    Executed in ${sessionScope.elapsedTime / 1000.0 } sec.
    otherwise:
    Executed in sec.
    type conversion should happen automatically

    Posted by: masterchief on June 19, 2007 at 6:01 AM

  • Another alternative is to use page inputs and action outputs. This has the advantage of keeping it scoped to the request, which sounds like what you want from your description rather than the broader and longer lived scope of the session.

    The action would look something like:

    long stopTime = System.currentTimeMillis();
    Long l = stopTime - startTime;
    
    Forward forward = new Forward("success");
    forward.addActionOutput("duration",l);
    return forward;
    

    Then your EL could look like:

    Executed in ${pageInput.duration / 1000.0 } sec. 
    

    Posted by: hogue on June 19, 2007 at 6:30 PM

  • Chris's solution above is probably more the appropriate thing to do.

    Posted by: wgroth2 on June 20, 2007 at 5:37 AM



Only logged in users may post comments. Login Here.

Powered by
Movable Type 3.31