I found good tool which contains lot of ready made javascript codes to improve the design, validate forms, detect browsers, create cookies,calendars, calculator, clocks, password protections, page-details, scrolls.It is a freeware.You just need to copy the code and use it
http://www.freedownloadscenter.com/Programming/Java/EMX_Javascript_Professional_Download.html
Wednesday, December 26, 2007
Free DHTML Menu Builder
Below is good link for Free DHTML menu builder
http://www.freedownloadscenter.com/Programming/Java/Sothink_DHTML_Menu_Builder.html
Sothink DHTML Menu is the most popular JavaScript navigation menu maker currently found on the web. It covers all your DHTML web menu system needs.
http://www.freedownloadscenter.com/Programming/Java/Sothink_DHTML_Menu_Builder.html
Sothink DHTML Menu is the most popular JavaScript navigation menu maker currently found on the web. It covers all your DHTML web menu system needs.
Friday, November 2, 2007
Calculating Time Taken By a Request
To Log the time taken by a request we can use Timer Filter.It will help us to find out performance of system and finding out which request are taking more time then required.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TimerFilter implements Filter
{
private FilterConfig config = null;
public void init(FilterConfig config) throws ServletException { this.config = config; }
public void destroy()
{
config = null;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
long before = System.currentTimeMillis();
chain.doFilter(request, response);
long after = System.currentTimeMillis();
String name = "";
if (request instanceof HttpServletRequest)
{
name = ((HttpServletRequest)request).getRequestURI();
}
config.getServletContext().log(name + ": " + (after - before) + "ms");
}
}
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TimerFilter implements Filter
{
private FilterConfig config = null;
public void init(FilterConfig config) throws ServletException { this.config = config; }
public void destroy()
{
config = null;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
long before = System.currentTimeMillis();
chain.doFilter(request, response);
long after = System.currentTimeMillis();
String name = "";
if (request instanceof HttpServletRequest)
{
name = ((HttpServletRequest)request).getRequestURI();
}
config.getServletContext().log(name + ": " + (after - before) + "ms");
}
}
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
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
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
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
Tuesday, April 10, 2007
Caching headers in HTTP
All dynamic applications need to prevent caching so that the request actually reaches the server each time.
Here are the headers used to control cache
response.setHeader("Pragma", "no-cache");
This is the only cache control directive for HTTP 1.0, so should feature in addition to any HTTP 1.1 cache control headers you include.
response.setHeader("Cache-Control", "no-cache"); // HTTP 1.1
This directive does NOT prevent caching despite its name. It allows caching of the page, but specifies that the cache must ask the originating web server if the page is up-to-date before serving the cached version. So the cached page can still be served up i- f the originating web server says so. Applies to all caches.
response.setDateHeader ("Expires", 0); // HTTP 1.1
This tells the browser that the page has expired and must be treated as stale. Should be good news as long as the caches obey.
response.setHeader("Cache-Control", "private"); // HTTP 1.1
This specifies that the page contains information intended for a single user only and must not be cached by a shared cache (e.g. a proxy server).
response.setHeader("Cache-Control", "no-store"); // HTTP 1.1
This specifies that a cache must NOT store any part of the response or the request that elicited it. Adding these two headers should prevent the caching of pages anywhere between the web server and browser, as well as in the browser itself. The meaning of each directive is very specific and so a given combination of directives has a different effect in any one environment.
Here are the headers used to control cache
response.setHeader("Pragma", "no-cache");
This is the only cache control directive for HTTP 1.0, so should feature in addition to any HTTP 1.1 cache control headers you include.
response.setHeader("Cache-Control", "no-cache"); // HTTP 1.1
This directive does NOT prevent caching despite its name. It allows caching of the page, but specifies that the cache must ask the originating web server if the page is up-to-date before serving the cached version. So the cached page can still be served up i- f the originating web server says so. Applies to all caches.
response.setDateHeader ("Expires", 0); // HTTP 1.1
This tells the browser that the page has expired and must be treated as stale. Should be good news as long as the caches obey.
response.setHeader("Cache-Control", "private"); // HTTP 1.1
This specifies that the page contains information intended for a single user only and must not be cached by a shared cache (e.g. a proxy server).
response.setHeader("Cache-Control", "no-store"); // HTTP 1.1
This specifies that a cache must NOT store any part of the response or the request that elicited it. Adding these two headers should prevent the caching of pages anywhere between the web server and browser, as well as in the browser itself. The meaning of each directive is very specific and so a given combination of directives has a different effect in any one environment.
javascritp to get application context path
Recently one of my project I required to get context root in js file .We can’t use request.getContextPath() in js file us I wrote javascript to get context root
function getContextRoot()
{
//Location object's href gives the complete URL
var url = window.location.href;
//We need to extract the context root from the full URL.
var regExp = /^http:\/\/[0-9a-zA-Z.]*\/[0-9a-zA-Z]*\//
var matches = url.match(regExp);
//var url = "http://172.28.49.18:8084/Cyclops/login.do
//it will return http://172.28.49.18:8084/Cyclops/
return matches ;
}
function getContextRoot()
{
//Location object's href gives the complete URL
var url = window.location.href;
//We need to extract the context root from the full URL.
var regExp = /^http:\/\/[0-9a-zA-Z.]*\/[0-9a-zA-Z]*\//
var matches = url.match(regExp);
//var url = "http://172.28.49.18:8084/Cyclops/login.do
//it will return http://172.28.49.18:8084/Cyclops/
return matches ;
}
Sunday, April 1, 2007
Javascript: Conditional Comments and Conditional Compilation
Recently came across some cool new features of Javascript that reduced the amount of coding required. For many years, developers have been writing Javascript code to detect the browser type and accordingly fire javascript functions or do a document.write()But now there are more developer-friendly ways of detecting the browser type and handling content.
Conditional Comments: These look like HTML comments, but IE browsers treat them differently.
Another feature of IE is Conditional Compilation. Non-IE browsers would ignore the @cc block
/*@cc_on
/*@if(@_win32)
document.write("OS is 32-bit, browser is IE.");
@else @*/
document.write("Browser is not IE (ie: is Firefox) or Browser is not 32 bit IE.");
/*@end@*/
Conditional Comments: These look like HTML comments, but IE browsers treat them differently.
Another feature of IE is Conditional Compilation. Non-IE browsers would ignore the @cc block
/*@cc_on
/*@if(@_win32)
document.write("OS is 32-bit, browser is IE.");
@else @*/
document.write("Browser is not IE (ie: is Firefox) or Browser is not 32 bit IE.");
/*@end@*/
Getting the Client IP address on a J2EE server
The HTTPRequest object has 2 methods 'getRemoteAddr()' and 'getRemoteHost()'. These methods would return us the IPAddress/HostName of the last proxy or the client. So if the client is behind a proxy, then we would get the IPAddress of the proxy.
Some proxies (known as transparent proxies) pass the actual IP address of the client in the header HTTP_X_FORWARDED_FOR.So on the server side we can code something like this:
if (request.getHeader("HTTP_X_FORWARDED_FOR") == null)
{
String ipaddress = request.getRemoteAddr();
} else
{
String ipaddress = request.getHeader("HTTP_X_FORWARDED_FOR");
}
But if the proxy is an anonymous proxy, then even this won't work. So the only way to get the Client Address correctly is using an Applet to capture the IP address of the client. For this, the client should be trusted and signed.Another option that I came across in a forum is to create a Socket connection back to the web server from which you came and asking the Socket for the local address:
URL url = getDocumentBase();
String host = url.getHost();
Socket socket = new Socket(host, 80);
InetAddress addr = socket.getLocalAddress();
String hostAddr = addr.getHostAddress();
System.out.println("Addr: " + hostAddr);
Some proxies (known as transparent proxies) pass the actual IP address of the client in the header HTTP_X_FORWARDED_FOR.So on the server side we can code something like this:
if (request.getHeader("HTTP_X_FORWARDED_FOR") == null)
{
String ipaddress = request.getRemoteAddr();
} else
{
String ipaddress = request.getHeader("HTTP_X_FORWARDED_FOR");
}
But if the proxy is an anonymous proxy, then even this won't work. So the only way to get the Client Address correctly is using an Applet to capture the IP address of the client. For this, the client should be trusted and signed.Another option that I came across in a forum is to create a Socket connection back to the web server from which you came and asking the Socket for the local address:
URL url = getDocumentBase();
String host = url.getHost();
Socket socket = new Socket(host, 80);
InetAddress addr = socket.getLocalAddress();
String hostAddr = addr.getHostAddress();
System.out.println("Addr: " + hostAddr);
Printing Stack Trace in Java
Quite often, developers use the statement exception.printStackTrace() to print the stack trace on to the console. It is important to remember that printStackTrace method prints the stack trace to the System.Err stream and not the System.Out stream.Due to this, there are a lot of exception stacks getting printed in the System.Err files on the application server, but we do not see any trace of it in the Log4J files or System.out files !!!!
As a best practice, I strongly recommend not using e.printStackTrace() in production code. If the project is using Log4J, then the method 'Logger.error(message, exception)' would print the stack trace to all the registered appenders.
Alternatively, I have written a utility method that would return the stack trace as String.
Please use this method if you want to get a stacktrace as a string for more flexibility in printing.
---------------------------------------------------------
/**
Gets the exception stack trace as a string.
* @param exception
* @return
*/
public String getStackTraceAsString(Exception exception)
{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
pw.print(" [ ");
pw.print(exception.getClass().getName());
pw.print(" ] ");
pw.print(exception.getMessage());
exception.printStackTrace(pw);
return sw.toString();
}
---------------------------------------------------
But what if there is already production code that uses e.printStackTrace() and System.out and you want to redirect them to the Log Files. In that case, we can make use of the System.setOut() and System.setErr() methods. Also try the LoggingOutputStream class in the contribs/JimMoore folder of the log4j release.
As a best practice, I strongly recommend not using e.printStackTrace() in production code. If the project is using Log4J, then the method 'Logger.error(message, exception)' would print the stack trace to all the registered appenders.
Alternatively, I have written a utility method that would return the stack trace as String.
Please use this method if you want to get a stacktrace as a string for more flexibility in printing.
---------------------------------------------------------
/**
Gets the exception stack trace as a string.
* @param exception
* @return
*/
public String getStackTraceAsString(Exception exception)
{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
pw.print(" [ ");
pw.print(exception.getClass().getName());
pw.print(" ] ");
pw.print(exception.getMessage());
exception.printStackTrace(pw);
return sw.toString();
}
---------------------------------------------------
But what if there is already production code that uses e.printStackTrace() and System.out and you want to redirect them to the Log Files. In that case, we can make use of the System.setOut() and System.setErr() methods. Also try the LoggingOutputStream class in the contribs/JimMoore folder of the log4j release.
Subscribe to:
Posts (Atom)