Monday, March 30, 2009

Handling Session timeout & other server http errors in AJAX

AJAX has really changed the way we build web applications.When I surf internet today, I find almost all the websites Ajaxified in some way. Writing an AJAX application is as simple as writing simple JavaScript methods and some server side code.

I have been writing AJAX codes since a long time now and believe me tge most difficult part is to handle errors and exceptions. Session timeout is one of the most trivial error that one needs to consider while creating an AJAX enabled application.

Normally, we use AJAX to deliever HTML content that we put in a DIV or SPAN tag and display it on screen. But suppose the request that we sent using AJAX was generated an error due to session timeout then the session timeout screen will be shown in DIV tag. Any error generated by server let say http 404 error or http 505 can lead to display such error in our display DIV.

Handle Session timeout errors

One solution that you can use to handle the session timeout errors is using JSON or XML format as output for your AJAX requests. You have to check for session in your script, say for example if you are using JSP then probably you can do something like this:

Thursday, March 19, 2009

How to reduce the size of JavaScript & CSS file?

Compress JavaScript / CSS file with the help of gzip:-

With the help of php ob_start() function we can create gzip file for JavaScript.

if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], ‘gzip’))
ob_start(”ob_gzhandler”);
else
ob_start();
?>

We can use above code at the beginning of the JavaScript and code at the end. So output of Php file is JavaScript code in gzip encrypted form.

In this method, we have to save JavaScript files as a .php file.

By compressing the plan text JavaScript file with the help of gzip; its size gets reduced by more than 60%. Smaller the file size, better the download time.

Reduce the size of JavaScript and CSS file by removing the white spaces, carriage return and comments:-

We can also remove the white spaces, carriage return and comments from JavaScript files to reduce its size. Every single letter or space in your HTML code takes up one byte. It doesn’t sound like much but it all adds up. By working through page source and eliminating unnecessary white space and comments, we can shave off up to, or even over 10% of its file size.

Struts Validation Framework tutorial with example.

Apache Struts has changed the way we develop a Web application. Since its inception as an MVC architecture, Struts has been extensively used in J2EE world to develop robust, extendable and effective web applications.

Introduction to Struts Validation Framework

One of the important features of Struts framework is Struts Validation framework that performs validation on incoming form data. Validation framework was introduced by David Winterfeldt as an external plugin to Struts framework. It’s functionality has since been split so that validator can serve as the basis for a independant component and is now part of Jakarta Commons.

The Struts framework’s simple validation interface alleviates much of the headache associated with handling data validation, allowing you to focus on validation code and not on the mechanics of capturing data and redisplaying incomplete or invalid data.

In order to do form validation without Validator framework, one has to use validate() method of the form bean (ActionForm class) to perform this task. Also one has to handle error messages during manual validation. Lot of fields that we validate require same logic to validate them, hence code is unneccessarily duplicated (if not managed properly).

Validation framework comes with set of useful routines to handle form validation automatically and it can handle both server side as well as client side form validation. If certain validation is not present, you can create your own validation logic and plug it into validation framework as a re-usable component.

Validator uses two XML configuration files to determine which validation routines should be installed and how they should be applied for a given application, respectively. The first configuration file, validator-rules.xml, declares the validation routines that should be plugged into the framework and provides logical names for each of the validations. The validator-rules.xml file also defines client-side JavaScript code for each validation routine. Validator can be configured to send this JavaScript code to the browser so that validations are performed on the client side as well as on the server side.

The second configuration file, validation.xml, defines which validation routines should be applied to which Form Beans. The definitions in this file use the logical names of Form Beans from the struts-config.xml file along with the logical names of validation routines from the validator-rules.xml file to tie the two together.

Using the Validator framework involves enabling the Validator plug-in, configuring Validator’s two configuration files, and creating Form Beans that extend the Validator’s ActionForm subclasses. The following sections explain in detail how to configure and use Validator.

package net.viralpatel.struts.validation.form;

import org.apache.struts.validator.ValidatorForm;

