Oct 5 2005

Class HighLite InetAddress

What I try to do in this Class HighLite series is to bring to focus a single class which I have found very useful but which I don’t use often enough. That almost sounds like a paradox but it is not and InetAddress will prove it. Please note that in when in doubt read the Java documentation, as it is the best available source of information on the Java API.

InetAddress net = InetAddress.getLocalHost();
String hostName = net.getHostName();

Once you have created an InetAddress object, I have found the getHostName the most useful. The getHostName, as you may imagine, returns the host name of the computer executing that line of code. You can use this, for example, in an about dialog box along with the software version and available resources.


Oct 2 2005

Generic Java

I have been using Java 1.5, or as Sun calls it Java 5.0, with Eclipse 3.1.0. One of the kewl new features of Java 1.5 is generics (which C++ has had for over ten years). If you use generics with a list you don’t have to keep casting to an expected object. When you cast you pretty much are guessing about the types contained in a collection. If you use generics you can guarantee the type of in a list or hash map, in fact it is used by the compiler for type checking. To get started lets create a list using generics:

List<String> myList = new ArrayList<String>();
// fill my list with string objects
for(int i=0; i < myList.size(); i++)  {
	String item = myList.get(i);
	System.out.println(item);
}

See. No cast required. Here is the same snippet using the new for/in loop to mix things up a bit.

List<String> myList = new ArrayList<String>();
// fill my list with strings objects...
for(String item : myList)  {
	System.out.println(item);
}

To illustrate Java generics with another example, here is a snippet of code that uses a map instead of a list.

Map<String, String> myMap = new HashMap<String, String>();
// fill my map with string key and value objects...
Iterator<String> i = myMap.keySet().iterator()
while(i.hasNext)
	String key = i.next();

Again, the benefits of using Java generics is compile type checking so that you won’t be surprised with a ClassCastException at runtime during an important demo.


Oct 2 2005

CMD Complete

This tip is for tech noobies. In some UNIX-flavored shells you can auto complete a directory path by hitting the esc key twice. And of course Microsoft Windows tailgates with its on version for its CMD command line prompt by, you guessed it, using another key, the tab key. At least Microsoft didn’t invent its own key for this like they did with the start key. It is always so confusing when you have to switch between Linux and windows and back.


Sep 27 2005

Import Script, Import CSS, Import PHP

Just because sometimes I forget, here are some code fragments to import JavaScript, CSS, and PHP files.

JavaScript:

<script
   type='text/javascript'
   src='directory/file.js'>
</script>

CSS:

<style type='text/css'  media='all'>
   @import 'directory/file.css';
</style>

Icon:

<link
   rel='Shortcut Icon'
   href='directory/file.ico'
   type='image/x-icon'>

PHP:

<?php @ require_once ('directory/file.html'); ?>

For the PHP import, by placing the @ symbol before require_once suppresses any error messages that might be generated in the included PHP file.

JSP:

<%@ include file='jspfile.jspi' %>

And, well, I guess the list can go on and on. You can use Struts Tiles to include other JSP snippets. I am sure that other languages and frameworks have the same functionality.


Sep 24 2005

Hide Your HTML Source

Sometimes you might not want to allow guests to your website to view the HTML source code. You can disable the right click context menu by using the following bit of code in your page:

<body oncontextmenu='return false;'>

You can use this feature to prevent users from right clicking and saving an image from your site. This is support by IE and Firefox. I have seen sites use similar functionality to this to pop up an alert with a copyright message is a user tries to right click on an image.

Keep in mind that a user can always disable JavaScript altogether, view the source from the application menu, and/or even save the whole HTML file onto his/her desktop and view the source from Notepad.


Sep 17 2005

Delete Temporary Files on Exit

Sometimes your application requires holding data in a temporary file. I’ve had to do this when printing a PDF file. The following bit of code demonstrates how to create a temporary file and delete said file when the program terminates.

File temp = File.createTempFile("SomePrefix", ".pdf");
temp.deleteOnExit();

The file is created in the system’s temp directory, see the Java java.io.tmpdir property.