Code

Contains arbitrary java code. This code has access to all of the implicit page variables and is run as-is when the page is rendered.

Syntax

Code sections can appear anywhere in text sections.
[[
..java code here (can be any number of lines)..
]]

Tag-Escape

To prevent the tag from being recognized, this tag can be escaped by prefixing the start tag with a backslash: \[[

Example

A simple molly page containing a code section.

The code section prints the value of a java variable using the variable "out" (which is a PrintWriter that writes to the output stream of the page).

<html> <body> [[ String greeting = "Hello World"; out.println(greeting); ]] </body> </html> Hello World

Example

A page showing a code section that writes several lines using a java for-loop. <html> <body> [[ //this is java code for (int n = 1; n <= 5; n++) { out.println("Dream " + n " + " grows like slow ice <br>"); } ]] </body> </html> Dream 1 grows like slow ice
Dream 2 grows like slow ice
Dream 3 grows like slow ice
Dream 4 grows like slow ice
Dream 5 grows like slow ice

Example

This is a for-loop similar to the above example. Molly pages allows one to use HTML within java code sections that is the equivalent of using out.println(..) statements but are easier to write. (These are called Hash sections). <html> <body> [[ //this is java code with #..# escapes for (int n = 1; n <= 5; n++) { # Dream [=n] grows like slow ice <br># } ]] </body> </html> Dream 1 grows like slow ice
Dream 2 grows like slow ice
Dream 3 grows like slow ice
Dream 4 grows like slow ice
Dream 5 grows like slow ice

Example

A code section containing a nested for-loop that writes out a HTML table. <html> <body> <table> [[ for (int n = 1; n <= 5; n++) { out.print("<tr><td>Dream "); out.print(n); out.print(" grows like slow ice &nbsp"); for (int k = 0; k < n; k++) { out.print("<img valign=\"bottom\""); out.print("src=\"redball.gif\">"); } out.println("</td></tr>"); } ]] </table> </body> </html>
Dream 1 grows like slow ice  
Dream 2 grows like slow ice  
Dream 3 grows like slow ice  
Dream 4 grows like slow ice  
Dream 5 grows like slow ice  
Note, the following is equivalent to the above example -- but instead uses hash sections instead of out.println()

<table> [[ for (int n = 1; n <= 5; n++) { #<tr><td>Dream [=n] grows like slow ice &nbsp;# for (int k = 0; k < n; k++) { # <img valign="bottom" src="redball.gif"> # } #</td></tr># } ]] </table>

Example

A code section that encloses the whole page in a try/catch/finally block. This is a typical template for database driven pages where the connection always needs to be closed after the page is rendered.
The test table referenced by this code was created as follows:

sql> create table test (a varchar(10));
sql> insert into test (a) values ('hello');
sql> insert into test (a) values ('world');

Note, this example also uses import and method declaration sections.

[import
import java.sql.*; 
import java.util.*;
]
[! 
//method declaration block
Connection getConnection() {
    //implement this method
    }
!]
[[
//about to start page rendering
Connection con = null;
try 
{
con = getConnection(); 
]]
<html>
<body>
<table border=1>
[[
PreparedStatement ps=con.prepareStatement(
    "select * from test");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
    #<tr><td>[=rs.getString(1)]</td></tr>#  
    }
]]
</body>
</html>
[[
} //~end outermost try block
finally {
    try { con.close(); }
    catch (SQLException e) { /* log..*/ }
    }
]]

hello
world