I had a situation in my code where I needed to dynamically execute a method based on a String I received from some other method. The string specified the method name I had to execute.
The easiest option was to put in an "if...else" block and do string comparison. However, that's not the cleanest way. I was looking for a way in Java to imitate javascripts eval behavior.
Reflection came to my rescue. Here's how you can dynamically invoke methods in your code:
Example A - Invoking a method that does not have any parameter
Assume you need to call method methodA() in class com.examples.Myclass
Class c = Class.forName("com.examples.Myclass");
Object objMyclass = c.newInstance();
Method m = c.getMethod("methodA", null);
m.invoke(objMyclass , null);
Example B - Invoking a method that accepts a String parameter and returns a String
Assume you need to call method methodB(String param1) in class com.examples.Myclass
Class[] clazzez = new Class[1];
clazzez[0] = Class.forName("java.lang.String");
Object[] params= new Object[1];
params[0] = "Parameter Value";
Class c = Class.forName("com.examples.Myclass");
Object objMyclass = c.newInstance();
Method m = c.getMethod("methodB", clazzez);
String return = (String)m.invoke(objMyclass, params);
In both the examples you need to handle the appropriate exceptions. For more reference, refer to package Reflection APIs and you can also check out these examples.
Let me know if you get stuck somewhere or come up with some other challenges.
Friday, February 26, 2010
Wednesday, February 24, 2010
Finding out geographic details from IP address
I am working on a web project currently where to enhance the user experience I tried to guess the user's location and do something with it. The only information I had about the user(since all of these are anonymous) was their IP address. I was looking for some services that would do the IP address lookup and tell me either the city or lat/long of the IP address.
After much googling, I came across a service provided by Geobytes which seems to suffice my requirement and looks to be quite accurate:
They also allow you to programmtically access this service. Try this link :
http://www.geobytes.com/IpLocator.htm?GetLocation&template=Y.txt&IpAddress=XXX.XXX.XXX.XXX
where Y is the response format [xml, php3, json, etc.]
and XXX... is the IP address you want to lookup
They also have few more interesting services that seem to be helpful for localizing content based on users location.
Thanks Geobytes for exposing these services.
After much googling, I came across a service provided by Geobytes which seems to suffice my requirement and looks to be quite accurate:
They also allow you to programmtically access this service. Try this link :
http://www.geobytes.com/IpLocator.htm?GetLocation&template=Y.txt&IpAddress=XXX.XXX.XXX.XXX
where Y is the response format [xml, php3, json, etc.]
and XXX... is the IP address you want to lookup
They also have few more interesting services that seem to be helpful for localizing content based on users location.
Thanks Geobytes for exposing these services.
Monday, February 22, 2010
Setting cookies in JSP
Tuesday, February 16, 2010
Calculating distance between two points on Earth identified by their Latitude/Longitude
Coming soon...
http://www.usnaviguide.com/zip.htm
http://www.usnaviguide.com/zip.htm
Friday, February 12, 2010
Utilizing Google Webmaster Tools
As a website owner it makes a lot of sense to utilize the Webmaster tools offered by Google. The tools help you to visualize how your site would appear to the Google crawler and where would your site's content appear on Google Search Results.
The tools allow you to tell Google what content to index and what to leave out. If certain existing pages are removed from your site, the tools allow you to notify Google about removing these from their indexes. You can define an optimal crawl rate for your website.
The tools also tell you about user search queries that lead to your website, information about other sites that have linked to your website, suggestions to make websites more "Google Search" friendly, information about broken links and other common HTML errors.
Best of all, its free :)
The tools allow you to tell Google what content to index and what to leave out. If certain existing pages are removed from your site, the tools allow you to notify Google about removing these from their indexes. You can define an optimal crawl rate for your website.
The tools also tell you about user search queries that lead to your website, information about other sites that have linked to your website, suggestions to make websites more "Google Search" friendly, information about broken links and other common HTML errors.
Best of all, its free :)
Labels:
Crawl,
Google Webmaster,
Search Results,
Website Analytics
Wednesday, February 10, 2010
Embedding Google AdSense in my Blogger
I am a big Google fan. I admire their engineering team and the ease with which they allow other developers to integrate Google's offerings into their site. Yesterday, I wrote about how I could integrate Google Docs into my applications and today, when I tried to put their AdSense program into this blog, it was a cake walk. Here's how I did it:
- Sign up for Google AdSense account. Google will verify your details and if everything is OK, they will send u a mail regarding your account
- Sign in to your Blogger account
- Click on the customize link on top right corner
- Click on the Layout tab
- In the "Add and Arrange Page Elements" there is a link at the bottom for "Publish ads with Google AdSense"
- Click on that link
- Select the layout pattern you prefer
- It will ask for your AdSense account. Enter the details and that's it
- Click Next and you are all set
Hats off to the Google and the Blogger engineers.
Tuesday, February 9, 2010
Integrating Google Docs with my application
I was able to successfully integrate Google Spreadsheets into my website. This gives my users an online spreadsheet experience they had been asking for a long time. It helps them work offline on MS-Excel and then be able to share it online. Google Spreadhseets integration has had some challenges, but eventually I was able to do it. The main issue that I faced was figuring out which API to use, whether to use the Docs API or to utilize the Spreadsheet APIs. Eventually I figured out where to use what. Also a big missing feature in the APIs is the ability to publish the speadsheet programmatically. Wish that the Google team includes this feature quickly.
Subscribe to:
Posts (Atom)