Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts
1 comments 2010-03-20

Recently I was reading books about Object Oriented design and analysis to refresh my designing skills. While doing that I came across few interesting design principles, I hope these will be helpful to new developers/designers
  1. Identify the aspects of your application that vary and separate them from whats stays the same.
  2. Program to an Interface, not an implementation.
  3. Favor composition over inheritance.
  4. Strive for loosely coupled designs between objects that interact.
  5. Classes should be open for extension, but closed for modification.
  6. Depend on abstractions. Do not depend on concrete class.
  7. Talk only to your Immediate friends - Principle of least knowledge.

0 comments 2010-03-04

These days many websites prefer to have fixed width body which is horizontally centered, and if you also want to create website like that, you would probably use "margin:auto;" style in your main container div inside body. This will work fine in most of the CSS2 and CSS3 compliant browsers(Firefox, Chrome) but in IE this page would not be centered!
Click here to see the Demo in IE.
You might be thinking "WTF is wrong with IE??".
Well there are many things which are wrong with IE, and this is just one of those.
Here is a workaround to fix this issue, use "text-align:center;" in BODY to align 'block' elements like "DIV". YUI also does the same in their reset css.
Click here to see the Demo with IE fix.
By now you must have noticed that we have to fix side effect of text-align when we use this approach, if you haven't noticed that in Demo, then again have a look at it.

0 comments 2010-02-12

As a web application developer, I tend to use XSLs very frequently, so I used the following technique to test my XSLs without spending money in buying professional XML/XSL editors like Oxygen. This trick will quickly allow you to test your XSL using XALAN's command line utility:

  1. Copy XML, XSL and XSLT processor jar in one folder. I am using XALAN(Download it from here) with jre 1.6(which should be installed on your machine). If you want to run this in many folders, you should put xalan.jar in one central location and add it to your CLASSPATH
  2. Run the following command in that directory 
  3. C:\test > java -cp c:\xalan.jar org.apache.xalan.xslt.Process -IN input.xml -XSL transformer.xsl -OUT output.xml
  4. output.xml is ready

To learn more about this command line utility, visit http://xml.apache.org/xalan-j/commandline.html and start saving money and time.

0 comments 2010-01-13

What are Cookies?
Cookies are small strings of data of the form name=value. These are delivered to the client via the header variables in an HTTP response. Upon receiving a cookie from a web server, the client application should store that cookie, returning it to the server in subsequent requests. The browser is expected to support 20 cookies for each Web server, 300 cookies total, and may limit cookie size to 4 KB each.
By having the server read information it sent the client previously, the site can provide visitors with a number of conveniences:

  1. Identifying a user during an e-commerce session.
  2. Avoiding username and password.
  3. Customizing a site as per user preferences.
  4. Focused advertising.
How to add Cookies to response header?
Cookie userCookie = new Cookie("user", "user_blr_2010");
userCookie.setMaxAge(60*60*24*365); // (Optional)
response.addCookie(userCookie);
How to get Cookies from request?
Cookie[] cookies = request.getCookies();
Cookie cookie;
for(int i=0; i<cookies.length; i++) {
 cookie = cookies[i];
 out.println("<TR>\n" +
 " <TD>" + cookie.getName() + "\n" +
 " <TD>" + cookie.getValue());
}
How to find Cookies with Specified Names?

public static String getCookieValue(Cookie[] cookies,String cookieName,String defaultValue) {
  for(int i=0; i<cookies.length; i++) {
    Cookie cookie = cookies[i];
    if (cookieName.equals(cookie.getName())) {
      return(cookie.getValue());
    }
  }
  return(defaultValue);
}
public static Cookie getCookie(Cookie[] cookies, String cookieName) {
  for(int i=0; i<cookies.length; i++) {
    Cookie cookie = cookies[i];
    if (cookieName.equals(cookie.getName()))
      return(cookie);
    }
    return(null);
}

How to use Java.Net.* API to handle Cookies?

Setup URLConnection to the URL:
URL myUrl = new URL("http://www.testcookies.com");
URLConnection urlConn = myUrl.openConnection();
urlConn.connect();

Header fields comes as Map so find the value which has key="Set-Cookie", and get that field:

String headerName=null;
for (int i=1; (headerName = uc.getHeaderFieldKey(i))!=null; i++) {
  if (headerName.equals("Set-Cookie")) {
    String cookie = urlConn.getHeaderField(i);
  }
}

The string returned by the getHeaderField(int index) method is a series of name=value separated by semi-colons (;), so if we want to find out first cookie:

cookie = cookie.substring(0, cookie.indexOf(";"));
String cookieName = cookie.substring(0, cookie.indexOf("="));
String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length());