Pure JSP without mixing HTML, by writing html as Java-like code

Posted by ADTC on Stack Overflow See other posts from Stack Overflow or by ADTC
Published on 2012-12-11T04:24:42Z Indexed on 2012/12/11 5:04 UTC
Read the original article Hit count: 371

Filed under:
|
|

Please read before answering. This is a fantasy programming technique I'm dreaming up. I want to know if there's anything close in real life.

The following JSP page:

<%
html {
    head {
        title {"Pure fantasy";}
    }
    body {
        h1 {"A heading with double quote (\") character";}
        p {"a paragraph";}
        String s = "a paragraph in string. the date is ";
        p {
            s;
            new Date().toString();
        }
        table (Border.ZERO, new Padding(27)) {
            tr {
                for (int i = 0; i < 10; i++) {
                    td {i;}
                }
            }
        }
    }
}
%>

could generate the following HTML page:

<html>
    <head>
        <title>Pure fantasy</title>
    </head>
    <body>
        <h1>A heading with double quote (") character</h1>
        <p>a paragraph</p>
        <p>a paragraph in string. the date is 11 December 2012</p>
        <table border="0" padding="27">
            <tr>
                <td>0</td>
                <td>1</td>
                <td>2</td>
                <td>3</td>
                <td>4</td>
                <td>5</td>
                <td>6</td>
                <td>7</td>
                <td>8</td>
                <td>9</td>
            </tr>
        </table>
    </body>
</html>

The thing about this fantasy is it reuses the same old Java programming language technique that enable customized keywords used in a way similar to if-else-then, while, try-catch etc to represent html tags in a non-html way that can easily be checked for syntactic correctness, and most importantly can easily be mixed up with regular Java code without being lost in a sea of <%, %>, <%=, out.write(), etc.

An added feature is that strings can directly be placed as commands to print out into generated HTML, something Java doesn't support (where pure strings have to be assigned to variables before use).

  • Is there anything in real life that comes close?
  • If not, is it possible to define customized keywords in Java or JSP?
  • Or do I have to create an entirely new programming language for that?
  • What problems do you see with this kind of setup?

PS: I know you can use HTML libraries to construct HTML using Java code, but the problem with such libraries is, the source code itself doesn't have a readable HTML representation like the code above does - if you get what I mean.

© Stack Overflow or respective owner

Related posts about java

Related posts about html