Tuesday, August 18, 2009

Creating an HTML layout that is cross browser x Dummies

This post is mostly for who have to use an HTML template generated by an external Web designer and making it “dynamic”, that means we are handed the html page and we have to create the template that will be used by a web application to render content using that templates like polopoly (www.atex.com) but it is also true for other systems like joomla.

1. do not use cellpadding and cellspacing in tables, set them to 0 and eventually use CSS to control spacing, firefox, chrome and internet explorer interprets them differently.

2. do not use SPACER tag since internet explorer will not understood it, use a transparent 1 pixel gif with an img tag instead.

3. do not comment a css line with // since internet explorer will read it anyway, comment using /* and */ if you really need to.

4. if you start doing evaluate the layout with internet explore it would be more easy to adjust it for firefox and chrome (if any adjustment needs to be made).

5. always start with IE6 if you need to target it since it is the poorer browser that probably will need more care.

Wednesday, August 05, 2009

How to execute a command line tool from a servlet

In a java program when you want to execute an external program you will normally write something like this:

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);

Where cmd is the command line needed to be executed.

However, if you run the same piece of code from a tomcat servlet it may just hung up until you kill it. The problem is documented in the documentation of the exec method, you need to consume the inputStream and the outputStream, so a quick way of doing it is to start other two threads that will consume those stream.

First create the thread class that will process the stream:

class StreamGobbler extends Thread
InputStream is;
String type;

StreamGobbler(InputStream is, String type)
this.is = is;
this.type = type;

public void run()
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null) {
System.out.println(type + ">" + line);
} catch (IOException ioe) {

Now we can modify the previous code to handle InputStream and ErrorStream:

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd);
StreamGobbler errorGlobber = new StreamGobbler(proc.getErrorStream(), "CMDTOOL-E");
StreamGobbler outputGlobber = new StreamGobbler(proc.getInputStream(), "CMDTOOL-O");

et voilĂ  the code will execute just fine in a servlet.

The idea and code has been taken from: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1