You are on page 1of 93

Java EE Web Development Training

Java Server Pages


Objectives

• Describe Java Server Pages (JSP) technology,

• Write JSP code using scripting elements,


• Write JSP code using directives,
• Write JSP code using action elements,
• Write an error page using JSP
Objectives

• Demonstrate on how to create a JSP file, and

• Deploy JSP files using the Apache Tomcat


web server.
JSP Concepts
Java Server Pages (JSP)

• Sun Microsystems' solution for developing


dynamic content web site
• Provides excellent server side scripting support
for creating web application

• Enable the developers to directly insert the

Java code to JSP file


Java Server Pages (JSP)

• Efficient, it loads into the web servers memory


on receiving the request the very first time and
the subsequent calls are served within a very

short period of time


• Files with .jsp extension
JSP Architecture
JSP Architecture
JSP Architecture

• JSP pages are high-level extension of servlet


• JSP files are compiled into a servlet by a JSP

engine

• Compiled servlet is used by the engine to serve


request
JSPs and Servlet

• JSP extends and depends on the Java Servlet

API and implementation


• JSPs are converted to Java Servlets
• JSP Implementation class is an underlying
servlet representation
JSPs and Servlets
• All the benefits provided to the Java Servlet

component is the same as to the JSP by its web


container

• JSP page can also refer to request, response and


session management objects created by a web

container
Phases of JSP
JSP Phases
Language Basics
Conceptual Model of JSP Components
JSP Lifecycle Methods (2 plus 1)

• public void jspInit()


