Wednesday, January 16, 2008

Using JAXWS 2.1 on Java 6.0

To use JAX-WS 2.1 on Java 6.0 and Tomcat 6.0, place all the libraries in the JAXWS 2.1 in “<TOMCAT_HOME>\endorsed” directory. To run the client, you have to have the JAX-WS 2.1 library (jaxb-api.jar) in your bootstrap. For this put the jaxb-api.jar from the JAX-WS 2.1 distribution in the "<JAVA_HOME>/jre/lib/"endorsed.

Friday, January 11, 2008

Why SiteMesh instead of Tiles?

  1. SiteMesh is much easier to configure and use (less typing and easier to understand)
  2. Using Tiles, you need to have your forwards go to a "tiles page" versus the direct JSP. SiteMesh takes the approach that your page (your JSP) doesn't even know or care that it's being decorated.
  3. Using Tiles, each individual page you want to go to has to be associated with a layout - Major pain! Every time you create a new JSP that you want to forward to, you have to create another tiles definition and associate it with a layout and forward to the Tile page (versus the JSP). With SiteMesh you can simply set up a URL pattern and all your pages are decorated with the layout you choose.
On the other hand there is downside of using SiteMesh. SiteMesh stores the entire content of your HTML body into memory before it decorates it. If you have some very large pages, such as might happen in a reporting application where you don't have pagination implemented and end up with one large page of rows, you could end up with severe memory problems.
You can read this http://today.java.net/pub/a/today/2004/03/11/sitemesh.html post by Will Iverson about how to use SiteMesh.

Thursday, December 13, 2007

Accessing the properties file outside the Jar file.

To access the .properties file when the file is outside the jar you just need to enter the classpath value in the manifest.mf file: Manifest-Version: 1.0 Main-Class: pack.HelloWorld Class-Path: . Multiple classpath requirements will be space separated and not ; separated i.e. You have to mention all the jar names. Class-Path: servlet.jar infobus.jar acme/beans.jar acme/mysql.jar For properties in a folder named config, you can put in class path as Class-Path: servlet.jar infobus.jar config/.

Programmatically compiling the Java program using Compiler API

Sample code to programmatically compile a Java program.
public class CompilerExample {
    public static void main(String[] args) {
        String fileToCompile = "c:" + java.io.File.separator
                + "HelloWorld.java";
        JavaCompiler compiler =      ToolProvider.getSystemJavaCompiler();
        int compilationResult = compiler.run(null, null, null, fileToCompile);
        if (compilationResult == 0) {
            System.out.println("Compilation is successful");
        } else {
            System.out.println("Compilation Failed");
        }
    }
}

Creating an image file (jpg) using java

You can create the custom images using java with the following snippet. This can be productively used in sign up forms to make the user enter the text in the image manually (to avoid automated script).
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); 
 Graphics g = image.getGraphics(); 
 g.drawString("Hello World!!!", 10, 20); 
 try {  
   ImageIO.write(image, "jpg", new File("c:/CustomImage.jpg")); 
 } catch (IOException e) {  
  e.printStackTrace(); 
 }