«^»
5.3. Another digression: JavaServer pages

The code of a servlet is a mix of Java source code doing some work and Java source code that generates HTML instructions. Because a lot of the code often consists of Java statements generating HTML, the code is not easy to read.

A JavaServer page is a WWW document that is written in a mix of elements of a markup language (e.g., HTML) and elements that will be dynamically generated by executing some Java code. As with servlets, the webserver program will either itself transform, or instead arrange for another program to transform, the JavaServer page into Java source code (a servlet). Behind the scenes, the servlet is then compiled and executed, and finally its output (HTML) is passed to the webserver program.

Here is a WWW page that has a button that will cause a JavaServer page to be executed:

0107: <HTML>
0108:    <BODY>
0109:       <FORM METHOD="POST" ACTION=
0110:          "http://localhost:8080/mytomcat/jtoFahrenheit/toFahrenheit.jsp">
0111:          Type in a Centigrade value
0112:          <INPUT TYPE="text" NAME="centigrade">
0113:          <BR>
0114:          <INPUT TYPE="submit" VALUE="Get Fahrenheit">
0115:       </FORM>
0116:    </BODY>
0117: </HTML>

And here is an appropriate JavaServer page (which is stored in the file toFahrenheit.jsp):

0118: <%@page language="java" %>
0119: <%
0120:    String tCentigradeString = request.getParameter("centigrade");
0121:    double tCentigrade = Double.parseDouble(tCentigradeString);
0122:    double tFahrenheit = 32 + tCentigrade*9/5;
0123: %>
0124: <html>
0125: <head>
0126: <title>Reply</title>
0127: </head>
0128: <body>
0129: <p>
0130: In Fahrenheit, this is <%= tFahrenheit %>
0131: </p>
0132: </body>
0133: </html>

The Netcraft survey points out that ‘over the last year JSP has been the fastest growing scripting technology after ASP.NET. JSP sites are often bigger, more complex, and better funded and run by larger organisations than sites using the more common scripting technologies’ [33]. Some examples of JSP sites are www.theaa.com, www.bt.com, www.explore.co.uk and www.opodo.co.uk.