About Jxp

Jxp (Java scripted page) is a script-processor that process JSP-like files. It contains a parser to parse the script file into an abstract syntax tree and a tree processor (JxpProcessor) that will process the syntax tree to execute the code using reflection API to produce output. The main uses of Jxp are:

  • as a script language engine to increase flexibility in the user application
  • as a template engine to produce dynamic text output
Some of the main features of Jxp include:
  • Java as script/template language. Why learn another one? ;)
  • Run JSP-like code outside of servlet container
  • support common java language 1.4 constructs (partial 1.5 syntax support on jdk 1.4)
  • support common JSP constructs including import directive, declaration, EL etc (taglib not supported, yet)
  • practical template sources management framework
  • support caching of parsed syntax tree to eliminate reparse of template
  • a servlet implementation to enable web-scripting
  • extensible processing context for defining built-in function on the scripts
Below is an example of a script file
This is normal text
This is text with variable <%=var1%>
This is another text with variable `var1`
This is yet another text with expression `var1.toString()`

<!-- jsp style import -->
<%@ page import="java.util.*"%>
            
<%! String text = "text"; %>
<jsp:declaration>
	String anotherText = "text";
</jsp:declaration>

This is output jsp expression output <%=text%>
This is EL output ${text}

<%
	//scriptlet
    import java.util.*;
    
    //print
    println("This text is printed by jxp");
    
    //populate the list with 1 to 10
    var i = 0;
    List l = new ArrayList();
    for (int i = 0; i<10; i++)
    {
        l.add(new Integer(i));
    }
    
    //include another script
    include("config.jxp");
    
    //try and catch
    try 
    {
        File f = new File("data.txt");
    } catch (Exception e)
    {
        //do nothing
    }
    
    //function definition and function call
    function void f1(String arg){
        //do somethod 
    }
    f1("test"); //call it here
    
    //synchronize 
    Object o = new Object();
    synchronized (o)
    {
        //do something
    }    
%>