Interface Page

All Known Implementing Classes:
PageImpl

public interface Page
A page is a cool and sane replacement for JSP's with much better syntax.

Server side pages implement this interface via the concrete PageImpl class. This class has some additional utility methods as well that page authors may find useful.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
     
    static final String
    The default encoding of the page, specified in the header sent back to the client.
    static final String
    The default mime-type for each page.
    static final String
    Used when compiling a page using javac.
    static final String
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    clientRedirect(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res, String newLocation)
    Redirects the client to the new page location.
    void
    This method is invoked whenever a page is destoryed/unloaded
    getPagePath(jakarta.servlet.http.HttpServletRequest req)
    Returns the path to this page from the web servers document root.
    getRealPath(jakarta.servlet.http.HttpServletRequest req)
    Returns the real absolute directory path for the PagePath.
    Returns a thread specific CharArrayWriter that can be passed to this method as various points in the page.
    void
    init(PageServlet servlet, String contextRelativePagePath)
    This method is invoked whenever a page is created and before it is run.
    void
    render(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res)
     
  • Field Details

    • PACKAGE_NAME

      static final String PACKAGE_NAME
      See Also:
    • DEFAULT_BUFFER_SIZE

      static final int DEFAULT_BUFFER_SIZE
      See Also:
    • DEFAULT_MIME_TYPE

      static final String DEFAULT_MIME_TYPE
      The default mime-type for each page. Usually, there should be no reason to change this. However, if need be, this can be changed via a page compiler directive or turned off entirely. If it's turned off, then the mime-type should be manually specified via the
      invalid reference
      jakarta.servlet.ServletResponse.setContentType
      method.
      See Also:
    • DEFAULT_ENCODING

      static final String DEFAULT_ENCODING
      The default encoding of the page, specified in the header sent back to the client. This can be changed to utf-8, utf-16 or any other coding by a page directive. Alternately, this can be set to an empty string and a different encoding can also be specified in the <head> section of the html document, for example:
      <head><meta http-equiv=content-type content='text/html; charset=UTF-8'></head>
      
      Encodings can be tricky. We are first compiling a page into a java source file, then running the source file and sending it's output to the browser. Read the page encoding and charsets if you are using any non-us-ascii or non-ISO-8859-1 characters in your molly source page.
      See Also:
    • DEFAULT_SRC_ENCODING

      Used when compiling a page using javac.

      If no src_encoding directive is present in the page, we use UTF-8 is the best modern way of doing this (ISO-8859-1 is obsolete and ISO-8859-1 documents can run into issues with javac (which may default to UTF-8 on modern platforms if not specified and umlauts/diacritics/accents are in UTF-8 and ISO-8859-1).

      See Also:
  • Method Details

    • render

      void render(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res) throws Exception
      Throws:
      Exception
    • init

      void init(PageServlet servlet, String contextRelativePagePath) throws jakarta.servlet.ServletException
      This method is invoked whenever a page is created and before it is run.

      Pages should override this method to instantiate/set-up page variables as needed. (pages have no-arg constructors so like most of the servlet API, setup and initialization of variables is done in a init method instead).

      When overriding this class, you must remember to call: super.init

      The page class is reloaded if the page is modified. Variables should therefore be cleaned up in the

      invalid reference
      destory
      method as needed.
      Throws:
      jakarta.servlet.ServletException
    • destroy

      void destroy()
      This method is invoked whenever a page is destoryed/unloaded
    • getPagePath

      String getPagePath(jakarta.servlet.http.HttpServletRequest req)
      Returns the path to this page from the web servers document root.

      So for example, if the page is at foo/bar.mp and is running under the webapp context of context1, then the page path will be: /context1/foo/bar.mp. If there is no specific web app (i.e., the most common case of a default "" webapp), then the page path will be /foo/bar.mp

      This page path is essentially what needs to be typed in the browsers URL window to invoke the page. It's also useful as form action parameters. For example, in a molly page:

      ..
      <form action="[=getPagePath(req)]" method="post">
      ..
      </form>
      
      This will submit the form to the same page where the form is defined. This can be hard coded of course but by using getPagePath, the html does not have to be changed if the name of the page changes on disk.
    • getRealPath

      String getRealPath(jakarta.servlet.http.HttpServletRequest req)
      Returns the real absolute directory path for the PagePath.

      So, for example, for a webserver document root at /web/sites/default/ and a page located in foo/bar.mp, the real path will be: /web/sites/default/foo/bar.mp

    • clientRedirect

      void clientRedirect(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res, String newLocation) throws IOException
      Redirects the client to the new page location. This is a thin (possibly easier to remember) wrapper around the
      invalid reference
      HttpServletResponse.sendRedirect
      method.

      The location parameter can be relative to the specified request's URI or relative to the context root if it contains a leading '/'. The webapp name (if any) does not have to be specified, the redirect will creates a full URL (including the webapp context path) suitable for this purpose.

      For example:

      webapp context current page location parameter resulting page
      default web app ("/") foo/bar.mp baz.mp foo/baz.mp
      default web app ("/") foo/bar.mp /baz.mp baz.mp
      /myapp foo/bar.mp baz.mp /myapp/foo/baz.mp
      /myapp foo/bar.mp /baz.mp /myapp/baz.mp
      Parameters:
      req - the current request
      res - the current response
      location - location to redirect to.
      Throws:
      IOException
    • getThreadLocalWriter

      Returns a thread specific CharArrayWriter that can be passed to this method as various points in the page. The contents of this writer can then be printed on the page when desired.

      Note: The writer is not reset or flushed when it is retrieved. It must be reset manually via calling the CharArrayWriter.reset() method. This design-decision allows request threads to collect debugging data across multiple pages.

      The suggested usage idiom is:

        dbg(true);
        CharArrayWriter cw = getThreadLocalWriter():
        bug(cw, "some message");
        ...
        bug(cw, "other message");
        ...
        
        cw.writeTo(out);
        cw.reset();