28 Programming Tutorials by Dr. Herong Gyang

July 16th, 2008 | Print This Post/Page | E-Mail This Post/Page

Just wanted to share a link- HerongGyang. Dr. Herong has been writing tutorials on programming languages. The site has a extremely thorough collection of 28 tutorials on different programming languages. While working on my assignments with JAVA/ SQL Server, his in-depth articles helped me lot with insider information which is hard to find on internet. What’s more, even Google ranks his site on top.

IBM DB2- useJDBC1 or useJDBC2?

July 16th, 2008 | Print This Post/Page | E-Mail This Post/Page

If you would have used Java programs to connect to IBM DB2 v7.2, you have a choice to use JDBC 1.0 or JDBC 2.0. You can use JDBC MetaData API to get the JDBC Driver version.

An excellent resource on history of JDBC can be found at Herong’s website.

Using JDBC2.0 has helped us solve many issues, particularly with some methods used in storing BLOB/ CLOB data.

By default, DB2 v7.2 provides JDBC 1.0 driver files. To use JDBC 2.0, the trick is to run a batch file that comes shipped with DB2 v7.2. Have a look in your “d:\Program Files\SQLLIB\java12″ directory. You will find two batch scripts: useJdbc1.bat & useJdbc2.bat. Now run “useJdbc2.bat” that will replace the driver files to JDBC2.0. While running batch file, you have closed your WSAD or DB2 instances.

Eavesdrop Browser - Server Talks

July 15th, 2008 | Print This Post/Page | E-Mail This Post/Page

Recently I was working on an official assignment to “talk” to a web server. In short, it was to simulate user’s action on a browser, but without a ‘browser’. Means, write a program to simulate user actions…blah.

With such tasks, you just can’t do without a packet sniffer. For e.g., you want to see which ‘cookies’ are being sent, what data is exchanged between web server & browser.

There are couple of tools available in market. I tried three tools, IE Inspector’s HttpAnalyser, Firefox add on “Firebug” & Fiddler.

IE Inspector’s HttpAnalyser - this is shareware (not free); though you can download a trial version. The user interface is intuitive, easy to use & provides lot many details. You can choose which browser process to “eavesdrop”. There is a request builder, to be used to manually generate “request” to the webserver. This helps to analyze specific request types you may be interested.

Firefox add on “Firebug”: A good add on to Firefox. Lot of my colleagues suggested to use Firebug. For e.g. when you want to analyze performance/ response times of the website. Or if you want to check the size of the HTML pages being returned.

Fiddler - HTTP Debugging Proxy which logs all HTTP traffic between your computer and the Internet. I didn’t used this tool much, as by that time, I was already comfortable using HttpAnalyser & Firebug. But I didn’t find it very useful either.

Of all these, I liked HttpAnalyser. All the features above & much more is available in HttpAnalyser. You can export all debugging data to HTML, XLS formats.

Parsing HTML with Java Libraries

July 10th, 2008 | Print This Post/Page | E-Mail This Post/Page

I have been trying to parse HTML using Java libraries for some time. This is related to my assignment involving using Apache HttpClient to send specific GET/ POST request to web server & analyze the response. Parsing HTML can be tricky presuming that you may not always get correct HTML tags from the server. Unlike XML which has a schema, HTML in wild wild web is not guaranteed & often does not meet standard HTML specifications.

When it comes to Java, there are literally 10+ libraries available to parse an HTML file. There are many open source libraries available under GNU license. Here, I am not going to comment on which is the best library. But I will share my experiences on the three libraries I tried.

  • JTidy- JTidy has simple API that reads HTML source & returns a Java DOM object. From there on it’s a simple manipulation of DOM to retrieve your desired element.
  • TagSoup - is another HTML parser library. Unlike JTidy which returns DOM object, TagSoup is SAX complaint. SAX is a serial based parser with callback events called as each tag is encountered during parsing. Callback events does than business processing. SAX does not keep the whole HTML in memory unlike DOM based parsers which will load full HTML in memory before allowing to parse. DOM has its own advantage where you need to be able to randomly locate any tag without first having need to parse whole HTML. I hope you can do more investigation & decide which approach is better for your application.
  • Java Swing - Starting JDK 1.4.2, Sun has included Swing framework which is primarily used to create user interface for Java-based applications. The “HTMLEditorKit” Java class can be used to parse a HTML file.

