hSenid Lanka: Java Server Pages Presentation
hSenid Lanka: Java Server Pages Presentation
 Introduction to JSP
 Introduction to Servlet
 JSP vs Servlet
 Architecture of JSP
 Life Cycle of a JSP Page
 JSP Scripting Elements
 JSP Directives
 JSP Implicit Objects
 Transferring Control of JSP
 MVC in JSP
2
hSenid Lanka: Java Server Pages Presentation
 Demo
o Create a JSP Page
o Create a Servlet
o Configure Servlet Using web.xml
o Parse Data Between JSP & Servlet
o Set Default Error Page
3
hSenid Lanka: Java Server Pages Presentation
• JSP is a server-side programming technology.
• Developed by Sun Microsystems in 1999.
• Fast way to create web pages to display dynamic
data.
• JSP page contains both static & dynamic data.
4
hSenid Lanka: Java Server Pages Presentation
• Helps to write web applications easily even with
a less knowledge in Java.
• Extension of a JSP page is .jsp or .jspx
• Initially JSP was developed to replace servlet
but now common practice is to use servlet and JSP
together using MVC (model-view-controller)
pattern.
5
hSenid Lanka: Java Server Pages Presentation
• Servlet is a server-side program.
• Executes in a web server (eg: Tomcat).
• Receives HTTP requests from users and returns
HTTP responses.
• Written in Java.
• Runs on every platform that supports Java.
• Supported my most popular web servers.
6
hSenid Lanka: Java Server Pages Presentation 7
Relationship between JSP & Servlet
• JSP is an interface on top of Servlets.
• A JSP program is compiled into a Java servlet before
execution.
• Why JSP?
 Easier to write than servlets
 Designers can write HTML, programmers can write
Java portions
• Servlets came first, followed by JSP.
hSenid Lanka: Java Server Pages Presentation 8
Advantages of Servlets
• Performance
 Get loaded upon first request
 Multithreading
 Each request runs in its own separate thread
• Simplicity
 Run inside controlled server environment
 No specific client software is needed: Web
browser is enough
hSenid Lanka: Java Server Pages Presentation 9
Fig: JSP Processing
hSenid Lanka: Java Server Pages Presentation 10
Client’s
Computer Server
1.Browser requests HTML
7. Server sends HTML
back to browser
servlet
servlet
class 5.The servlet
runs and
generates
HTML
Java Engine
6. Java Engine sends HTML to server
2. Server sends requests to Java Engine
3. If needed, the Java Engine
reads the .jsp file
4. The JSP is turned into a
servlet, compiled, and loaded
Bean JSP
hSenid Lanka: Java Server Pages Presentation 11
• A JSP life cycle can be defined as the entire
process from its creation till the destruction
• The following are the paths followed by a JSP
 Compilation has 3 steps
 Parsing JSP
 Turning the JSP into a servlet
 Compiling the servlet
 Initialization
 Execution
 Destroy
hSenid Lanka: Java Server Pages Presentation 12
[destroy]
jspDestroy()
[Execution ]
_jspService()
[Intialization]
jspInit()
Request
Response
hSenid Lanka: Java Server Pages Presentation 13
 There are three types of scripting
elements in JSP,
o Scriptlet tag
o Expression tag
o Declaration tag
hSenid Lanka: Java Server Pages Presentation 14
Scriptlet Tag
 In JSP, JAVA code can be written inside the JSP page
using Scriptlet tag.
Syntax:
<% java source code %>
Example:
<html>
<body>
<% out.print(“Hello world…”); %>
</body>
</html>
hSenid Lanka: Java Server Pages Presentation 15
Expression Tag
 Code placed within expression tag is written to the
output stream of the response. So, no need to write
out.print() to write data.
Syntax:
<%= Statement %>
Example:
<html>
<body>
<%= “Hello world!” %>
</body>
</html>
Note: Do not end statement with semicolon (;)
hSenid Lanka: Java Server Pages Presentation 16
Declaration Tag
 Used to declare fields and methods. The code written
inside this tag is placed outside the service() method
of auto generated servlet. So it doesn’t get memory at
each request.
Syntax:
<%! Statement %>
Example:
<html>
<body>
<%! int data=60; %>
<%= “Value is: “ + data %>
</body>
</html>
hSenid Lanka: Java Server Pages Presentation 17
 There are two types of JSP comment types that we are using.
 Hidden Comments (JSP comments)
o Visible only inside JSP page but not in the generated
HTML page
o Example:
<%-- This is a hidden JSP comment --%>
 Comments within HTML content
