Thursday, March 5, 2009

Encoding URL's to access a resource over HTTP in Java

In java the URI class is more flexible to construct much more flexible URL's and also supports encoding.

URI uri = new URI("http","localhost","/temp/XYZ.xsd",null);
URL url = uri.toURL();
URLConnection urlConnection = url.openConnection();
urlConnection.setDoOutput(true);    

Quote from the sun documentation "The recommended way to manage the encoding and decoding of URLs is to use URI, and to convert between these two classes using toURI() and URI.toURL()."  I did see this only on the 1.5 version not on 1.4 version of the doc. 

Setup apache to serve xml files.

Apache by default is configured to serve html files, if you want to setup Apache to serve other files then you have to edit the "httpd.conf" file. This file is usually located in your apache installation folder conf directory, open the file using notepad search for the following tag "<IfModule mime_module>" within this tag you have to tell apache to serve other content types, you have to add the new type that you want to publish with the following values

    AddType application/xhtml+xml .xhtml .xml

    AddEncoding xhtml xml

The first variable defines the type of media and the second tell the encoding that you want to use, this will instruct the browser to handle these media types respectively. For a list of registered media types see here.