0 comments 2010-01-17

I am using Eclipse since few years and always liked their basic settings of font and appearance, but few days back when I was debugging a large source code file, I had to scroll up and down a lot even though those code sections were just 30-40 lines apart. So I thought of changing font size to see more code in my small laptop screen and debug it fast, but to my surprise when I went to change font size in preferences section, I was confused with so many options shown to me. I can understand by giving these many options they are allowing users to customize every part of IDE but it could have been lot simpler. Anyways, I wasted lot of time to figure it out so, I am putting my findings here to help other poor souls like me.

  1. In Eclipse, go to Window > Preferences > General > Appearances > Colors and Fonts
  2. Don't panic if you see lot of options, go to 'Basic' node and select 'Text Font'.
  3. Click 'Change' button and change the font size as per your requirement.
Doesn't it look so easy when you read this article? It is, but for new users this is not at all intuitive.
Anyways, Happy coding!

0 comments 2010-01-14


Not only Munna Bhai, I also saw Bapu's bhoot :)

















Shot @ Nandi Hills, Karnataka, India

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());