# Page Compilation in Jigsaw.

Jigsaw Home / Documentation Overview / Page Compilation Overview

- Writing your first page
- Handling the POST method
- Debugging

# Writing your first page

Please notice that what is discussed below is not part of any W3C specification! It is server-side only.

First, choose a place to put your page, for example `<instdir>/Jigsaw/Jigsaw/WWW/MyPage.jhtml`.

Then put the following in MyPage.jhtml.

    <html>
      <head><title>My Page</title></head>
      <body>
        <h1>My Page</h1>

        <java>
          int age = 42;
        </java>

        <h2>A Print</h2>
        <java>
          out.println ("age : "+age);
        </java>

        <h2>Another Print</h2>
        age : <java type=print>age</java>

        <h2>A Test</h2>
        <java>
          if (age > 100) {
          out.println("more than 100");
          } else {
          out.println("less than 100");
          }
        </java>

        <h2>Another Test</h2>
        <java> if (age > 100) {</java>
        more than 100
        <java> } else { </java>
        less than 100
        <java> } </java>

        <h2>A Loop</h2>
        <java>
          for (int i = 0; i < 80; i++) {
          out.println("*");
          }
        </java>

        <h2>Another Loop</h2>
        <java>for (int i = 0; i < 80; i++) {</java>
        *
        <java>}</java>

      </body>
    </html>

The first time you will access this page, the PageCompileFrame will generate a Frame. By default the frame generated extends GeneratedFrame, but it can be a subclass of GeneratedFrame, see the documentation for `<java type=extends>`. The source code of this frame will looks like:

    public class MyPage extends org.w3c.jigsaw.pagecompile.GeneratedFrame  {

        protected void get(org.w3c.jigsaw.http.Request request,
                           org.w3c.jigsaw.http.Reply reply,
                           org.w3c.jigsaw.pagecompile.PageCompileOutputStream out)
            throws java.io.IOException
        {
            org.w3c.jigsaw.pagecompile.PageCompileFile _file =
                new org.w3c.jigsaw.pagecompile.PageCompileFile("/0/w3c/bmahe/Jigsaw/Jigsaw2/WWW/MyPage.jhtml");
            _file.writeBytes(0,79,out);

            int age = 42;

            _file.writeBytes(118,144,out);
            out.println ("age : "+age);

            _file.writeBytes(198,240,out);
            out.println(String.valueOf(age));

            _file.writeBytes(268,297,out);

            if (age > 100) {
                out.println("more than 100");
            } else {
                out.println("less than 100");
            }

            _file.writeBytes(434,469,out);

            if (age > 100) {
                _file.writeBytes(500,522,out);
            } else {
                _file.writeBytes(546,568,out);
            }

            _file.writeBytes(585,614,out);

            for (int i = 0; i < 80; i++) {
                out.println("*");
            }

            _file.writeBytes(702,737,out);

            for (int i = 0; i < 80; i++) {
                _file.writeBytes(781,791,out);
            }

            _file.writeBytes(806,830,out);
        }
    }

And the page displayed on your browser will looks like:

# My Page

# A Print

age : 42

# Another Print

age : 42

# A Test

less than 100

# Another Test

less than 100

# A Loop

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

# Another Loop

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

# Handling the POST method

The GeneratedFrame inherits from PostableFrame, so it's really easy to handle the `POST` method with Page Compilation. The following example get the user name from the client and display it. Actually, you just need to override the post method of GeneratedFrame. If you want `GET` requests with query string to be converted in `POST` requests, override the method getConvertGetFlag like in the example.

    <html>
      <java type=import>
        import java.io.IOException;

        import org.w3c.jigsaw.http.*;
        import org.w3c.jigsaw.pagecompile.*;
        import org.w3c.jigsaw.forms.URLDecoder;
      </java>
      <java type=class>
        protected String NAME_P = "name";

        //Get the 'convert GET to POST' flag.
        public boolean getConvertGetFlag() {
            return true;
        }

        //handle the post method:
        public void post (Request request,
                          Reply reply,
                          URLDecoder data,
                          PageCompileOutputStream out)
            throws IOException
        {
            String name = data.getValue(NAME_P);
            out.println("<h1>Your Name:</h1>");
            out.println(name);
        }
      </java>
      <head>
        <title>Post test</title>
      </head>
      <body>
        <h1>Post test</h1>
        Enter your name:
        <form method="POST">
          <input type="text" name="<java type=print>NAME_P</java>">
          <input type="submit">
        </form>
      </body>
    </html>