• public void _jspService(HttpServletRequest
request, HttpServletResponse responsea0
throws ServletException, IOException()

• public void jspDestroy()


How A JSP is Processed
Language Basics

• JSP file can contain a mixture of data


(HTML, XML)
• Elements are defined within begin and end tags
and are the portions that are translated and
compiled by the JSP compiler into Servlets
JSP Constructs

• Scripting Elements

• Directives
• Actions
Scripting Elements
JSP Constructs – Scripting Elements
JSP Constructs – Comments

• HTML client side comment <!– This is a


comment. -->
• JSP server side comment <%-- This is a
comment. --%>

• Java comment (on scriptlet and on declaration)


//, /* */, /** */
JSP Constructs – Scripting Elements

• Variable and declarations


• Expressions to be evaluated

• Scriptlet

• Comment
JSP Comment

• Execution lines that are ignored during


processing

<%-- This is a JSP comment line --%>


JSP Scripting Comment

• This is an example of a scriptlet comment


<%
/**
This is a Java-based JSP comment line
*/
%>
JSP Scripting Elements - Declaration

• Embed code at the top level of the generated


Java Servlet class

• Are suitable for member variables, methods


and inner classes

• Defined as <% ! ....... %> character sequences


Sample Code - Declaration
Sample Code - Declaration
JSP Scripting Elements - Declaration

• For java-specific declaration, one or more

comma separated Java variable and method


declarations can be defined within a JSP
declaration
JSP Scripting Element - Expression

• Element that define statements that are

evaluated during request processing time and


embedded in the HTML output

• Output of the expression is of type String


• Defined <%= %> character sequences
JSP Expression Demo
JSP Scripting Element - Scriplet

• Allows arbitrary code in the generated Java


Servlet
• General purpose for executing Java statements,

declare local variables and call methods,


evaluate expressions, and utilize JSP implicit
objects
JSP Scripting Element - Scriplet

• Executed during the processing of JSP client

requests with any output generated to the


response output
• Defined within <% ... %> character sequences
JSP Scriplet Demo
Output of Scriplet Demo
Directive Elements
JSP Constructs – Directives

• Interpreted at translation time


• Inclusion of other files

• Defining attributes about a JSP file being

translated
General Form of a JSP Directive
Types of JSP Directive

• include
• page

• taglib
JSP Directive - include

• Merges page fragments at translation


• Used for file insertion to a JSP file

• Inclusion may be recursive


• Types of file: text, HTML or JSP file
JSP Directive - include

• Has one attribute, “file” - value is the relative

URL of the file to be included


• Types of file: text, HTML or JSP file
• URLs beginning with “/” is relative to the
context root
Sample include Directive
JSP Directive - page
• Use to define information about a JSP file and

any of its statically included files


• JSP containers use page directive to determine
the nature and characteristics of a JSP file

• Has various standard attributes that can be


defined only once
page Directive Attribute - language

• Specifies the type of scripting language that is

used inside the JSP


• The only defined, required and default is java
page Directive Attribute - contentType

• Value is “text/html”

• Defines the MIME type of the output stream


page Directive Attribute - pageEncoding

• Value is “ISO-8859-1”

• Defines the character encoding of the output


page Directive Attribute - import

• Specifies a comma-separated list of classes and

packages used by JSP


• Similar to java import statements
• Implicitly defined: java.lang.*, javax.servlet.*,
javax.servlet.http.*, and javax.servlet.jsp
page Directive Attribute - session

• Specifies a literal value of “true” or “false”

indicating whether this JSP uses HTTP session


• Default is true
page Directive Attribute - errorPage

• Specifies a URL for a JSP that handles any

Throwable exception object that were


uncaught by JSPs and made their way to the
JSP container
page Directive Attribute - isErrorPage

• Specifies a value of true or false indicating


whether this JSP is an error page to handle

uncaught JSP exceptions


• Default is false
Sample Use of page Directives
Sample Code - JSP Session
Sample Code – JSP Session
Sample Code – JSP Session
Sample Output – JSP Session
Sample Output – JSP Session
Built-in JSP Objects
Built-in JSP Objects

• request – represents the current request from

the browser and is a sublass of ServletRequest;


instance of HttpServletRequest
• response – instance of HttpServletResponse
Built-in JSP Objects

• out – responsible for writing responses back to

the browser; an instance of the JspWriter class


• session – instance of HttpSession for the given
user of the request. This variable does not exists
if the JSP is not participating in sessions
Built-in JSP Objects

• page – equivalent to the this variable in the Java


programming language

• application - instance of the ServletContext


(manages data at the application level) for the
web application
Built-in JSP Objects

• config – the ServletConfig object associated


with the Java Servlet for this JSP page
• exception – represents the exception type

object generated; true if the JSP file could


deal with exceptions
Built-in JSP Objects

• pageContext – the pageContext object that


encapsulates the environment of a single
request for this JSP page
Action Elements
JSP Constructs – Action

• Standard tags that start with <jsp:


• Standard tags that often operate on
JavaBeans
• JavaBeans must follow the correct coding
convention
Review on JavaBean

• Instance variables must be declared as private


with setter and getter methods
• JavaBeans must have a no-argument
constructor
• JavaBeans should implement the
java.io.Serializable
Using JSP useBean
The jsp Action Tags

• Tags that affect the runtime behavior of the JSP


and the response sent back to the client

• Provided by all web containers, irrespective of


the implementation
Examples of jsp Action Tags

• <jsp:useBean>

• <jsp:setProperty>
• <jsp:getProperty>
The <jsp:useBean> Tag

• Used for instantiating a JavaBean


• Locating a bean instance and assigns it to a
variable name to be used in the JSP page
• Lifetime of the bean object can be defined
using a specified scope
The <jsp:useBean> Tag

Syntax of the <jsp:useBean>

<jsp:useBean id=“name” class=“className”


scope=“scopeName”/> or

<jsp:useBean id=“name” class=“className”


scope=“scopeName”></jsp:useBean>
The <jsp:useBean> Tag Attributes

• id – the case sensitive name to identify the


object instance
• scope – the scope within the reference is
available. Possible values are “page”, “request”,
“session”, and “application”. The default value
is “page”
• class – the fully qualified class name
The <jsp:useBean> Scope Values

• page – an object with page scope is bound to


the javax.jsp.PageContext. The object is
associated with this particular request to this
page
• request – bound to the HttpServletRequest and
can be accessed by invoking the getAttribute( )
method on the implicit request object
The <jsp:useBean> Scope Values

• session – an object with session scope is bound


to the javax.servlet.http.HttpSession and can be
accessed by invoking the getAttribute( ) method
on the implicit session object. The object will be
available during any request made by the same
client within the current session
The <jsp:useBean> Scope Values

• application – the object is bound to


ServletContext. An object with this scope can
be accessed by invoking the getAttribute( )
method on the implicit application object. The
object will be available in any JSP page within
the same web application.
JSP Requirement For JavaBeans

• Beans must belong to a Java package - side


effect of the JSP compiler which, when
generating a compiled JSP page, places the
generated code into a package (directory)
corresponding to the source's directory
structure
The <jsp:setProperty> Tag

• Used in conjunction with <jsp:useBean> tag

• Used for setting the value of bean properties


• Stores data in JavaBean instances
• Property values can be set from parameters or
literal values
The <jsp:setProperty> Tag

property_expression can be any of the following:

• property = “*”

• property = “propertyName”

• property = “propertyName” param=“paramName”

• property = “propertyName” value=“value”


The <jsp:setProperty> Tag

• The asterisk (*) can be used to specify all properties


of the bean

• Insert sample code here


The <jsp:setProperty> Tag

• The asterisk (*) can be used to specify all properties


of the bean
The <jsp:setProperty> Tag

The code from the previous slide is equivalent to


The <jsp:getProperty> Tag

• Complementary to the <jsp:setProperty>

• Used for accessing the properties of a bean


• Accesses the value of a property, converts it to
String, and prints it to the output stream to the
client
The <jsp:getProperty> Tag - Conversion

• Invokes the toString( ) method on the property


if it is an object

• Converts the value directly to a String


• If it is primitive, using the valueOf( ) method of
the corresponding wrapper class for the
primitive type
The <jsp:getProperty> Tag

• Syntax tag :
<jsp:getProperty name=”name” property=”propertyName” />

Where
name – name of the bean instance from this property is obtained
property – name the property to get; this is an instance variable
in the bean (create first the bean before using the
<jsp:getProperty>, by using <jsp:useBean> or by instantiating
the object with a new operator)
Code - useBean Demo
Code - useBean Demo
Code - useBean Demo
Code - useBean Demo
Code useBean Demo Output
Code - useBean Demo Output
Looking Back
Objectives

• Described Java Server Pages (JSP) technology,

• Wrote JSP code using scripting elements,


• Wrote JSP code using directives,
• Wrote JSP code using action elements,
• Wrote an error page using JSP
Objectives

• Demonstrated on how to create a JSP file,


and

• Deployed JSP files using the Apache Tomcat


web server.
Thank You!

You might also like