Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · ASP.NET Newsletter Application · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Newsletter
Enter your email address to receive full length articles at Stardeveloper:


Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (11)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)

Main Category  Other
  Website Maintenance (3)
Log In
UserName Or Email:

Password:

Auto-Login:

Hosted by Securewebs.com
 
Home : J2EE : JSP : Introducing JavaServer Pages
 
Read full length articles at Stardeveloper using Twitter Follow on Twitter Facebook Facebook fan page Email Get Articles via Email RSS Get Articles via RSS Feed

Introducing JavaServer Pages

by Wrox Press.

Based on servlet technology, and currently shaping up at breakneck speed, JavaServer Pages (JSP) is set to be one of the most important elements of Java server programming. It's by no means complete yet, but that will change as the Java 2 Enterprise Edition comes together.

So what are JavaServer Pages? Well, they 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.

To give you an idea of the future, this separation of logic and presentation may become yet more extreme with the use of custom tags slated for JSP 1.1.

JavaServer Pages is a specification that is already implemented by several web servers (for more details, see the JSP FAQ at http://www.esperanto.org.nz/jsp/jspfaq.html), on which your code will run without change, making it more portable and the server market more competitive than its rivals. 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.

JSP Methods
JSP Methods

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.

Elements of a JavaServer Page
The Java code in the page includes :

  • Directives – 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.

Now let's examine the basic elements of a JSP in a more complete fashion, before we code some more.

Something I've found very useful to have around while coding is the syntax crib sheet available at http://java.sun.com/products/jsp/syntax.html in PDF format. This has a concise summary of what we'll see here.

A JSP directive is a statement that gives the JSP engine information for the page that follows. The general syntax of a JSP directive is <%@ directive { attribute=”value” } %>, where the directive may have a number of (optional) attributes. Each directive has an optional XML equivalent, but these are intended for future JSP tools, so we won't consider them here.

Possible directives in JSP 1.0 are :

  • Page – information for that page
  • Include – files to be included verbatim
  • Taglib – the URI for a library of tags that you'll use in the page (unimplemented at the time of writing)

As is to be expected, the page directive has many possible attributes. Specifying these is optional, as the mandatory ones have default values.

JSP Attributes
JSP Attributes
JSP Attributes
JSP Attributes

A JSP declaration can be thought of as the definition of class-level variables and methods that are to be used throughout the page. To define a declarative block, begin the block of code with <%! declaration>.

<%!
	String var1 = "x";
	int count = 0;

	private void incrementCount()
{
	count++;
	}
%>

Note that you put semi-colons after each variable declaration, just as if you were writing it in a class.

This is how you would define the optional jspInit() and jspDestroy() methods that we mentioned earlier.


 ( 6 Remaining ) Next

Buy This Book From Amazon
Title: Professional Java Server Programming: with Servlets, JavaServer Pages (JSP), XML, Enterprise JavaBeans (EJB), JNDI, CORBA, Jini and Javaspaces
Publisher: Wrox Press Inc
Price: $59.99
Pages: 1121
DatePublished: August 1999



Comments/Questions ( Threads: 4, Comments: 6 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. How to wright XML file using jbean or jsp or java or jdom
  2. Java Beans ( 1 Reply ) This thread contains 1 reply by Faisal Khan.
  3. Where is the property ( 1 Reply )
  4. JSP date&time

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2010 Stardeveloper.com, All Rights Reserved.