So, the generated java file will looks like:

    package pagecompile;

    import java.io.IOException;

    import org.w3c.jigsaw.pagecompile.*;
    import org.w3c.jigsaw.http.*;
    import org.w3c.jigsaw.forms.URLDecoder;

    public class post extends org.w3c.jigsaw.pagecompile.GeneratedFrame  {

        protected String NAME_P = "name";

        //Get the 'convert GET to POST' flag.
        public boolean getConvertGetFlag() {
            return true;
        }

        //handle the post method:
        public void post (Request request,
                          Reply reply,
                          URLDecoder data,
                          PageCompileOutputStream out)
            throws IOException
        {
            String name = data.getValue(NAME_P);
            out.println("<h1>Your Name:</h1>");
            out.println(name);
        }

        protected void get(org.w3c.jigsaw.http.Request request,
                           org.w3c.jigsaw.http.Reply reply,
                           org.w3c.jigsaw.pagecompile.PageCompileOutputStream out)
            throws java.io.IOException
        {
            org.w3c.jigsaw.pagecompile.PageCompileFile _file =
                new org.w3c.jigsaw.pagecompile.PageCompileFile("/0/w3c/bmahe/Jigsaw/Jigsaw2/WWW/pagecompile/post.jhtml");
            _file.writeBytes(0,6,out);
            _file.writeBytes(73,73,out);
            _file.writeBytes(507,634,out);
            out.print(String.valueOf(NAME_P));
            _file.writeBytes(665,715,out);
        }
    }

And the page displayed on your browser will looks like:

After a click on "Submit Query":

NOTE: If you don't override the `post` method, the generated frame will return a "`Method POST not allowed`" reply on every `POST` request.

# Debugging

The generated java file could be incorrect, for example the following examples emit a compilation error (missing '}').

    <html>
      <head><title>Compilation Error</title></head>
      <body>
        <h1>Compilation error</h1>

        <h2>A Loop</h2>
        <java>
          for (int i = 0; i < 80; i++) {
          out.println("*");
        </java>

      </body>
    </html>

Then Jigsaw will send the following reply to your browser when you will try to access your page.

    /0/w3c/bmahe/Jigsaw/Jigsaw2/compiledPages/CompileError.java:16: '}' expected.
    }
     ^
    1 error

Your generated page could also produce some runtime errors, for example the following page will produce a ArrayIndexOutOfBoundsException:

    <html>
      <head><title>Runtime Error</title></head>
      <body>
        <h1>Runtime error</h1>

        <java>
          int tab[] = {1,2,3,4,5};
        </java>

        <h2>A Loop</h2>
        <java>
          for (int i = 0; i < 6; i++) {
            out.print(tab[i]);
          }
        </java>

      </body>
    </html>

And the reply sent will be:

    The generated frame at

    http://ender.w3.org/RuntimeError.jhtml

    reported this exception :

    5

    Stack trace :

    java.lang.ArrayIndexOutOfBoundsException: 5
            at RuntimeError.get(RuntimeError.java:16)
            at org.w3c.jigsaw.pagecompile.GeneratedFrame.get(GeneratedFrame.java:34)
            at org.w3c.jigsaw.frames.HTTPFrame.perform(HTTPFrame.java:1281)
            at org.w3c.tools.resources.FramedResource.performFrames(FramedResource.java:581)
            at org.w3c.jigsaw.frames.HTTPFrame.performFrames(HTTPFrame.java:1256)
            at org.w3c.tools.resources.FramedResource.perform(FramedResource.java:599)
            at org.w3c.tools.resources.ResourceFrame.perform(ResourceFrame.java:108)
            at org.w3c.jigsaw.frames.HTTPFrame.perform(HTTPFrame.java:1269)
            at org.w3c.jigsaw.pagecompile.PageCompileFrame.perform(PageCompileFrame.java:662)
            at org.w3c.jigsaw.http.httpd.perform(httpd.java:1538)
            at org.w3c.jigsaw.http.Client.processRequest(Client.java:384)
            at org.w3c.jigsaw.http.Client.startConnection(Client.java:459)
            at org.w3c.jigsaw.http.socket.SocketClient.run(SocketClient.java:114)
            at org.w3c.util.CachedThread.run(ThreadCache.java:86)

Reading the generated java file will help you to find where the error is in the original HTML/Java file. This file is located in the Compiled Pages Directory (on any subdirectory). The generated java file could be in a package, for example if the URL of your HTML/Java file is `/pagecompile/test/Page.jhtml` the package is `pagecompile.test` and the class name is `Page`. So the generated java file is `<Compiled Pages Directory>/pagecompile/test/Page.java` .