jsp notes by Saeed.shah


									                       JSP(Java Server Pages)

Introduction :-
Java server pages technology allows you to easily create that has both static and dynamic

A JSP page is a text document that contains two types of text :-

      Static data , which can be expressed in any text – based format (such as HTML , WML ,
       and XML)

      Provides expression language for accessing server – side objects

      JSP specifications extend the Java Servlet API.

Life Cycle Of a JSP page :-

      A JSP page services requests as a servlet. Thus ,the life cycle of JSP pages is determined
       by Java Servlet technology.

      When a request is mapped to a JSP page , the web container first checks whether the
       JSP page’s servlet is older than the JSP page . If the servlet is older , the web container
       translates the JSP page into a servlet class and compiles the class.

JSP elements are treated as follows :

      Directives are used to control how the web container translates and executes the JSP

      Scripting elements are inserted into the JSP page’s servlet class

      Expression language expressions are passed as parameters to calls to the JSP expression
         Jsp:[set][get] property elements are converted into method calls to java beans

         Jsp:[include][forward] elements are converted into invocations of the java servlet API

         Custom tags are converted into calls to the tag handler that implements the custom

After the page has been translated and compiled , the JSP page’s servlet follows the servlet
life cycle

         If an instance of the JSP page’s servlet does not exit , the container loads the JSP page’s
          servlet class ; instantiates an instance    of the servlet class then initializes the servlet
          instance by calling the jspInit method

         The container invokes the _jspService method , passing request and response objects.

         If the container needs to remove the jsp pages’s servlet , it calls the jspDestroy method

Implicit Objects
The following implicit object can be used from JSPs

session           The session object for the client

request           The request triggering the execution of the JSP page

response          The response returned by the JSP page

out               Writes the given data to output stream of client

config            Allows access to servletConfig for JSP page
There are four main tags:

     1. Declaration tag
     2. Expression tag
     3. Directive Tag
     4. Scriptlet tag
     5. Action tag

Scriptlet tag ( <% … %> )

Between <% and %> tags, any valid Java code is called a Scriptlet. This code can
access any variable or bean declared.
For example, to print a variable.

String username = “Giri” ;
out.println ( username ) ;

Declaration tag ( <%! %> )

This tag allows the developer to declare variables or methods.
Before the declaration you must have <%!
At the end of the declaration, the developer must have %>
Code placed in this tag must end in a semicolon ( ; ).
Declarations do not generate output so are used with JSP expressions or scriptlets.

For Example,


private int counter = 0 ;
private String get Account ( int accountNo)

Expressions tag(<%= %>)
An expression is a shorthand notation for a scriptlet that outputs a value in the response
stream back to the client .
This tag allows the developer to embed any Java expression and is short for
A semicolon ( ; ) does not appear at the end of the code inside the tag.

For example :
Date : <%= new java.util.Date() %>
<input type=text value=<%=request.getParameter(“uid”)%> name=uid>

Directive tag ( <%@ directive … %>)

A JSP directive gives special information about the page to the JSP Engine.
There are three main types of directives:
1) page – processing information for this page.
2) Include – files to be included.
3) Tag library – tag library to be used in this page.

Directives do not produce any visible output when the page is requested but
change the way the JSP Engine processes the page.
For example, you can make session data unavailable to a page by setting a page
directive (session) to false.

1. Page directive
This directive has 11 optional attributes that provide the JSP Engine with special
processing information. The following table lists the 11 different attributes with a
brief description:

language Which language the file uses. <%@ page language = “java” %>

extends Superclass used by the JSPengine for the translated Servlet. <%@ page extends =
“com.taglib…” %>

import     Import all the classes in a java package into the current JSPpage. This allows the
JSP page to use other java classes. <%@ page import = “java.util.*”%>
2. Include directive

Allows a JSP developer to include contents of a file inside another. Typically
include files are used for navigation, tables, headers and footers that are common
to multiple pages.

Two examples of using include files:

This includes the html from privacy.html found in the include directory into the current jsp

<%@ include file = “include/privacy.html %> or to include a naviagation menu (jsp file)
found in the current directory.

<%@ include file = “navigation.jsp %> Include files are discussed in more detail in the later
sections of this tutorial.

3. Tag Lib directive

A tag lib is a collection of custom tags that can be used by the page.

<%@ taglib uri = “tag library URI” prefix = “tag Prefix” %>

Custom tags were introduced in JSP 1.1 and allow JSP developers to hide complex
server side code from web designers.

Action tag

There are three main roles of action tags :

1) enable the use of server side Javabeans
2) transfer control between pages
3) browser independent support for applets.
                                       JAVA BEANS

Java Beans component design conventions govern the properties of the class and govern
the public methods that give access to the properties.

A javaBeans component property can be :

      Read /write , read –only or write-only

      Simple ,which means it contains a single value , or indexed , which means it
       represents an array of values.

A property does not have to be implemented by an instance variable . it must simply be
accessible using public methods that conform to the following conventions:

      For each readable property , the bean must have a method of form type

      For each writable property , the bean must have a method of the form
       setProperty(type pc){……………….}

In addition to the property methods, a java beans component must define a constructor
that takes no parameters.

Using JavaBeans with JSP
In order to use a javabean in JSP , you have to use <jsp:useBean> standard action as follows


This standard action is used to create or access and instance of javaBean . It specifies class ,
scope and id for javaBean

<jsp:useBean id=”name” scope=”scope” class=”fullyqualifiedclassname” />


<jsp:useBean id=”count” scope=”page” class=”test.count” />
The above example creates an object of test.count class and assigns id count .Its life time is
the execution of the page. Remember the class count is to be placed in package test. This
package starts with classes directory (/web-inf/classes).

Once a bean is included in jsp , throughout the page the bean can be accessed using id of the
bean .the scope of the bean can be any of the following :

Scope                 Meaning

Page                 Objects are available throughout the page. They are released when
request is over or another page is invoked

Request               objects are available as long as the request is being processed.

Application           objects are available in any session that is in the same application as
the session that created the bean


It is used to set property of the bean to the given or the given value or the specified
parameter of the form

<jsp:setProperty name=”beanname” property=”propertyname or * “ value=”vlue” or
param=”parameter name” />

The following table shows the meaning of each attribute.

Attribute                    Meaning

Name                         Name of the bean defined by <jsp:useBean> tag

Property                      Name of the property to be changed, If the property is set to *
then the tag iterates over all the parameters of the request, matching parameter names and
value types to bean properties , and setting each matched property to the value of the
matching parameter.

Param                      Specifies the name of the request parameter whose value is to
be assigned to bean property.
Value                value to assign to the property. This cannot be given if param is
already given.


<jsp:setProperty name=”emp” property=”*” />

<jsp:setProperty name=”emp” property=”bs” param=”sal”/>

<jsp:setProperty name=”emp” property=”bs” value=”1200”/>

It is used to retrieve the value of the parameter

<jsp:getProperty name=”beanname” property=”propertyname” />

Name is the name of the bean specified by <jsp:useBean> tag and property is the name of
the property whose value is to be retrieved.

<jsp:getProperty name=”emp” property=”sal” />

To top