Though I tried above 3 approaches, I found TagSoup to be working to me specific requirement- which was to parse HTML file to read value of some hidden variables. For some unknown reasons, Swing was not able to read those hidden & I could not find way to extract details with DOM API while using JTidy.
Resources:

SimpleDefaultHandler: A simple handler class extending the SAX DefaultHandler class. It just handles two events- startElement() & endElement(), but I hope you will get an essence of what needs to be done.

Three Java Methods: feel free to use this code snippets to parse html using Swing, TagSoup or JTidy.

Stop Inflation- The Egg Way!

July 10th, 2008 | Print This Post/Page | E-Mail This Post/Page

Here’s a novel way to curb inflation doing rounds on internet. Please do pass this message to all your friends; you can click on the ‘email’ icon above to email this article.

A man eats two eggs each morning for breakfast. When he goes to the Kirana store he pays Rs. 12 a dozen. Since a dozen eggs won’t last a week he normally buys two dozens at a time. One day while buying eggs he notices that the price has risen to Rs. 16. The next time he buys groceries, eggs are Rs. 22 a dozen.

When asked to explain the price of eggs the store owner says, “The price has gone up and I have to raise my price accordingly”. This store buys 100 dozen eggs a day. He checked around for a better price and all the distributors have raised their prices. The distributors have begun to buy from the huge egg farms. The small egg farms have been driven out of business. The huge egg farms sell 100,000 dozen eggs a day to distributors. With no competition, they can set the price as they see fit. The distributors then have to raise their prices to the grocery stores. And on and on and on.

As the man kept buying eggs the price kept going up. He saw the big egg trucks delivering 100 dozen eggs each day. Nothing changed there. He checked out the huge egg farms and found they were selling 100,000 dozen eggs to the distributors daily. Nothing had changed but the price of eggs.

Then week before Diwali the price of eggs shot up to Rs. 40 a dozen. Again he asked the grocery owner why and was told, “Cakes and baking for the holiday”. The huge egg farmers know there will be a lot of baking going on and more eggs will be used. Hence, the price of eggs goes up. Expect the same thing at Christmas and other times when family cooking, baking, etc. happen.

This pattern continues until the price of eggs is Rs. 60 a dozen. The man says, ” There must be something we can do about the price of eggs”.

He starts talking to all the people in his town and they decide to stop buying eggs. This didn’t work because everyone needed eggs.

Finally, the man suggested only buying what you need. He ate 2 eggs a day. On the way home from work he would stop at the grocery and buy two eggs. Everyone in town started buying 2 or 3 eggs a day.

The grocery store owner began complaining that he had too many eggs in his cooler. He told the distributor that he didn’t need any eggs.
Maybe wouldn’t need any all week.

The distributor had eggs piling up at his warehouse. He told the huge egg farms that he didn’t have any room for eggs would not need any for at least two weeks.

At the egg farm, the chickens just kept on laying eggs. To relieve the pressure, the huge egg farm told the distributor that they could buy the eggs at a lower price.

The distributor said, ” I don’t have the room for the %$&^*&% eggs even if they were free”. The distributor told the grocery store owner that he would lower the price of the eggs if the store would start buying
again.

The grocery store owner said, “I don’t have room for more eggs. The customers are only buying 2 or 3 eggs at a time. Now if you were to drop the price of eggs back down to the original price, the customers
would start buying by the dozen again”.