public class CustomerForm extends ValidatorForm {

private String name;
private String telephone;
private String email;
private int age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getTelephone() {
return telephone;
}

public void setTelephone(String telephone) {
this.telephone = telephone;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}

Thursday, March 12, 2009

Export data to a CSV file using MySQL command prompt

SELECT tableColumnName1, tableColumnName2 INTO OUTFILE '/path/to/file/data.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM tableName;

Access Denied

Even if you have been granted the SELECT privilege, you will still need to be granted the FILE privilege, or else access will be denied. Some architects will only grant this privilege to the root user. Another reason that you might receive an access denied message, is because the directory where you are trying to save the CSV file is not writable.

In order to prevent a user from accidentally overwriting an important system file (like etc/passwd), you will be unable to save over a file that already exists on the server.

Tuesday, February 24, 2009

IBM develops Hyperspeech Transfer Protocol (HSTP)

IBM’s research scientists in India have developed a technology that will offer users the ability to talk to the Web and create ‘voice’ sites using mobile phones according to a news article in the Economic Times today.

Hyperspeech Transfer Protocol (HSTP), a protocol designed to seamlessly connect telephony voice applications, will enable users to browse across voice applications by navigating the Hyperspeech (the voice hyperlink) content in a voice application.

“People will talk to the Web and the Web will respond. The research technology is analogous to the Internet. Unlike personal computers it will work on mobile phones where people can simply create their voice sites,” IBM India Research Laboratory Associate Director Manish Gupta told the Economic Times.

Given India’s position as the fastest growing mobile phone market in the world, this new protocol may be particularly useful in India, where mobile phone sales are booming despite our current economic crisis.

If you’re interested in reading the entire paper, HSTP : Hyperspeech Transfer Protocol, you can download it here (PDF).

Source:
Economic Times
ReadWriteWeb

Thursday, February 19, 2009

Google Chrome released a new Beta

Last December when Google dropped the Beta tag from its browser, Google Chrome. But now they have released a new beta version of Chrome which is claimed to be faster than the stable version. The official blog of Chrome quoted that the new “beta” version is 25% faster on V8 benchmark and 35% faster on the Sunspider benchmark than the current stable version and is almost twice as fast when compared to original beta version.
rom today onwards, Google is going to develop its browser in three parallel channels: Stable channel, Beta channel and Developer preview channel.

Some of the new features include in new beta release are form autofill, full page zoom and autoscroll and a new way to drag tabs out to get a side-by-side view.

The latest beta version can be downloaded here.

Speed has became a real factor in today’s Browser WAR. From the latest release of Safari 4 beta which is claimed to be fastest browser till date, the browser war has becoming really intense.

Monday, January 19, 2009

How to apply TinyMCE editor only to specific Textarea?

“80-90% of the end-user response time is spent downloading all the components in the page: images, stylesheets, scripts, Flash, etc. Rather than starting with the difficult task of redesigning your application architecture, it’s better to first disperse your static content. This will achieves a bigger reduction in response times.” - Steve Saunders

Performance, Scalability and Cost efficiency to end users are main key factors for any web application. CDN (content delivery network) is the solution to achieve it. A CDN can offer 100% availability, even with large power, network or hardware outages. Traditional CDNs focus on web acceleration. New CDNs have integrated media delivery services so they are optimized for live video streaming, high definition video and large asset delivery.

Why we need to use CDN?
The main point of the CDN is caching. So, it definitely helps in improving performance and scalability of a website.

When not to use a CDN?
- Whenever you’re working offline.
- When you can deliver faster than the CDN, and care about that. This might be the case when all users are close to the server.

Privacy and security concerns.
Using the CDN, you are trusting the CDN to faithfully serve contents and relying on no third-parties injecting funniness in between the CDN and your user’s browser. It’s good to use a reliable CDN like “Google’s The AJAX Libraries API“

The AJAX Libraries API is a content distribution network and loading architecture for the most popular, open source JavaScript libraries. By using the Google AJAX API Loader’s google.load() method, your application has high speed, globally available access to a growing list of the most popular, open source JavaScript libraries including: jQuery, jQuery UI, Prototype, script.aculo.us, MooTools, Dojo, SWFObject and YUI

Try it in your web application to see performance improvement in your application.