Enterprise Beans

Contact Us

Introduction to JavaServer Pages - JSP


JavaServer Pages JSP is a Java based technology that is used to develop dynamic web sites. With JSP, web designers and developers can quickly incorporate dynamic elements into web pages using embedded Java and markup tags. These tags provide the HTML designer with a way to access data and business logic stored inside Java objects.

 Life Cycle of JSP

The following figure shows the process of the flow of events that occur after a client requests for a JSP page.
Once a JSP is translated to a servlet, the container invokes the following life cycle methods on the servlet that are defined in the javax.servlet.jsp.JspPage interface:
  • jspInit(): This method is invoked at the time when the servlet is initialized.
  • jspService(): This method is invoked when request for the JSP page is received.
  • jspDestroy():This method is invoked before the servlet is removed from the service.
A directive element in a JSP page provides global information about a particular JSP page and can be of three types:
  • page directive
  • taglib directive
  • include directive
The syntax for defining a directive is:
<%@ directive attribute="value" %>
 The page Directive

The syntax of the page directive is: <%@ page attribute_list %>

The attribute_list for page Directive are
Attribute Name Description
language (language = "java") Defines the scripting language of the JSP page.
extends (extends = "package.class") Defines the parent class that the JSP generated servlet extends.
import (import = "package.class") Imports the list of packages, classes, or interfaces into the generated servlet.
session (session = "true | false") Specifies if the generated servlet can access the session or not. An implicit object, session, is generated if the value is set to true. The default value of session attribute is true.
buffer (buffer = "sizekb | none") Specifies the size of the out buffer. If size is set to none, no buffering is performed. The default value of buffer size is 8 KB.
autoflush (autoflush = "true | false") Specifies that the out buffer be flushed automatically if the value is set to true. If the value is set to false, an exception is raised when the buffer is full. The default value of autoFlush attribute is true.
isThreadSafe (isThreadSafe = "true | false") Specifies whether a JSP page is threadsafe or not.
errorPage (errorPage = "url") Specifies that any un-handled exception generated will be directed to the URL.
isErrorPage (isErrorPage = "true | false") Specifies that the current JSP page is an error page, if the attribute value is set to true. The default value of isErrorPage attribute is false.
contentType (contentType = "MIME-Type") Defines the Multipurpose Internal Mail Extension (MIME) type for a response. The default value of the contentType attribute is text/html.

<%@ page language="java" %>
 The include Directive

The syntax to define the include directive is:
<%@ include file = "URLname" %>

<%@ include file = "anyfile.html" %>
 The taglib Directive

The syntax to import a taglib directive in the JSP page is:
<%@ taglib uri="tag_lib_URI" prefix="prefix" %>
The following table lists the two attributes of the taglib directive along with their description:
Attribute Description
Uri Locates the TLD file of a custom tag.
Prefix Defines a prefix string to be used for distinguishing a custom tag instance.

<%@ taglib uri="" prefix="bean" %>
 JSP Scripting Elements

Various types of JSP scripting elements that you can use in a JSP page are:
  • Declarations: Declarative statements are placed within <%! and %> symbols and always end with a semicolon.
    The following code snippet uses JSP declarations to define variables:
    int i=0;
    int j=0;
    int z=0;
    int prod=0;
  • Expressions: The syntax to include a JSP expressions in the JSP file is: <%= expression%>
    <%= new java.util.Date() %>
    <%= request.getRemoteHoat() %>
    <%= ++accessCount %>
  • Scriptlets: The syntax to declare JSP scriptlets to include valid Java code is: <% Java code %>
    The following code snippet uses JSP Script:
    int i=Integer.parseInt(request.getParameter("t1"));
    int j=Integer.parseInt(request.getParameter("t2"));
    int k=0;
    if (i==j)
    int prod=0;

 Click Next To Continue ...