Tuesday, September 15, 2009

Web Server and Application Server

Web Server: It is a server which host web application. Form Client Point of View, Server know only two thing:

1.HTTP Request
2.HTTP Response

Application Server: It is a server which host application. From client point of View, Application server is who provide them a way to interact business application. By use of Application Server and business application, client can ask question and get answers (Invoke methods and get Results).Application server can be any language like .net application server, Mainframe Application Server, J2EE application Server.

J2EE compliant Application Server: It is an application server who meets the J2EE specifications.

Converting a Chinese/ other language string into Unicode (UTF-16)

String b = --some chinese or other language text ---

StringBuffer str = new StringBuffer();
for(int i=0; i<b.length(); i++)
{
str.append(Integer.toHexString(b.charAt(i)));
}
System.out.println(" changed:--- "+str.toString());

Tuesday, September 8, 2009

Redirect And Forward

Redirect Versus Forward

1. Redirect sends the request back to browser and all previous request context and parameters are lost and a new request will be send to redirected page.
Forward does the decorator work for the request and can add more parameters to the request and send to the forwarded page.
2. Redirect is slower than Forward
3. Redirect will show the redirected page URL in address bar. Forward will not.
4. On refresh, only redirected page will be called not the page which has redirected i.e. if the flow is from A to B then on refresh only B will be called.
But in case of forward, if the flow is A to B then on refresh First A and then B will be called in the same sequence as occurred before.
5. Redirect can be used to send the request to another running web application. Forward will work within a web application only.
6. Redirect is done when servlet’s service method get completed i.e. statements after redirect call will execute first and after completion of service method redirect will occur.
But forward does the work of a method call i.e. the statements after forward call be executed when the forwarded page has done its work.
Example with a code snippet.
FirstServlet.java
...
System.out.println("Before Next Page");
response.sendRedirect("NextServlet");
System.out.println("After Next Page");

In NextServlet.java
System.out.println("Next Servlet called....");

In this case following will be the output:
Before Next Page
After Next Page
Next Servlet Called

I.e. in case of redirect first servlet ‘s service method will complete and then next servlet is called..
Now if we could have used forward instead of sendRedirect then following will be the output
Before Next Page
Next Servlet Called
After Next Page

Declaring and iterating over 2D array in javascripts.

Declaring and iterating over 2D array in javascripts.

A Code snippet. Self explainatory.

var b = new Array( 2 );
b[ 0 ] = new Array( 3 );
b[ 1 ] = new Array( 3 );

for ( var i in grades )
{

for ( var j in grades[ i ] )
{


}
}

Friday, June 19, 2009

How driver manger works internally? (in java)

DriverManager has a static registry of JDBC drivers. It has static methods registerDriver and unregisterDriver. When you load a JDBC Driver class, it calls the registerDriver method and registers itself. As many drivers can be registered to the DriverManager as you want. The order of registered drivers is significant. Because when you pass the connectionURL to the method, DriverManager.getConnection(), then it calls acceptsURL() one by one on each driver and the first driver which returns true, is used to get a connection.

Different ways of getting database connection

There are two ways of getting JDBC connection in java. One is from Driver Manager and the other is from an object of javax.sql.DataSource. The data source can be obtained from JNDI look up or by any other means. The implementation of Data Source internally uses the DriverManager but can pool the connections and reuse them and can give wrappers on actual connection so that when you call the close method, the connection is returned to the pool instead of actually closing the underlying connection.

Thursday, June 18, 2009

Get query string from PreparedStatement

When performing an SQL query, If you would like to catch any SQLException and throw a RunTimeException containing the failed query, like:

try {
String YOUR_QUERY = "SELECT * FROM table WHERE id = ? ";
Preparedstatement stamnt = con.prepareStatement( YOUR_QUERY )
stamnt.setString(1, 100);
stamnt.executeUpdate( );
}
catch (SQLException exc) {
throw new RuntimeException( );
}

where query would be "SELECT * FROM TABLE WHERE id = 100". However, it it is not possible to retrieve this query from a prepared statement.
There are two ways to solve this problem:-

(1) the easiest way is encapsulate the parameters and query string in the exception. It will not solve your problem 100%, but at least it will be enough descriptive to handle the error. Code snippet as below-

String param = 100;
String YOUR_QUERY = "SELECT * FROM table WHERE id = ? ";
try {
Preparedstatement stamnt = con.prepareStatement( YOUR_QUERY );
stamnt.setString(1, param);
stamnt.executeUpdate( );
}
catch (SQLException exc) {
throw new RuntimeException(YOUR_QUERY + “ parameters are :- ” + param );
}

(2) You can create a utility method in which we will pass the query string and list of parameters. Simply replace all the ‘?’ with corresponding parameters. Code snippet as below-

String param = 100;
String YOUR_QUERY = "SELECT * FROM table WHERE id = ? ";
try {
stamnt.setString(1, param);
Preparedstatement stamnt = con.prepareStatement( YOUR_QUERY );
stamnt.executeUpdate( );
}
catch (SQLException exc) {
String query = getQueryString(QUERY_STRING, param);
throw new RuntimeException(query);
}

String getQueryString(String query, Object[] params) {
    int idx = query.indexOf("?");
    int count = 0;
    while (idx != -1) {
        Object value = params[count++];
        if (value instanceof String) {
            value = "'" + value.toString() + "'";
        }
        query = query.replaceFirst("\\?", value.toString());
        idx = query.indexOf("?");
    }
    return query;
}



Wednesday, June 17, 2009

Software Engineering and Social Life

We all know that Society and technology are tightly coupled together. For a layman technology means some mechanical or chemical or electrical or medical inventions but not software engineering.

But software industry too can contribute a lot to society that can have an impact on social life of human being. You can clearly imagine the way people connect with each other. Most of the people like me are now connected to their peers with this most obvious invention of software industry – the internet.

There are lots n lots of social networking site through which we can remain in touch with our friends and family members and take regular updates. Sites like http://www.facebook.com/ and http://www.orkut.com/ are most famous social networking sites. Similarly there are lots of professional networking sites and http://www.linkedin.com/ is the most effective of all.