to the main page about the tutorial  INTERACTIVE WEB SERVICES WITH JAVA back up next

Translation into Servlets

Translation is extremely simple - doesn't even need to parse the HTML or Java code!

HTML(/XML) -> out.write("...");
<%= expression %> -> out.print(expression);
<% statement %> -> statement
<%! declaration %> -> declaration (in Servlet class)
<%@ directive %> -> instruction to translator, e.g. include file

Example:
<% response.addDateHeader("Expires", 0); %>
<html><head><title>JSP</title></head>
<body><h1>Hello World!</h1>
<%! private int hits = 0; %>
You are visitor number <% synchronized(this) { out.println(++hits); } %>
since the last time the service was restarted.
<p>
This page was last updated: <%= new java.util.Date().toLocaleString() %>
</body></html>

is by Tomcat translated into the following Servlet code (slightly abbreviated):
package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import org.apache.jasper.runtime.*;

public class HelloWorld2$jsp extends HttpJspBase {
    private int hits = 0;

    private static boolean _jspx_inited = false;

    public void _jspService(HttpServletRequest request, HttpServletResponse response)
        throws java.io.IOException, ServletException 
    {
        JspFactory _jspxFactory = null;
        PageContext pageContext = null;
        HttpSession session = null;
        ServletContext application = null;
        ServletConfig config = null;
        JspWriter out = null;
        Object page = this;
        String _value = null;
        try {
            if (_jspx_inited == false)
                synchronized (this) {
                    if (_jspx_inited == false) {
                        _jspx_init();
                        _jspx_inited = true;
                    }
                }
            _jspxFactory = JspFactory.getDefaultFactory();
            response.setContentType("text/html;charset=ISO-8859-1");
            pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
            application = pageContext.getServletContext();
            config = pageContext.getServletConfig();
            session = pageContext.getSession();
            out = pageContext.getOut();
            response.addDateHeader("Expires", 0); 
            out.write("\r\n<html><head><title>JSP</title></head>\r\n<body><h1>Hello World!</h1>\r\n");
            out.write("\r\nYou are visitor number ");
            synchronized(this) { out.println(++hits); } 
            out.write("\r\nsince the last time the server was restarted.\r\n<p>\r\nThis page was last updated: ");
            out.print( new java.util.Date().toLocaleString() );
            out.write("\r\n</body></html>");
        } catch (Throwable t) {
            if (out != null && out.getBufferSize() != 0) out.clearBuffer();
            if (pageContext != null) pageContext.handlePageException(t);
        } finally {
            if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext);
        }
    }
}

Note: since translation is on the lexical level, the following is perfectly acceptable in a JSP page:
<% if (Math.random() < 0.5) { %>
  Have a <b>nice</b> day!
<% } else { %>
  Have a <b>lousy</b> day!
<% } %>

back COPYRIGHT © 2002-2003 ANDERS MØLLER & MICHAEL I. SCHWARTZBACH next