What is JSP

JSP combine markup (whether HTML or XML) with nuggets of Java code to produce a dynamic web page. Each page is automatically compiled to a servlet by the JSP engine, the first time it is requested, and then executed. JSP provides a variety of ways to talk to Java classes, servlets, applets and the web server. With it, you can split the functionality of your web applications into components with well-defined public interfaces glued together by a simple page.
This model allows tasks to be sub-divided – a developer builds custom components and the page designer assembles the application with a few judicious method calls. In this ‘application assembly’ model, the business logic is separated from the presentation of data. Finally, it’s nice and simple In this chapter, we will : Discuss the JavaServer Pages (JSP) architecture Look at the elements of a JSP file, and the tags used to represent them Encapsulate logic in a JavaBean component and integrate it with JSP Walk through a detailed example using JSP, showing a typical web application architecture.

Architectural Overview

A JavaServer Page is a simple text file consisting of HTML or XML content along with JSP elements (a sort of shorthand for Java code). When a client requests a JSP page of the web server and it has not been run before, the page is first passed to a JSP engine which compiles the page to a servlet, runs it and returns the resulting content to the client. Thereafter, the web server’s servlet engine will run the compiled page. It should be no surprise, given the flexibility of the servlet model, that the current reference implementation of the JSP engine is itself a servlet. It is possible to view the finished servlet code that is generated by locating it within the directory structure of the servlet engine. For example, with JRun, you can find the source code for your JSP files (in servlet form) in the jrun/jsm-default/services/jse/servlets/jsp directory. This is very helpful when trying to debug your JSP files.

If you take a look in the source files for the javax.servlet.jsp package, you’ll find the following classes :

  • JSPPage
  • HttpJspPage

They define the interface for the compiled JSP page – namely that it must have three methods. Not surprisingly they are :

  • jspInit()
  • jspDestroy()
  • _jspService(HttpServletRequest request, HttpServletResponse response)

The first two methods can be defined by the JSP author (we’ll see how in a moment), but the third is the compiled version of the JSP page, and its creation is the responsibility of the JSP engine.

<html>
<head>
<title>Demo of a JSP page</title>
</head>
<body>
<!-- Set global information for the page -->
<%@ page language="java" %>
<!-- Declare the character variable -->
<%! char c = 0; %>
<!-- Scriptlet - Java code --><%
	for (int i = 0; i < 26; i++)
{
	for (int j = 0; j < 26; j++)
{
// Output capital letters of the alphabet, and change starting letter
c = (char)(0x41 +
(26 - i + j)%26);
%>
<!-- Output the value of c.toString() to the HTML page -->
<%= c %><%
}%><br><%}%>
</body>
</html>

This page just outputs the alphabet 26 times and changes the starting letter. The HTML is self-explanatory and written the way it should be, rather than cluttering up methods of a servlet.

It’s time we saw a JSP file :

<html> <head> <title>Demo of a JSP page</title> </head> <body>
<!-- Set global information for the page -->
<%@ page language="java" %>
<!-- Declare the character variable -->
<%! char c = 0; %>
<!-- Scriptlet - Java code -->
<% for (int i = 0; i < 26; i++) {
for (int j = 0; j < 26; j++) { // Output capital letters of the alphabet, and change starting letter 
c = (char)(0x41 + (26 - i + j)%26); 
%> <!-- Output the value of c.toString() to the HTML page -->
<%= c %> <%
}
%>
<br>
<% } %> 
</body> 
</html>

This page just outputs the alphabet 26 times and changes the starting letter. The HTML is self-explanatory and written the way it should be, rather than cluttering up methods of a servlet.

The Java code in the page includes :

  • Directives  of these provide global information to the page, for example, import statements, the page for error handling or whether the page is part of a session. In the above example we set the script language to Java.
  • Declaratives – these are for page-wide variable and method declarations.
  • Scriptlets – the Java code embedded in the page.
  • Expressions – formats the expression as a string for inclusion in the output of the page.

We will meet the last JSP element type, actions, soon. These elements follow an XML-like syntax, and perform a function behind the scenes. They provide the means to totally separate presentation from logic. A good example is <jsp:useBean …/> which finds or creates an instance of a bean with the given scope and name. With the tag extension mechanism to be introduced in JSP 1.1, you’ll be able to define similar action tags and put their functionality in a tag library.