Friday, August 22, 2008

Javascript function similar to Sleep of JAVA

Here is a Javascript function that works similar to the Thread.sleep(milliseconds) method of JAVA.

The code of the JavaScript function is as under:

function pause(numberMillis)
var now = new Date();
var exitTime = now.getTime() + numberMillis;
while (true)
now = new Date();
if (now.getTime() > exitTime)

You need to call this function like
pause(1000) ;
to wait a second!

Monday, August 18, 2008

JavaScript: Print Button on Your Web Page

It is fairly simple to add a PRINT Button on your web pages using JavaScript. The best practise to provide the facility of printing on web page is to provide a page that does not contain the banners/headers/footers/menus etc.
For this just create another web page containing only the data you actually need to print; For this make sure the background is not dark or does not contains the darked background image. etc

You can use simple JavaScript built in method window.print(). For example the following code segment will place a print link on your webpage using the img tag of HTML.

< i m g onclick="window.print();" src="/images/PrinterIcon.gif"
style="cursor:hand;" />

(Replace the text /images/PrinterIcon.gif with the location of an icon in your site.)

Wednesday, August 13, 2008

JAVA: Method That Removes Specified Characters From String

I needed a method that can remove specified set of characters from a given string. I came up with this solution. This method takes two parameters.

1. First parameter is original String from which you want to remove the characters.
2. Second parameter is a string containing the characters that we want to remove from the original string.


If we provide the following input:

removeFromString("MYRSH Blogs by Sabah u Din Irfan","asi");

The method will return the followin output:

MYRSH Blog by Sbh u Dn Irfn

public static String removeFromString(String str,String rem)
StringBuffer result=new StringBuffer();

char[] s = str.toCharArray();
char [] r = rem.toCharArray();

boolean [] flags =new boolean[128];

int len=str.length();
int src, dest;

for(src = 0 ; src < r.length; ++src )

src = 0 ; dest = 0 ;
while(src < len)
if( !flags[(int)s[src]] )
return result.toString();

Friday, August 8, 2008

JSP: Save As Pop Up in JSP Another way

Place the following code segment in your scriptlet tag of JSP page.

response.setHeader("Content-Disposition","attachment; filename=abc.xls");
ServletOutputStream so = response.getOutputStream();
String filename = "c:\\reports\myreport.xls";
String mimetype = "application/";
ByteArrayOutputStream output = new ByteArrayOutputStream();
InputStream in = new BufferedInputStream(new FileInputStream(filename));
byte bytebuff[] = new byte[500];
for(int lengthread = 0; (lengthread = != -1;){
output.write(bytebuff, 0, lengthread);
byte data[] = output.toByteArray();


is the file name that will automatically appear in the file Name box of SaveAs Pop Up.


is the actual file path on your server machine. It may be outside of your WEB-INF directory.

JSP: Save As Pop Up in JSP

Step First:

Add the following method in the declaration tag of your JSP page.


void returnFile(String filename, OutputStream out)throws Exception
// open file here using File object
File inputFile =new File(filename);
// open stream from that file
FileReader in = new FileReader(inputFile);
int c;
while ((c = != -1)

// close the stream
// close the file


Step Two:

Add the following code segment in scriptlet tag.

response.setHeader("Content-Disposition", "attachment; filename=ASPTransBreakOut.txt");

// Send the file.
OutputStream outstr = response.getOutputStream( );
returnFile(fileLocation, outstr); // Method implemented in declarative tag in step first


is the name that will automatically show you up in SaveAs Popup menu



is the location of the actual fine on your server.