Wednesday, May 30, 2007

Setting Cache headers for Gifs, Css and JS files

By default, browser and servers use the "if-modified-since" header to check if the file should be downloaded from the server or given from the cache.Though this default behaviour is good for performance, we can increase the performance by giving a cache expiry period. This would make the browser cache the content for that period - i.e. the browser won't even make "if-modified-since" conditional fetch requests.The best place to set these headers in a J2EE application is thru a filter.Suppose you want to cache GIFs, CSS and JPEG for 2 hours, just add this header in the response:Cache-Control: max-age=120

Silently print PDFs on the browser

If your web application needs to print PDF's silently on the browser, then check out this blog.Of all options given, I found the iText trick the simplest to use. The concept is quite simple:- Create a PDF on the fly using iText- Embed Acrobat Javascript into the PDF that would print it automatically to the default printer.- Open the PDF document in a iFrame that is hidden (size 0*0)A live example can be found here.

Sunday, May 6, 2007

javascript:select the row in table

Code to make the table row highlight depending on what you entered in text field.
call this function on keyUp of textField

Function searchTable(table1,txtId)
{
var trimVal = trim(document.getElementById(txtId).value);
var table = document.getElementById(table1);
var rows = table.rows;
var rowLength = rows.length;

var i = 1 ;
var firstFind = true ;
if(rows == null typeof(rows) == 'undefined')
{
return ;
}
for(i = 1; i < rowLength ; i++) {
var cells = rows[i].cells;
var cellLength =cells.length;
rows[i].className="normalrow";

tdValue = trim(cells[colOnSearch].innerHTML) ;
var enteredValueLength = trimVal.length;
var valueToSearch = tdValue.substring(enteredValueLength,0);
valueToSearch = valueToSearch.toLowerCase();
if(valueToSearch == trimVal)
{
if(firstFind)
{
rows[i].className="classmouseover";
rows[i].cells[colOnSearch].scrollIntoView();
}
firstFind = false ;
}
}

}//end of for
}//end of function

Compressing Content Using a Servlet Filter

To increase performance of your web application use compression filters
You can obtain very effective compression by having a servlet filter conditionally pipe whatever your web application produces to a GZIP-compressed file.
Why GZIP? Because the HTTP protocol, the protocol used to transmit web pages, allows for GZIP compression.

GZIP compression will usually get you around a 6:1 compression ratio; it depends on how much content you are sending and what the content is. In practice, this means you will send content to a user up to six times faster if you simply use GZIP compression whenever you can.
There are following links where you will get code of Compression Filters

http://java.sun.com/products/servlet/Filters.html
http://www.thomas-bayer.com/gzip-compression-filter.htm