A Bit About JSPs

 

Introduction.  JSPs seem to operate pretty much like their other middleware counterparts PHPs and ASPs.  The code for JSPs is really Java like, but it is usually embedded within HTML in order to operate.  There seem to be two main overall requirements in order to create a JSP page.
  • a text file saved with the .jsp extension
  • delimiting the JSP code between  tags  <%   and   %>

They are probably more like ASPs than PHPs, but the similarities between all three are clear upon investigation.

The web server, Tomcat is used to convert JSPs into Servlets which are then run on the web server.  Tomcat can actually function as a complete web server but it is most often used in conjunction with Apache.  The name of the processor that converts JSP into Servlets is Jasper.

Hello World.  In order to work a simple example and test your abilities to FTP we will now develop hello_world.jsp.

 

<html>
<head>
<title>Hello World JSP Page</title>
</head>

<body>
<%

out.println("<H1>Hello World</H1>");

%>

</body>
</html>

 

When you
  • save this code into a file
  • upload it to a server running Tomcat
  • access the page via a web browser

You should see something like the following.

 

 

Cubes.  Now we will work another example that makes use of your background in Java.  You should call this cubes.jsp.

 

<html>
<head>
<title>Calculating Cubes</title>
</head>

<body bgcolor = "000044" text="cccccc">

<%
out.println("<h2><B>Cubes</B></h2><BR>");
int iLoop;
int iCube;
for (iLoop = 1; iLoop <= 10; iLoop++)
{

iCube = iLoop*iLoop*iLoop;
out.println("<font size = 4>The cube of " + iLoop + " is <b>" + iCube + "</b></font><br>");

}
%>
</body>
</html>

 

In this instance I have mixed HTML and Java variables within the out class println( ) method.  We also make use of a for loop.

Your output should look something like the following.