8/3/2019 Session12-JSP(1)
1/26
JAD Session 12 JSP 1/26
Session 12
JavaServer Pages (JSP)
http://www.docu-track.com/buy/http://www.docu-track.com/buy/8/3/2019 Session12-JSP(1)
2/26
JAD Session 12 JSP 2/26
Objectives
JavaServer Pages (JSP)What JSP Technology is and how you can use it. JSP Page
Translation Time
How JSP Works?
JSP Elements
JSP Predefined Variable Implicit Objects
JSP Lifecycle
JSP Directives (chapter 6)The page DirectiveThe include DirectiveThe taglib Directive
http://www.docu-track.com/buy/http://www.docu-track.com/buy/8/3/2019 Session12-JSP(1)
3/26
JAD Session 12 JSP 3/26
JavaServer Pages (JSP)
What JSP Technology is and how you can use it. JavaServer Pages (JSP) technology provides a simplified,
fast way to create web pages that display dynamically-generated content.
The JSP 1.2 specification is an important part of the Java 2Platform, Enterprise Edition. Using JSP and EnterpriseJavaBeans technologies together is a great way toimplement distributed enterprise applications with web-based front ends.
The first place to check for information on JSP technologyis http://java.sun.com/products/jsp
/
http://java.sun.com/products/jsphttp://java.sun.com/products/jsphttp://www.docu-track.com/buy/http://www.docu-track.com/buy/http://java.sun.com/products/jsphttp://www.docu-track.com/buy/http://www.docu-track.com/buy/8/3/2019 Session12-JSP(1)
4/26
JAD Session 12 JSP 4/26
JSP Page
A JSP page is a page created by the web developer thatincludes JSP technology-specific tags, declarations, andpossibly scriptlets, in combination with other static HTML orXML tags.
A JSP page has the extension .jsp; this signals to the webserver that the JSP engine will process elements on this page.
Pages built using JSP technology are typically implemented
using a translation phase that is performed once, the firsttime the page is called. The page is compiled into a JavaServlet class and remains in server memory, so subsequentcalls to the page have very fast response times.
http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/8/3/2019 Session12-JSP(1)
5/26
JAD Session 12 JSP 5/26
Translation Time
A JSP application is usually a collection of JSP files,HTML files, graphics and other resources.
A JSP page is compiled when your user loads it into a
Web browser1. When the user loads the page for the first time, the files that
make up the application are all translated together, withoutany dynamic data, into one Java source file (a .java file)
2. The .java file is compiled to a .class file. In mostimplementations, the .java file is a Java servlet that complieswith the Java Servlet API.
http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/8/3/2019 Session12-JSP(1)
6/26
JAD Session 12 JSP 6/26
Simple JSP Page
First JSP Page
http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/8/3/2019 Session12-JSP(1)
7/26
JAD Session 12 JSP 7/26
How JSP Works?
User Request JSP File RequestedUser Request JSP File Requested
ServerServer
File
Changed
File
ChangedCreate Source from JSPCreate Source from JSP
CompileCompile Execute ServletExecute Servlet
http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/8/3/2019 Session12-JSP(1)
8/26
JAD Session 12 JSP 8/26
JSP Elements
Declarations
Expressions
Scriplets
http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/8/3/2019 Session12-JSP(1)
9/26
JAD Session 12 JSP 9/26
HTML Comment
Generates a comment that is sent to the client.
Syntax
Example:
http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/8/3/2019 Session12-JSP(1)
10/26
JAD Session 12 JSP 10/26
Declaration
Declares a variable or method valid in the scripting language used in theJSP page.
Syntax
Examples
You can declare any number of variables or methods within onedeclaration
element, as long as you end each declaration with a semicolon.
The declaration must be valid in the Java programming language.
http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/8/3/2019 Session12-JSP(1)
11/26
JAD Session 12 JSP 11/26
Declaration Demo
Demo\JSP1\dec.jsp
http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/8/3/2019 Session12-JSP(1)
12/26
JAD Session 12 JSP 12/26
Expression
Contains an expression valid in the scripting language used in the JSP page. Syntax
Description:An expression element contains a scripting language expression that is evaluated,
converted to a String, and inserted where the expression appears in the JSP file. Because the value of an expression is converted to a String, you can use an expressionwithin a line of text, whether or not it is tagged with HTML, in a JSPfile. Expressions areevaluated from left to right.
http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/8/3/2019 Session12-JSP(1)
13/26
JAD Session 12 JSP 13/26
Expression Demo
Demo\JSP1\exp.jsp
http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/8/3/2019 Session12-JSP(1)
14/26
JAD Session 12 JSP 14/26
Scriptlet
Contains a code fragment valid in the page scripting language.
Syntax
This code will be placed in the generated servlet method:_jspService()
http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/8/3/2019 Session12-JSP(1)
15/26
JAD Session 12 JSP 15/26
Scriplet Demo
Demo\JSP1\scriptlet.jsp
http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/8/3/2019 Session12-JSP(1)
16/26
JAD Session 12 JSP 16/26
JSP Predefined Variable Implicit
Objects request Object of HttpServletRequest (request parameters, HTTP headers, cookies
response Object of HttpServletResponse
out - Object of PrintWriter buffered version JspWriter
session - Object of HttpSession associated with the request
application - Object of ServletContext shared by all servlets in the engine
config - Object of ServletConfig
pageContext - Object of PageContext in JSP for a single point of access
page variable synonym for this object
http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/8/3/2019 Session12-JSP(1)
17/26
JAD Session 12 JSP 17/26
JSP Lifecycle
jspInit()jspInit()
jspDestroy()jspDestroy()
jspService()jspService()
Servlet from JSP
Init Event
RequestResponse
Destroy Event
http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/8/3/2019 Session12-JSP(1)
18/26
JAD Session 12 JSP 18/26
JSP Directives
Directives, whichlike scripting elementsarepieces of JSP tag-like syntax. Like an XML or HTML
tag, directives have attributes that dictate their
meaning and effect. In almost all cases, the effectsproduced by directives cant be replicated using
expressions, scriptlets, or declarations.
Well consider the three directives:
page,
include,
and taglib.
http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/8/3/2019 Session12-JSP(1)
19/26
JAD Session 12 JSP 19/26
The page Directive
You can include a page directive anywhere in
your JSP page source: beginning, middle, or
end.
An example of how one looks:
8/3/2019 Session12-JSP(1)
20/26
JAD Session 12 JSP 20/26
The page Directive-import attribute
Import attributeUse the import attribute to create import statements in
the generated servlet source produced by the JSPcontainers translation phase.
Example:
JSP Container will import 04 packages at translationtime:javax.servlet
javax.servlet.http
javax.servlet.jspjava.lang
http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/8/3/2019 Session12-JSP(1)
21/26
JAD Session 12 JSP 21/26
The page Directive - contentType
attribute The value of the contentType attribute is a String that specifiesthe MIME type of the response to be sent back.
-1) {os.write(info);
}os.flush();%>
http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/8/3/2019 Session12-JSP(1)
22/26
JAD Session 12 JSP 22/26
Other attributes of thepage directive
http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/8/3/2019 Session12-JSP(1)
23/26
JAD Session 12 JSP 23/26
The include Directive
A JSP source file and the pages it includes throughthe include directive are collectively referred to as a
translation unit.
The file type you include doesnt have to be JSP pagesource, nor does it have to have a .jsp extension.
Once included, the file contents must make sense to
the JSP translation process, but this gives scope to
include entire HTML or XML documents, ordocument fragments.
http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/8/3/2019 Session12-JSP(1)
24/26
JAD Session 12 JSP 24/26
The include Directive
8/3/2019 Session12-JSP(1)
25/26
JAD Session 12 JSP 25/26
The taglib Directive
The taglib directive makes custom actions
available in the JSP page by referencing a tag
library (will study in the next session).
http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/http://www.docu-track.com/buy/8/3/2019 Session12-JSP(1)
26/26
JAD Session 12 JSP 26/26
Summary JavaServer Pages (JSP)
What JSP Technology is and how you can use it.
JSP Page
Translation Time
How JSP Works?
JSP Elements
JSP Predefined Variable Implicit Objects
JSP Lifecycle
JSP Directives (chapter 6)
The page Directive
The include Directive
The taglib Directive
http://www.docu-track.com/buy/http://www.docu-track.com/buy/