The JSP Files (part 1): Purple Pigs In A Fruitbasket
By Vikram Vaswani and Harish Kamath
This article copyright Melonfire 2000−2002. All rights reserved.
Table of Contents
The JSP Story......................................................................................................................................................1
Studying The Foundations.................................................................................................................................2
Java In A Teacup................................................................................................................................................3
Enter John Doe....................................................................................................................................................6
Putting Two And Two Together........................................................................................................................8
Basket Case........................................................................................................................................................10
Alphabet Soup For The Soul............................................................................................................................13
The JSP Files (part 1): Purple Pigs In A Fruitbasket
i
The JSP Story
Ever since Sun Microsystems (aka "the dot in dot−com") came up with Java, the geeks have been screaming
themselves hoarse about the wonders of this technology. Terms like "platform−independent code" and "write
once, run anywhere" have been given so much airplay that even novice Java developers are aware of them,
and the language is also popular with talk−show pundits and Internet consultants, who tout it as the solution to
almost all problems of cross−platform compatibility.
Now, we're big fans of Java − we've used it in the past, and will do so again in the future − but this series of
tutorials isn't about Java. It's actually about an offshoot of Java, the innocuously−named Java Server Pages, or
JSP, which attempts to offer Web developers a compelling alternative to traditional server−side scripting
languages like Perl, PHP and ASP.
How? First, a little history...
During the early days of the Web, it was the sheer amount of (free!) content that encouraged people to use it.
Using the easy−to−learn HTML language, any one and their deaf grandma could set up a Web page and reach
out to other like−minded souls online. As the infrastructure improved, content was no longer restricted to text;
you could now view pictures or watch videos on the Web. And as more and more people began adding
interactivity to their Web sites, a bunch of programming languages were born in order to meet increasingly
complex requirements.
The best−known of these is, of course, Perl, although PHP and ASP are also popular favourites. The problem
In the context of JSP, a multi−tier architecture involves the Web server for static HTML content, the
application server for JavaBeans and servlets, and the database server for database connectivity. Additionally,
you can combine JSP with JavaBeans and Java servlets to create complex Web applications which build upon
previously−released and tested code modules, thereby simplifying code maintenance and increasing
reusability.
It is important to note here that JSP code is not read line−by−line, as with PHP; it is first converted into a
servlet (a bytecode version of the program) and then invoked by a servlet engine (such as Tomcat) to perform
the required actions. Once the servlet is executed, the results are sent back to the client. Since the servlet
engine has to compile the servlet the first time around, displaying a JSP page can take a little while the first
time you access it; however, the next time around, response time will be dramatically reduced, since the
servlet will have already been compiled and therefore ready for immediate use.
Studying The Foundations 2
Java In A Teacup
In order to begin working on JSP, you need to get yourself copies of Sun's Java Development Kit, Apache's
httpd Web server and mod_jserv module, and the Tomcat servlet engine, and configure them so that they're all
working together. This tutorial assumes that you've got a JSP development environment set up − in case you
don't, take a look at "Slapping Together A JSP Development Environment" at , a tutorial which will guide you
through the process.
With that out of the way, let's get down to the nitty−gritty of actually creating a JSP page. Open up a new file
in your favourite text editor and type in the following lines of code:
<html>
<head>
</head>
<body>
<%
// asking for it!
out.println("Waiter, can I have a cup of Java, please?");
%>
</body>
</html>
^
: Invalid declaration.
out.write("\r\n\r\n\r\n");
^
2 errors
at org.apache.jasper.compiler.Compiler.compile(Compiler.java,
Compiled
Code)
at
org.apache.jasper.servlet.JspServlet.doLoadJSP(JspServlet.java:462)
at
org.apache.jasper.servlet.JasperLoader12.loadJSP(JasperLoader12.java:146)
at
org.apache.jasper.servlet.JspServlet.loadJSP(JspServlet.java:433)
at
org.apache.jasper.servlet.JspServlet$JspServletWrapper.loadIfNecessary(JspSe
rvlet.java:152)
at
org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.ja
va:164)
at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:318)
at
org.apache.jasper.servlet.JspServlet.service(JspServlet.java,
Compiled
Code)
at
javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
The JSP Files (part 1): Purple Pigs In A Fruitbasket
Java In A Teacup 4