The distributors sent that proposal to the huge egg farmers but the egg farmers liked the price they were getting for their eggs but, those chickens just kept on laying. Finally, the egg farmers lowered the
price of their eggs. But only a few paisa.

The customers still bought 2 or 3 eggs at a time. They said, “when the price of eggs gets down to where it was before, we will start buying by the dozen.”

Slowly the price of eggs started dropping. The distributors had to slash their prices to make room for the eggs coming from the egg farmers.

The egg farmers cut their prices because the distributors wouldn’t buy at a higher price than they were selling eggs for. Anyway, they had full warehouses and wouldn’t need eggs for quite a while.

And those chickens kept on laying.

Eventually, the egg farmers cut their prices because they were throwing away eggs they couldn’t sell.

The distributors started buying again because the eggs were priced to where the stores could afford to sell them at the lower price.

And the customers starting buying by the dozen again.

Now, transpose this analogy to the gasoline industry.

What if everyone only bought Rs 200.00 worth of Petrol each time they pulled to the pump? The dealer’s tanks would stay semi full all the time. The dealers wouldn’t have room for the gas coming from the huge tanks. The tank farms wouldn’t have room for the petrol coming from the refining plants. And the refining plants wouldn’t have room for the oil being off loaded from the huge tankers coming from the oil fiends.

Just Rs 200.00 each time you buy gas. Don’t fill up the tank of your car. You may have to stop for gas twice a week, but the price should come down.

Think about it.

Also, don’t buy anything else at the fuel station; don’t give them any more of your hard earned money than what you spend on gas, until the prices come down…”

…just think of this concept for a while.

………………please pass this concept around….reaching out to
the masses …the world …..

Importing SSL Certificate - How to Add to Webserver

July 9th, 2008 | Print This Post/Page | E-Mail This Post/Page

There are two steps involved in adding (i.e importing) SSL certificates to Webserver. I assume you are dealing with Java (any version) JVM. It doesn’t matter whether you are using Websphere, Weblogic, or any other J2EE application server, as long as you know the path to the Java home folder.

You can find out Java Home folder by this simple statement: System.getProperty(”java.home”).

Step1: Exporting SSL Certificate from IE6.0

Exporting SSL Certificate thro IE6

  1. IE6: Open the target website e.g. https://www.verisign.com
  2. Go to your Internet Explorer option menu “Tools / Options / Content”. Click button.
  3. Find certificate with Target Website name.
  4. Click button.
  5. Select the format as “DER encoded binary x.509 (.CER)”. Save on some location, say c:\

Step2: Use keytool from /bin directory to add the certifcate to TrustStore.

Java SDK comes with lot of tools under its /bin folder. Note: you need Java SDK & not just JVM installed. The ‘keytool’ program in /bin folder is used to import a SSL certificate to Java’s TrustStore.

For Windows,
keytool -keystore cacerts -storepass changeit -import -alias -file

.cer -trustcacertsFor Unix/ Linux
bash-2.05# ./keytool -import -file /tmp/.cer -keystore /jre/lib/security/cacerts -alias -trustcacerts

