Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts
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());