Signup · Login
Stardeveloper.com  
Home · Articles · Forums · Advertise · Contact
Search this Website
Stay Informed
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:
Article Categories
.NET  .NET
  ASP (15)
  ASP.NET (29)
  ADO (16)
  ADO.NET (10)
  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)
Latest Forum Activity
Re: Unable to insert data in an Access databa..
by Faisal Khan on 12 Nov 2008 Go To Post

Re: HOW TO MOVE FROM APACHE TO MS IIS
by Faisal Khan on 12 Nov 2008 Go To Post

Re: Preventing/Investigating the Source of SQ..
by smithsan on 19 Oct 2008 Go To Post

Re: Preventing/Investigating the Source of SQ..
by peeru999 on 8 Oct 2008 Go To Post

Re: Preventing/Investigating the Source of SQ..
by Faisal Khan on 8 Oct 2008 Go To Post

Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : J2EE : JSP : Examining Java Server Pages in detail
 

Examining Java Server Pages in detail
by Faisal Khan.

Overview
In this article we will study the anatomy of a JSP page and how you can use each of these elements for your own use.

Anatomy of a JSP page
If you rename a .html file to a .jsp file then that page is a valid JSP page and will compile and run successfully. But this .jsp page will be using default features provided to a JSP page and will not be doing anything useful. One other thing to keep in mind is that all JSP pages are compiled to Java Servlet classes, so inherently JSP pages are actually Java Servlets.

A JSP page consists of following parts :

  • Directives
  • Scripting Elements
  • JSP Tags

We will now take each one of them in detail.

Directives
Directives as their name implies are compile time control tags. They allow you to customize how your JSP pages are compiled to Java Servlets. There are currently of three types :

  • Page Directive
  • Taglib Directive
  • Include Directive

i. The Page Directive
This directive is placed at the top of a JSP page and allows you to customize different features of your JSP page. It's syntax is as follows :

<%@ page attribute-name="value" %>

All JSP directives, scripting elements and expressions lie in between the <% and %> tags. Following are the different attributes available to you along with the values you can use in your JSP page.

Attribute Value Description
language scriptingLanguage The language you are using on the JSP page. Default is Java.
extends className The Java class which your generated Servlet will be extending.
import importList A comma separated list of classes, interfaces and packages you want to use in your JSP page.
session true | false If you want to use session then set it to true, otherwise false. Default is true.
buffer none | sizeInKB Set it to none if you don't want to use buffering or set it to a value greater than 8KB. Default is 8KB.
autoFlush true | false If set to true, once buffer is full it will be sent to the client. If set to false then an exception will be generated on buffer overflow. Default is true.
isThreadSafe true | false If set to true then the JSP engine will send all the requests as they come to the same instance of the Servlet. If false then request will be sent to the Servlet one at a time. Default is true.
info servletInfo Returns info about the JSP page.
errorPage pageURL The URL to the JSP page where all un-handled exceptions will be sent.
isErrorPage true | false If this JSP page is the error page where other JSP page's un-handled exceptions will be sent then set it to true, otherwise false.
contentType content/type The content-type for the current JSP page. Default is text/html.
pageEncoding encodingInfo Character encoding for the JSP page. Default is ISO-8859-1.

ii. The Taglib Directive
It is used to incorporate custom tags in a JSP page. It requires two attributes and it's syntax is as follows :

<%@ taglib uri="tag-lib-uri" prefix="tag-prefix" %>

The uri attribute contains the location of the tag library TLD ( Tag Library Descriptor ) file, while prefix is the tag prefix you want to use for your custom tag e.g,

<%@ taglib uri="/WEB-INF/tlds/mytag.tld" prefix="star" %>

<star:time />

Above code describes a tag with prefix 'star' which displays current time. We will learn more about these custom JSP tags in some later tutorial.

iii. The Include Directive
The include directive allows insertion of the content of another file into the JSP page at compilation time. It's syntax is as follows :

<%@ include file="localOrAbsoluteURL" %>

During compilation the content of the file specified in the file attribute will be added to the current JSP page e.g,


	... Some HTML and JSP code above

<%@ include file="/misc/copyright.txt" %>

</body>
</html>

Above code adds the copyright info to the current JSP page.

Scripting Elements
The scripting elements in a JSP page comprise of four types :

  • Comments
  • Class level declarations
  • Expression
  • Scriptlets

Lets look at all of them one by one.

i. Comments
Like other Java classes you can add comments in a JSP page. Comments are useful as it makes your code more understandable. The syntax of JSP comment is as follows :

<%-- This is a JSP Comment --%>

ii. Class Level Declarations
This feature allows you to add your own class level methods and variables which can be accessed by the scriptlets. It's syntax is as follows :

<%! Declaration Statement %>

For example :

<%! String author = "Faisal Khan"; %>

Above code declares a class level variable named author with a value of "Faisal Khan".

iii. Expression
It is a short hand for writing output directly to the output stream. It's syntax is as follows :

<%= statement %>

The value of the Java statement is displayed to the user. e.g,

<%= new java.util.Date().toString() %>

Above code displays the current date and time to the user.

iv. Scriptlets
Within scriptlets you can add whatever Java code you like. All this code will go inside the _jspService() method, so you cannot declare class level methods or variables within scriptlets ( see above ). It's syntax is as follows :

<% statements %>

For example :

<%
	String name = "Faisal Khan";

	out.println("My name is " + name);
%>

Above code first declares a name variable with value of "Faisal Khan" and then displays it to the user screen.

If you have been following some of my tutorials on accessing database with Java Servlets then you can just put all that JDBC code inside JSP Scriptlets and it will work. We will look into accessing database with JSP in detail in some other article.

JSP Tags
As we saw earlier we can create and use our own custom tags inside a JSP page using the taglib directive. With that there are quite a few standard JSP tags which are part and parcel of the JSP specification and provide really useful functionality :


 ( 1 Remaining ) Next

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

  1. Java Mail Using JSP and beans
  2. i need the models of javabeans
  3. It's great !!! ( 2 Replies ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.

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 - 2008 Stardeveloper.com, All Rights Reserved.