Thursday 4 August 2011

Java Static Block





I was working on the requirement of extracting the contents from a html page(Which is not that dynamic,in the sense no frequent changes) and format it, display on the front end(jsp in my case).


I opted  HtmlUnit and grepped the contents and transferred them to the front end.Since,the HtmlUnit has to parse the tables,, the page rendering was very slow.


Static block for rescue(since the html is not so dynamic and has no frequent changes),parsed the html and load the contents at the time of class loading.


The loaded data will be served for each request instead of loading data for each request and decreasing the page rendering time and the static block looks as below and it worked like a charm ;-).


So,the elementList in the above code will be populated (which is time consuming) during the class load and happens only once and this would be used in the request serving methods.Load once Serve manier times.




Cheers... Happy Coding ;-)

No comments:

Post a Comment