o Comments to be generated inside the output HTML page
o Example:
<!-- Comment inside the generated HTML -->
hSenid Lanka: Java Server Pages Presentation 18
• Directives are messages that tells the web container
how to translate a JSP page into corresponding
servlet.
• Three types:
 page directive
 include directive
 taglib directive
• Syntax of JSP directives
<%@ directive attribute=“value” %>
hSenid Lanka: Java Server Pages Presentation 19
page directive
• Defines attributes that apply to an entire JSP page.
• Syntax:
<%@ page attribute=“value” %>
• Attributes :
import, contentType, extends, info, buffer, language, autoFlush, session,
pageEncoding, errorPage, isErrorPage
hSenid Lanka: Java Server Pages Presentation 20
include directive
• Includes the contents of any resource(may be jsp file,
html file or text file.
• It includes the original content of the included
resources at page translation time.
• Reusability is the advantage.
• Syntax:
<%@include file=“resourcename” %>
hSenid Lanka: Java Server Pages Presentation 21
taglib directive
• Used to define a tag library that defines many tags.
• We use the TLD (Tag Library Descriptor) file to
define the tags.
• Syntax:
<%@ taglib uri=“uriofthetaglibrary”
prefix=“prefixoftaglibrary” %>
hSenid Lanka: Java Server Pages Presentation 22
 There are several objects that are automatically available in
JSP called implicit objects.
Variable Type
out JspWriter
request HTTPServletRequest
response HTTPServletResponse
config ServletConfig
application ServletContext
session HTTPSession
pageContext PageContext
page Object
exception Throwable
hSenid Lanka: Java Server Pages Presentation 23
out implicit object
• For writing any data to the buffer, JSP provides an
implicit object named out.
Syntax:
out.print();
request implicit object
• Access to information associated with a request. This
object is normally used in looking up parameter values
and cookies.
<% String str = request.getParameter(“uname”); %>
hSenid Lanka: Java Server Pages Presentation 24
config implicit object
• This object can be used to get configuration
information for a particular JSP page.
• This variable information can be used for one JSP page
only.
application implicit object
• This object can be used to get configuration
information from configuration file(web.xml). This
variable information can be used for all JSP pages.
hSenid Lanka: Java Server Pages Presentation 25
session implicit object
• The Java developer can use this object to set, get or
remove attribute or to get session information.
pageContext implicit object
• The pageContext object can be used to set,get or remove
attribute from one of the following scopes:
i. Page ii. Request iii. Session iv. Application
exception implicit object
• This object can be used to print the exception. But it
can only be used in error pages.
hSenid Lanka: Java Server Pages Presentation 26
• Transferring control explain how to redirect user to
another page from JSP. There are two methods.
 sendRedirect()
 forward()
hSenid Lanka: Java Server Pages Presentation 27
sendRedirect()
• Request is transfer to another resource to different
domain or different server for further processing.
• Container transfers the request to client or browser
so URL given inside the sendRedirect method is visible
as a new request to the client.
• When sendRedirect method calls, old request and
response objects are lost because it is treated as new
request by the browser.
hSenid Lanka: Java Server Pages Presentation 28
sendRedirect() Cont.
• We can notice the browser address bar URL will change.
• This method is slower because it involves two tasks
(requests).
 Create a new request
 Remove old request
• If we need to pass data using this method, we may need
to use session or pass data along with the URL.
hSenid Lanka: Java Server Pages Presentation 29
forward()
• Request is transfer to other resource within the same
server for further processing.
• Web container handle all process internally and client
or browser is not involved.
• When forward method is called on requestdispatcher
object we pass request and response objects so our old
request object is present on new resource which is
going to process our request.
hSenid Lanka: Java Server Pages Presentation 30
forward() Cont.
• We cannot notice any change of the URL in browser
address bar.
• Faster than send redirect.
• When we redirect using forward and we want to use same
data in new resource we can use request.setAttribute()
as we have request object available.
hSenid Lanka: Java Server Pages Presentation 31
MVC – Model View Controller
• It is a design pattern that separates the business
logic, presentation logic and data.
• Model
 The back-end of the application.
 Can be legacy code or Java code (usually JavaBeans).
hSenid Lanka: Java Server Pages Presentation 32
• View
• The end user’s view (what does the end user see?).
• Mostly done using JSP.
• Controller
• Controls the overall flow of the application.
• Usually a servlet.
MVC Cont.
hSenid Lanka: Java Server Pages Presentation 33
(Client)
Browser
(Controller)
Servlet
(View)
JSP
(Model)
Java
Beans
Enterprise
Servers/
Data
sources
Request
Response
Application server
1
5
3
2
instantiate
4
hSenid Lanka: Java Server Pages Presentation 34
hSenid Lanka: Java Server Pages Presentation 35

Java Server Pages

  • 1.
    hSenid Lanka: JavaServer Pages Presentation
  • 2.
    hSenid Lanka: JavaServer Pages Presentation  Introduction to JSP  Introduction to Servlet  JSP vs Servlet  Architecture of JSP  Life Cycle of a JSP Page  JSP Scripting Elements  JSP Directives  JSP Implicit Objects  Transferring Control of JSP  MVC in JSP 2
  • 3.
    hSenid Lanka: JavaServer Pages Presentation  Demo o Create a JSP Page o Create a Servlet o Configure Servlet Using web.xml o Parse Data Between JSP & Servlet o Set Default Error Page 3
  • 4.
    hSenid Lanka: JavaServer Pages Presentation • JSP is a server-side programming technology. • Developed by Sun Microsystems in 1999. • Fast way to create web pages to display dynamic data. • JSP page contains both static & dynamic data. 4
  • 5.
    hSenid Lanka: JavaServer Pages Presentation • Helps to write web applications easily even with a less knowledge in Java. • Extension of a JSP page is .jsp or .jspx • Initially JSP was developed to replace servlet but now common practice is to use servlet and JSP together using MVC (model-view-controller) pattern. 5
  • 6.
    hSenid Lanka: JavaServer Pages Presentation • Servlet is a server-side program. • Executes in a web server (eg: Tomcat). • Receives HTTP requests from users and returns HTTP responses. • Written in Java. • Runs on every platform that supports Java. • Supported my most popular web servers. 6
  • 7.
    hSenid Lanka: JavaServer Pages Presentation 7 Relationship between JSP & Servlet • JSP is an interface on top of Servlets. • A JSP program is compiled into a Java servlet before execution. • Why JSP?  Easier to write than servlets  Designers can write HTML, programmers can write Java portions • Servlets came first, followed by JSP.
  • 8.
    hSenid Lanka: JavaServer Pages Presentation 8 Advantages of Servlets • Performance  Get loaded upon first request  Multithreading  Each request runs in its own separate thread • Simplicity  Run inside controlled server environment  No specific client software is needed: Web browser is enough
  • 9.
    hSenid Lanka: JavaServer Pages Presentation 9 Fig: JSP Processing
  • 10.
    hSenid Lanka: JavaServer Pages Presentation 10 Client’s Computer Server 1.Browser requests HTML 7. Server sends HTML back to browser servlet servlet class 5.The servlet runs and generates HTML Java Engine 6. Java Engine sends HTML to server 2. Server sends requests to Java Engine 3. If needed, the Java Engine reads the .jsp file 4. The JSP is turned into a servlet, compiled, and loaded Bean JSP
  • 11.
    hSenid Lanka: JavaServer Pages Presentation 11 • A JSP life cycle can be defined as the entire process from its creation till the destruction • The following are the paths followed by a JSP  Compilation has 3 steps  Parsing JSP  Turning the JSP into a servlet  Compiling the servlet  Initialization  Execution  Destroy
  • 12.
    hSenid Lanka: JavaServer Pages Presentation 12 [destroy] jspDestroy() [Execution ] _jspService() [Intialization] jspInit() Request Response
  • 13.
    hSenid Lanka: JavaServer Pages Presentation 13  There are three types of scripting elements in JSP, o Scriptlet tag o Expression tag o Declaration tag
  • 14.
    hSenid Lanka: JavaServer Pages Presentation 14 Scriptlet Tag  In JSP, JAVA code can be written inside the JSP page using Scriptlet tag. Syntax: <% java source code %> Example: <html> <body> <% out.print(“Hello world…”); %> </body> </html>
  • 15.
    hSenid Lanka: JavaServer Pages Presentation 15 Expression Tag  Code placed within expression tag is written to the output stream of the response. So, no need to write out.print() to write data. Syntax: <%= Statement %> Example: <html> <body> <%= “Hello world!” %> </body> </html> Note: Do not end statement with semicolon (;)
  • 16.
    hSenid Lanka: JavaServer Pages Presentation 16 Declaration Tag  Used to declare fields and methods. The code written inside this tag is placed outside the service() method of auto generated servlet. So it doesn’t get memory at each request. Syntax: <%! Statement %> Example: <html> <body> <%! int data=60; %> <%= “Value is: “ + data %> </body> </html>
  • 17.
    hSenid Lanka: JavaServer Pages Presentation 17  There are two types of JSP comment types that we are using.  Hidden Comments (JSP comments) o Visible only inside JSP page but not in the generated HTML page o Example: <%-- This is a hidden JSP comment --%>  Comments within HTML content o Comments to be generated inside the output HTML page o Example: <!-- Comment inside the generated HTML -->
  • 18.
    hSenid Lanka: JavaServer Pages Presentation 18 • Directives are messages that tells the web container how to translate a JSP page into corresponding servlet. • Three types:  page directive  include directive  taglib directive • Syntax of JSP directives <%@ directive attribute=“value” %>
  • 19.
    hSenid Lanka: JavaServer Pages Presentation 19 page directive • Defines attributes that apply to an entire JSP page. • Syntax: <%@ page attribute=“value” %> • Attributes : import, contentType, extends, info, buffer, language, autoFlush, session, pageEncoding, errorPage, isErrorPage
  • 20.
    hSenid Lanka: JavaServer Pages Presentation 20 include directive • Includes the contents of any resource(may be jsp file, html file or text file. • It includes the original content of the included resources at page translation time. • Reusability is the advantage. • Syntax: <%@include file=“resourcename” %>
  • 21.
    hSenid Lanka: JavaServer Pages Presentation 21 taglib directive • Used to define a tag library that defines many tags. • We use the TLD (Tag Library Descriptor) file to define the tags. • Syntax: <%@ taglib uri=“uriofthetaglibrary” prefix=“prefixoftaglibrary” %>
  • 22.
    hSenid Lanka: JavaServer Pages Presentation 22  There are several objects that are automatically available in JSP called implicit objects. Variable Type out JspWriter request HTTPServletRequest response HTTPServletResponse config ServletConfig application ServletContext session HTTPSession pageContext PageContext page Object exception Throwable
  • 23.
    hSenid Lanka: JavaServer Pages Presentation 23 out implicit object • For writing any data to the buffer, JSP provides an implicit object named out. Syntax: out.print(); request implicit object • Access to information associated with a request. This object is normally used in looking up parameter values and cookies. <% String str = request.getParameter(“uname”); %>
  • 24.
    hSenid Lanka: JavaServer Pages Presentation 24 config implicit object • This object can be used to get configuration information for a particular JSP page. • This variable information can be used for one JSP page only. application implicit object • This object can be used to get configuration information from configuration file(web.xml). This variable information can be used for all JSP pages.
  • 25.
    hSenid Lanka: JavaServer Pages Presentation 25 session implicit object • The Java developer can use this object to set, get or remove attribute or to get session information. pageContext implicit object • The pageContext object can be used to set,get or remove attribute from one of the following scopes: i. Page ii. Request iii. Session iv. Application exception implicit object • This object can be used to print the exception. But it can only be used in error pages.
  • 26.
    hSenid Lanka: JavaServer Pages Presentation 26 • Transferring control explain how to redirect user to another page from JSP. There are two methods.  sendRedirect()  forward()
  • 27.
    hSenid Lanka: JavaServer Pages Presentation 27 sendRedirect() • Request is transfer to another resource to different domain or different server for further processing. • Container transfers the request to client or browser so URL given inside the sendRedirect method is visible as a new request to the client. • When sendRedirect method calls, old request and response objects are lost because it is treated as new request by the browser.
  • 28.
    hSenid Lanka: JavaServer Pages Presentation 28 sendRedirect() Cont. • We can notice the browser address bar URL will change. • This method is slower because it involves two tasks (requests).  Create a new request  Remove old request • If we need to pass data using this method, we may need to use session or pass data along with the URL.
  • 29.
    hSenid Lanka: JavaServer Pages Presentation 29 forward() • Request is transfer to other resource within the same server for further processing. • Web container handle all process internally and client or browser is not involved. • When forward method is called on requestdispatcher object we pass request and response objects so our old request object is present on new resource which is going to process our request.
  • 30.
    hSenid Lanka: JavaServer Pages Presentation 30 forward() Cont. • We cannot notice any change of the URL in browser address bar. • Faster than send redirect. • When we redirect using forward and we want to use same data in new resource we can use request.setAttribute() as we have request object available.
  • 31.
    hSenid Lanka: JavaServer Pages Presentation 31 MVC – Model View Controller • It is a design pattern that separates the business logic, presentation logic and data. • Model  The back-end of the application.  Can be legacy code or Java code (usually JavaBeans).
  • 32.
    hSenid Lanka: JavaServer Pages Presentation 32 • View • The end user’s view (what does the end user see?). • Mostly done using JSP. • Controller • Controls the overall flow of the application. • Usually a servlet. MVC Cont.
  • 33.
    hSenid Lanka: JavaServer Pages Presentation 33 (Client) Browser (Controller) Servlet (View) JSP (Model) Java Beans Enterprise Servers/ Data sources Request Response Application server 1 5 3 2 instantiate 4
  • 34.
    hSenid Lanka: JavaServer Pages Presentation 34
  • 35.
    hSenid Lanka: JavaServer Pages Presentation 35