JSP Handout v1.0
JSP Handout v1.0
Version: JSP/Handout/0408/1.0
Date: 04-04-08
Cognizant
500 Glen Pointe Center West
Teaneck, NJ 07666
Ph: 201-801-0233
[Link]
Handout - JSP
TABLE OF CONTENTS
Introduction ...................................................................................................................................5
About this Module .........................................................................................................................5
Target Audience ...........................................................................................................................5
Module Objectives ........................................................................................................................5
Pre-requisite .................................................................................................................................5
Session 02: Understanding JSP ..................................................................................................6
Learning Objectives ......................................................................................................................6
What is a JSP page? ....................................................................................................................6
When to use Servlet and when JSP .............................................................................................6
Summary ......................................................................................................................................6
Test Your Understanding..............................................................................................................6
Session 03: Sample JSP ................................................................................................................7
Learning Objectives ......................................................................................................................7
Try It Out .......................................................................................................................................7
Summary ......................................................................................................................................7
Test your Understanding ..............................................................................................................7
Session 04: JSP Architecture Model ............................................................................................8
Learning Objectives ......................................................................................................................8
JSP Model 1 Architecture .............................................................................................................8
JSP Model 2 Architecture .............................................................................................................8
Summary ......................................................................................................................................9
Test your Understanding ..............................................................................................................9
Session 05: JSP Syntax elements ..............................................................................................10
Learning Objectives ....................................................................................................................10
JSP Element:
JSP Element:
JSP Element:
JSP Element:
JSP Element:
JSP Element:
JSP Element:
Page 2
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
Summary ....................................................................................................................................11
Test your Understanding ............................................................................................................11
Session 07: Understanding JSP Page Directives .....................................................................12
Learning Objectives ....................................................................................................................12
JSP Page Directives ...................................................................................................................12
Try It Out .....................................................................................................................................13
Summary ....................................................................................................................................14
Test your Understanding ............................................................................................................14
Session 10: JSP Page Life Cycle ................................................................................................15
Learning Objectives ....................................................................................................................15
JSP Page Life Cycle ...................................................................................................................15
Summary ....................................................................................................................................15
Test your Understanding ............................................................................................................15
Session 12: JSP Model - Advanced ............................................................................................16
Learning Objectives ....................................................................................................................16
Translation process ....................................................................................................................16
JSP Pages as XML Documents .................................................................................................16
Try It Out .....................................................................................................................................16
Summary ....................................................................................................................................17
Test your Understanding ............................................................................................................17
Session 13: JSP Implicit Variables and Objects .......................................................................18
Learning Objectives ....................................................................................................................18
JSP Implicit Variables and Objects.............................................................................................18
Request ......................................................................................................................................18
Response....................................................................................................................................18
Session .......................................................................................................................................18
Config .........................................................................................................................................19
Application ..................................................................................................................................19
Page ...........................................................................................................................................19
PageContext ...............................................................................................................................19
Try It Out .....................................................................................................................................19
Summary ....................................................................................................................................20
Test your Understanding ............................................................................................................20
Session 16: JSP Page Scopes ....................................................................................................21
Learning Objectives ....................................................................................................................21
Page 3
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
JSP Page Scopes .......................................................................................................................21
Try It Out .....................................................................................................................................21
Summary ....................................................................................................................................22
Test your Understanding ............................................................................................................23
Session 18: JSP Static and Dynamic Inclusions ......................................................................24
Learning Objectives ....................................................................................................................24
Static Inclusions ..........................................................................................................................24
Dynamic Inclusions .....................................................................................................................24
Try It Out .....................................................................................................................................24
Summary ....................................................................................................................................25
Test your Understanding ............................................................................................................25
Session 21: Java Beans with JSP Scriptlets .............................................................................26
Learning Objectives ....................................................................................................................26
JavaBeans ..................................................................................................................................26
JavaBeans Component Design Conventions: ............................................................................26
JavaBeans Component property: ...............................................................................................26
Syntax: ........................................................................................................................................26
Try It Out .....................................................................................................................................27
Summary ....................................................................................................................................28
Test your Understanding ............................................................................................................28
Session 22: Java Beans with JSP Actions ................................................................................29
Learning Objectives ....................................................................................................................29
Java Beans and JSP ..................................................................................................................29
Creating and Using a JavaBeans Component: ..........................................................................29
Setting JavaBeans Component Properties: ...............................................................................29
Retrieving JavaBeans Component Properties: ..........................................................................29
Try It Out .....................................................................................................................................29
Summary ....................................................................................................................................30
Test your Understanding ............................................................................................................31
References ....................................................................................................................................32
Websites .....................................................................................................................................32
Books ..........................................................................................................................................32
STUDENT NOTES: ........................................................................................................................33
Page 4
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
Introduction
About this Module
This module provides an overview about following topics:
An introduction to JSP
Target Audience
This module is designed for the entry level trainees.
Module Objectives
After completing this module, you will be able to:
Pre-requisite
This module is designed for the trainees having basic knowledge on java.
Page 5
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
Static template data: It can be expressed in any text-based format, such as HTML,
SVG, WML, and XML.
Summary
They are good for non-HTML content, such as a file download servlet or a chart
generating servlet.
JSP is certainly better for HTML output because you can write a lot of the HTML as
plain HTML.
Page 6
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
Try It Out
Problem Statement:
Write a program to that prints this is an example for template text using html template, this is an
example of expression using JSP expression and this is an example of scriplet
Code:
<html>
<head>
<title>My Title</title>
</head>
<body>
<h1>This is an example for template text</h1>
<%="This is an example of expression"
%>
<br>
<%[Link]("This is an example of
scriplet");%>
</body>
</html>
How It Works:
Based upon the style it will prints
Summary
Static template data, which can be expressed in any text-based format, such as
HTML, SVG, WML, and XML.
Page 7
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
While this may not seem to be much of a problem for Java developers, it is certainly
an issue if your JSP pages are created and maintained by designers, which is usually
the norm on large projects.
Page 8
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
Summary
JSP Model 1 Architecture: The JSP page alone is responsible for processing the
incoming request and replying back to the client. Separation of presentation from
content.
Page 9
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
Page 10
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
Try It Out
Problem Statement:
Create [Link] using all the jsp syntax elemts.t
Code:
<%=WelcomeJsp %>
<% [Link](Welcome JSP): %>
<%! Striing str= (Welcome JSP); %>
How It Works:
Based upon the JSP syntax it will work
Summary
JSP Declaration: It is inserted in body of servlet class, outside the service method.
JSP Comment: Comment is ignored, when JSP page is translated into servlet.
Page 11
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
Page 12
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
JSP Page Directives : isErrorPage
Used to specify if the jsp page is an error page or not
JSP Page Directives : pageEncoding
Used to specify the language that the page uses when the page is sent to the browser.
JSP Page Directives : isELIgnored
Used to control the EL expression evaluation in the JSP page.
Try It Out
Problem Statement:
Create a JSP page containing page directives
Code:
package [Link];
public class Perform {
public String getWord(){
return "hello world";
}
public void createError(){
int i=10/0;
}
Page 13
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
Summary
Attributes are as follows:
language: Used for specifying other scripting languages to be used in a JSP page
extends: Used for specifying some other java classes to be used in a JSP page
buffer: Used by the out object to handle output generated by the JSP page
errorPage: Used to set the error page to be called if any error occurs
pageEncoding: Used to specify the language that the page uses when the page is
sent to the browser
Page 14
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
Execution: It is where the compiled and initialized Servlet (JSP) handles the requests
Summary
Translation: This is the place, where the JSP is converted into a Java file.
Compilation: This is the place, where the translated java file is compiled to a class
file.
Execution: This is the place, where the compiled and initialized Servlet (JSP) handles
the requests.
Page 15
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
Translation process
The JSP page is first converted into a Java file. This java file is compiled to a class file
and is initialized to handle http requests, just like http Servlet.
The best part is a JSP developer, who need not do this translation manually. The
Servlet container (Tomcat) does this translation.
A JSP document is an XML document and must comply with the XML standard.
Fundamentally, this means that a JSP document must be well formed, meaning that
each start tag must have a corresponding end tag and that the document must have
only one root element.
In addition, JSP elements included in the JSP document must comply with the XML
syntax.
Try It Out
Problem Statement:
Create JSP named [Link] with only XML tags. The page should contain a instance variable
named names of type [Link]. Add three names of your choice to that list. And then iterate
though the list to print on the browser all the contents of the list.
This can be using the following XML tags:
<jsp:directive>, <sp:declaration>, <jsp:expression> and <jsp:scriptlet>>.
Code:
<jsp:root xmlns:jsp="[Link] version="1.2">
<jsp:[Link] import="[Link].*"/>
<jsp:declaration>private List names;</jsp:declaration>
<jsp:scriptlet>
names=new ArrayList();
[Link]("abc");
[Link]("def");
[Link]("ghi");
for(Object temp:names){
</jsp:scriptlet>
Page 16
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
<jsp:expression>
temp
</jsp:expression>
<br></br>
<jsp:scriptlet>}</jsp:scriptlet>
</jsp:root>
Summary
Translation: This is the place, where the JSP is converted into a Java file.
A JSP document is an XML document and must comply with the XML standard.
Page 17
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
Request
The class or the interface name of the object request is [Link]. The object request
is of type [Link]. This denotes the data included with the HTTP
Request. The client first makes a request that is then passed to the server. The requested object is
used to take the value from clients web browser and pass it to the server. This is performed using
HTTP request like headers, cookies and arguments.
Response
This denotes the HTTP Response data. The result or the information from a request is denoted by
this object. This is in contrast to the request object. The class or the interface name of the object
response is [Link]. The object response is of type [Link].
>httpservletresponse. Generally, the object response is used with cookies. The response object is
also used with HTTP Headers
Session
This denotes the data associated with a specific session of user. The class or the interface name
of the object Session is [Link]. The object Session is of type
[Link]. The previous two objects, request and response, are used to pass
information from web browser to server and from server to web browser respectively. The Session
Object provides the connection or association between the client and the server. The main use of
Session Objects is for maintaining states when there are multiple page requests. This will be
explained in further detail in following
Page 18
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
Config
This is used to get information regarding the Servlet configuration, stored in the Config object. The
class or the interface name of the Config object is ServletConfig. The object Config is written
[Link]
Application
This is used to share the data with all application pages. The class or the interface name of the
Application object is ServletContext. The Application object is written:
[Link]
Page
The Page object denotes the JSP page, used for calling any instance of a Page's servlet. The
class or the interface name of the Page object is [Link]. The Page object is written:
[Link]
PageContext
This is used to access page attributes and also to access all the namespaces associated with a
JSP page. The class or the interface name of the object PageContext is [Link]. The
object PageContext is written: [Link]
Try It Out
Problem Statement:
Create a jsp named [Link]. Set attributes in all the 4 possible scopes and try to retrieve them.
Also display on the browser the servlet name using the SerlvetConfig instance.
This sample uses the following implicit variables: request, out, session, config, application
Code:
<html>
<body>
<% [Link]("req","Req Atribute");
[Link]("sess","Session Atribute");
[Link]("context","Context Atribute");
[Link]("page","Page Atribute");
%>
<%= [Link]("req")%><br>
<%= [Link]("sess")%><br>
<%= [Link]("context")%><br>
<%= [Link]("page")%><br>
<%= [Link]()%>
</body>
</html>
Page 19
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
How It Works:
The attributes are stored in various scopes and are retrieved and displayed. Servlet name is
displayed to show the config parameter.
Summary
Page 20
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
Page Scope
Request Scope
Session Scope
Application Scope
Try It Out
Problem Statement:
Write a web application that has an index page, which takes username from the user.
If submitted, it has to call a servlet, which then calls another jsp. This jsp has to print
the username attached with the request.
System should retrieve the contact email configured as an init param for whole of the
application:
Page 21
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
Code:
<HTML>
<HEAD>
<TITLE>Page Scopes Example</TITLE>
</HEAD>
<BODY>
<FORM action=[Link] method=POST>
Username:<INPUT type=text name=username>
Password:<INPUT type=text name=password>
<INPUT type=submit value=go!>
</FORM>
</BODY>
</HTML>
<html>
<head>
<title>Index Page</title>
</head>
<body>
<h2><%= [Link]("userName") %></h2>
<jsp:useBean id="name" class="[Link]" scope="request" />
<jsp:useBean id="name" class="[Link]" scope="session" />
<jsp:useBean id="name" class="[Link]" scope="application"
/>
</body>
</html>
How It Works:
The [Link] calls the servlet mapped for the [Link] call. The servlet sets the
username as an attribute to the scopes request, session and application.
It then forwards the request to the [Link]. It then prints the values from the different
scopes.
Summary
Page 22
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
Test your Understanding
1. What do you understand by JSP Page Scopes?
Page 23
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
Static Inclusions
It can be done using the "include" directive.
<% @include file="[Link]" %>
Dynamic Inclusions
It can be done using the <jsp: include> action tag
<jsp:include page="[Link]" />
Example:
<% @include file="[Link]" %>
< jsp:include page="[Link]"/>
Try It Out
Problem Statement:
Write an index page that includes a header page and a proverb page. The proverb page follows
the header and it displays a proverb, which changes every one hour. The header contains the
name and a logo.
The page should be able to display the header as well as the dynamic proverb page
Code:
<%@ include file="/[Link]" %>
Proverb: <jsp:include page=/[Link] " />
<br>
<br>
<h1>Static and Dynamic Includes</h1>
Start Tomcat server. Open a browser and type the [Link] page
Now do step 1 and see that the new proverb is getting displayed in the index page.
Page 24
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
Summary
If any of the contents in the jsp page is dynamic, use dynamic includes. In other
conditions static includes helps improve systems performance.
Page 25
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
JavaBeans
JavaBeans components are Java classes that can be easily reused and composed
together into applications.
Any Java class that follows certain design conventions can be a JavaBeans
component.
You can easily create and initialize beans and get and set the values of their
properties.
Simple, which means it contains a single value, or indexed, which means it represents
an array of values..
For each readable property, the bean must have a method of the form:
For each writable property, the bean must have a method of the form:
Syntax:
One of the ways to set JavaBeans component properties in a JSP page is using a scriptlet.
<% [Link](value); %>
Retrieving JavaBeans Component Properties:
<% Object o = [Link](); %>
Example:
<% [Link](bookId); %>
<%
%>
Page 26
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
Try It Out
Problem Statement:
Create a jsp named [Link] and a bean class named [Link] with accessor methods
for name and [Link] the instance variables of the bean class and then get the variables from the
stored request attributes and display in the browser.
Code:
public class Student{
String id = "";
String name = "";
public void setId(String id) {
[Link] = id;
}
public String getId() { return id; }
public void setName(String name) {
[Link] = name;
}
public String getName() {
return name;
}
Page 27
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
Summary
JavaBeans components are Java classes that can be easily reused and composed
together into applications.
Any Java class that follows certain design conventions can be a JavaBeans
component.
You can easily create and initialize beans and get and set the values of their
properties.
Page 28
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
You can easily create and initialize beans and set the values of their properties.
Try It Out
Problem Statement:
Write a JSP page that demonstrates the usage of JSP Action tags. Also develop a bean class to
be called from the JSP page.
The JSP page should declare a variable of that bean type, set the properties of that bean and in
the end it has to display the properties of that bean:
Page 29
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
Code:
<html>
<head>
<title>Acessing Bean Class Using JSP Action</title>
</head>
<body>
<jsp:useBean id="student" class="[Link]"/>
<jsp:setProperty name="student" property="name" value="John"/>
<jsp:setProperty name="student" property="id" value="123456"/>
<h1>The Name and Id of the Students are<br></h1>
<h2>Name :
<jsp:getProperty name="student" property="name"/><br>
Id :<jsp:getProperty name="student" property="id"/></h2>
</body>
</html>
package [Link];
public class Student{
String id = "";
String name = "";
public void setId(String id) {
[Link] = id;
}
public String getId() { return id; }
public void setName(String name) {
[Link] = name;
}
public String getName() {
return name;
}
}
How It Works:
The bean has to be compiled and the class file has to be present in the classpath.
Start the Tomcat server. Access the [Link] page from the browser.
The JSP actions aids users develop scripless pages, which is easy in terms of
maintaining and is easily readable.
Summary
JSP technology directly supports using JavaBeans components with JSP language
elements.
You can easily create and initialize beans and set the values of their properties.
Page 30
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
Test your Understanding
1. What is a JSP Bean Class?
2. How can you access Bean class from JSP using JSP Action?>
Page 31
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
References
Websites
[Link]
ml
Books
Page 32
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected
Handout - JSP
STUDENT NOTES:
Page 33
Copyright 2007, Cognizant Technology Solutions, All Rights Reserved
C3: Protected