How does it work?
When URL connection class connects to a secure website (usually with https:// prefix), the Webserver would send SSL certificate to prove that it is indeed the one which you tried to connect. Now URL connection is hardwired (actually its another class called TrustManager) with logic to validate the SSL Certificate against the default factory TrustStore. The TrustStore usually lies in “jre/lib/security/cacerts” file. If the website’s SSL certificate is not valid, then URL connection class would throw a SSL Handshake Exception. So it’s important to add target website’s SSL certificate to the TrustStore.

Creating Custom TrustStore for Websphere 5.x

July 9th, 2008 | Print This Post/Page | E-Mail This Post/Page

To my query on creating a Custom TrustManager in Websphere, I got a reply. It talks about alternative way to trick your code into trusting the SSL certificate. Instead this solution below talks about using custom Truststore & configuring your server to check Truststore to validate web servers SSL certificates.
Assuming that you are doing plain HTTPS, first procure the server’s HTTPS certificate.

Next, you need to create a custom truststore that contains the server’s certificate. You can do this using the JDk keytool command:
C:\>keytool -import -file -alias

-keystore You will be prompted for a password that will be eventually used to protect the truststore.Once you have created the truststore, you need to tell your client’s JVM that you want to use that truststore. You can do that by specifying the following system properties:
javax.net.ssl.trustStore
javax.net.ssl.trustStorePassword.

For example you might use the following java command to run your client:
C:/>java -Djavax.net.ssl.trustStore=mytruststore.jks -Djavax.net.ssl.trustStorePassword=secret my.main.Class

There is a 3rd solution to this problem, which is importing the web servers SSL certificate into your JVM’s /jre/lib/security/cacerts’ file which I will discuss in future post. But sometimes, this approach may not be possible due to security implications & limited access owing to company policies & shared servers.

Generate JavaDoc for JavaScript

July 9th, 2008 | Print This Post/Page | E-Mail This Post/Page

There are couple of options if you want to generate a JavaDoc like documentation for your JavaScript. Generally, most of the time, the JavaScript you write will be collection of simple methods. But, if you do not know, you can create ‘class’ like structures & instantiate them as Objects. That’s one powerful Object Oriented feature of Javascript that many don’t know.

Okay, now coming to point- a documentation of your Javascript, be it functions or full fledged classes, comes in handy & can be major USP for letting developers use your JavaScripts.

One such tool, again from Open source community is “JsDoc”. You can locate it @ http://jsdoc.sourceforge.net/

JsDoc is a Perl script (you will need to install Perl on your comp to run it). Here’s how to download & install “Active Perl”- the most used Perl version for Windows.

JsDoc is not something like Magic trick to automatically add documentation. Like normal Java classes comments or any other language, it requires you properly document your code with appropriate Java like tags such @author, @class, @parameters. Using these tags will help JsDocs to generate JavaDocs for your JavaScript file.

Example, how you need to comment your JavaScript code:

                  /**
* Shape is an abstract base class. It is defined simply
* to have something to inherit from for geometric
* subclasses
* @constructor
*/
function Shape(color){
this.color = color;
}

// Bind the Shape_getColor method to the Shape class
Shape.prototype.getColor = Shape_getColor;

/**
* Get the name of the color for this shape
* @returns A color string for this shape
*/
function Shape_getColor(){
return this.color;
}

To ease your JavaScript coding, you can try some JavaScript editors or search more.


Cutest Software Bug

July 5th, 2008 | Print This Post/Page | E-Mail This Post/Page

This is something very old, way back in 2002. But it still as cute as it was before. A bug, in software engineer’s life- we all know frustation & remorse they are capable of generating. Have a look at this flash file (takes 5 minutes to load on dial up, with Firefox/ IE).

http://img.tapuz.co.il/forums/8572800.swf

Introducing Snap Shots from Snap.com

July 1st, 2008 | Print This Post/Page | E-Mail This Post/Page

I just installed a nice little tool on this site called Snap Shots that enhances links with visual previews of the destination site, interactive excerpts of Wikipedia articles, MySpace profiles, IMDb profiles and Amazon products, display inline videos, RSS, MP3s, photos, stock charts and more.Sometimes Snap Shots bring you the information you need, without your having to leave the site, while other times it lets you “look ahead,” before deciding if you want to follow a link or not.

Should you decide this is not for you, just click the Options icon in the upper right corner of the Snap Shot and opt-out.

Bloggers! Snapshot features, installation & wordpress plugin can be found here. Snapshot plugins are also available for other blogging platforms like blogger- install instructions available when you signup with snapshot here.

See for yourself, when you move your mouse over the ‘call-out’ icon next to a hyperlink, you will get snap window displaying image of target page.

Snap-demo