Archive for the 'Java' Category

28 Programming Tutorials by Dr. Herong Gyang

Wednesday, July 16th, 2008

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.

Parsing HTML with Java Libraries

Thursday, July 10th, 2008

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.

javax.net.URL HTTPS Connection

Wednesday, June 25th, 2008

Code snippet to open HTTPS connection to website. This will work on JDK1.4.2 onwards.

For JDK1.3 & below it will throw “Malformed URL Exception: HTTPS”. The reason is connecting to HTTPS requires SSL Handshake between client (your Java class) & Server (in this case paypal.com). When your Java class opens HTTPS connection, the Server will send its SSL Certificate. The JDK URL connection class will need to first validate the Server SSL certificate.

Historically, RSA Security has patent rights for encryption algorithms being used in SSL Handshake. All major browsers have licensed encryption from RSA Security to include in their browsers like Firefox, Opera, IE, etc. When Sun shipped previous versions (i.e. JDK1.3, 1.2, etc), it had not licensed RSA Security algorithms. Later Sun packaged in JSSE (Java Security Extensions) as separate jar. If you want to use URL class for HTTPS connections, pls include jsse.jar in your classpath. JDK1.4.2 onwards, Sun has included the jsse.jar in lib files.

Source:

http://www.rohitblog.com/wp-content/uploads/2008/06/testhttpsurljava.txt

eoSense’s Profiler for Java Performance Tuning

Saturday, May 31st, 2008

Recently one of my projects (Java-based) faced slow response issue. Now this site has been functional for almost 3-4 years now, & this is the first time we came across such issue.

Guess, we kept rolling feature after feature & user base kept growing. Alas! It had to take it’s tool sometime. Now, time you roll up sleeves & get your hands dirty…in code.

Now this post is not where I am going to share wealth of knowledge- a little ‘Googling’ will find you many articles on Java performance tuning. I will do that, but once we fix every last bit of bottleneck.

I came across real nice & easy tool - eoSense that offers you tool to analysis your code. In simpler terms, it’s class of software called “Profiler”. These tools would make use of JVMPI (Java Virtual Machine Profiler API) to analsye your program runtime & give you idea of what could be source of problems- like memory leaks, CPU hogging, a thousand object created, etc.

eoSense Profiler’s value proposition is that it will show what is problem & line by line source of issue- so it can point you well, database connection is not released or a loop creates excessive objects, or may be hey you still hold references to dead objects. I hope you will get more, when you watch their online demo.

Backdrop- the tool only runs on Websphere 6.0 & BEA WebLogic versions 6.1, 8.1, 9.1, 9.2 and 10.0. As per my discussion with eoSense’s sales representative, version for Websphere 5.0 is still under works.

Java Regular Expression

Wednesday, December 13th, 2006

Regular expressions in Java can help you in two ways - validating user input and find and replace text. Here’s a good tutorial on Sun’s website on what is Regular expression and how to use them. Most of the syntax of regular expressions in Java comes from Perl. (if you don’t know Perl…well you don;t need to learn perl!)

Apache Struts Resources

Saturday, November 11th, 2006

I’m now working on a development project for designing web application for MNC company. Technology - we decided to use apache struts which provides a very good implementation on MVC2 framework. I’ve created a page where I’ll share any interesting and helpful articles, tutorials on Struts. So zoom off to Struts resources page.

UnsupportedClassVersionError

Wednesday, October 11th, 2006

Apart from out of memory error, here’s another error from java language. Find an explanation on this –> java.lang.UnsupportedClassVersionError


Dropdown Design Pattern - Tech

Monday, August 21st, 2006

Hi, today’s post is one of my creations in technical field. So, if you’re tech-hearted, go on, read it and I’m sure you’ll be happy you did. Well, if you’re in web applications developement (all those sites you may have visited like ebay.com or amazon.com, or simply your internal company site, in short you put/pull some data in/out database;)). Anyway, my tech-mates, if you develop sites, you sure would have to maintain many dropdown values - this I bet you would either just hardcode or fetch values from database. I’ve tried to come up with a design pattern (that’s in JAVA) that can help you with proper maintenance of dropdown values with clean, simple design. So, if you want to impress your senior, go ahead, copy it and tell that you’ve “invented” (aaha..) this design. I want to see Dropdown Design Pattern.