IPOWERWEB.com

 Affordable, reliable
web hosting solutions

Call IPOWERWEB Today at 1-888-511-HOST Chat with an IPOWERWEB representative LIVE!
24/7x365 service - Live Technical Support

Domain Name Registration
web hosting services
cheap web hosting
IPOWERWEB help section
contact IPOWERWEB
testimonials for best hosting
affordable web hosting
IPOWERWEB web hosting
IPOWERWEB accolades
best domain prices


Web Hosting Money Back Guarantee
home build profit promote manage


Introducing Java Server Pages

Home > Build > Backend > Java Server Pages

Chapter 7- Introducing Java Server Pages
Page 2 of 4

(1) (2) (3) (4)

A Simple JavaServer Page


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.

©1999 Wrox Press Limited, US and UK.

 
home | products | about us | help center | testimonials | press room | contact us
affiliates | careers | domain names | web hosting | site map

Copyright © 1999-2007 IPOWER, Inc. Read our Terms and Conditions. All rights reserved.