Escolar Documentos
Profissional Documentos
Cultura Documentos
Nov/2012
GBS AS Brazil
Day Content
Revisiting how to write a servlet
Wha is a web container?
Java Server Pages(JSP)
Exercise: Rewrite the JSP submit values to the servlet
JSP Standard Tag Library
Exercise: Create a servlet which puts a collection into the request,
forward the response to the JSP and use JSTL to show the
collection content
AJAX: What is this?
How to use AJAX
Exercise: Ajaxfying the applications
Nov/2012
GBS AS Brazil
04/24/15
Nov/2012
GBS AS Brazil
04/24/15
Nov/2012
GBS AS Brazil
JSP Syntax
Declaration
Declares a variable or method
JSP Syntax:
<%! ... %>
XML Syntax:
<jsp:declaration>
declaration; [ declaration; ]+ ...
</jsp:declaration>
04/24/15
Nov/2012
GBS AS Brazil
JSP Syntax
Directive
Includes a static file in a JSP page, parsing the file's JSP
elements
JSP Syntax:
<%@ include file="relativeURL" %>
XML Syntax:
<jsp:directive.include file="relativeURL" />
04/24/15
Nov/2012
GBS AS Brazil
JSP Syntax
Expression
Contains an expression valid in the scripting language used in
the JSP page
JSP Syntax:
<%= expression %>
XML Syntax:
<jsp:expression>
expression
</jsp:expression>
04/24/15
Nov/2012
GBS AS Brazil
JSP Syntax
Scriptlet
Contains a code fragment
JSP Syntax:
<%= expression %>
XML Syntax:
<jsp:expression>
expression
</jsp:expression>
04/24/15
Nov/2012
GBS AS Brazil
JSP Syntax
Hidden comment
Documents the JSP page but is not inserted into the response.
JSP Sytax:
<%-- comment --%>
XML Sytax:
None
04/24/15
Nov/2012
GBS AS Brazil
JSP Syntax
Actions
<jsp:plugin> : Causes the execution of an applet or bean
<jsp:setProperty> : Sets a property value or values in a
bean
<jsp:useBean> : Locates or instantiates a bean with a specific
the result
<jsp:forward> : Forwards a request to a web resource
10
04/24/15
Nov/2012
GBS AS Brazil
11
04/24/15
Nov/2012
GBS AS Brazil
XML Syntax:
<jsp:root
xmlns:jsp=http://java.sun.com/JSP/Page
[ xmlns: tagPrefix ="URI" ]+... version="1.2">
JSP Page
</jsp:root>
Example:
<%@ taglib uri=http://www.jspcentral.com/tags prefix="public" %>
<public:loop>
...
</public:loop>
12
04/24/15
Nov/2012
GBS AS Brazil
<taglib>
<taglib-uri>http://abc.com/ex</taglib-uri>
<taglib-location>/WEB-INF/mytaglib.tld</taglib-location>
</taglib>
</web-app>
13
04/24/15
Nov/2012
GBS AS Brazil
JSP Contexts
Application (Servlet) Context
(multiple components within an application)
Session Context
(multiple request from a single client)
Request Context
(single request from single client)
Page Context
(single JSP page)
14
04/24/15
Nov/2012
GBS AS Brazil
JSP Lifecycle
Developer deploys the .jsp as part of web app
Container reads web.xml, but does not do anything with .jsp
Developer
Container
web.xml
MyJSP.jsp
Nov/2012
GBS AS Brazil
JSP Lifecycle
Client access the .jsp through link or servlet dispatcher
Container tries to translate .jsp do .java source for a servlet
Client
request
Container
MyJSP_jsp.java
translate
generate
MyJSP.jsp
Nov/2012
GBS AS Brazil
JSP Lifecycle
Container tries to compile the servlet .java source
into a .class file
Java language/syntax errors
are caught this time
Container
MyJSP_jsp.class
compile
MyJSP_jsp.java
generate
Nov/2012
GBS AS Brazil
JSP Lifecycle
Container loads the newly-generated servlet class
Container
load
MyJSP_jsp.class
Nov/2012
GBS AS Brazil
JSP Lifecycle
Container instantiates the servlet causing jspInit()
method to run
Servlet is initialized and ready
to accept client requests
Container
MyJSP_jsp
jspInit()
MyJSP_jsp
becomes
Nov/2012
GBS AS Brazil
JSP Lifecycle
Container creates or allocates a new thread to handle clients request
Runs the the servlets _jspservice() method
Container
MyJSP_jsp
_jspService()
Thread A
Everything that happens
after this is just plain old
servlet request-handling
Nov/2012
GBS AS Brazil
JSP lifecycle
Translation and compilation happens only once
At the first client access
Nov/2012
GBS AS Brazil
JSP Contexts
Page Context
Provides access to all the namespaces associated with a JSP page, provides access
to several page attributes, as well as a layer above the implementation details.
Request Context
An HTTP servlet request. It gets data from the client to the servlet for use in the
HttpServlet.service method. It allows the HTTP-protocol specified header information
to be accessed from the service method.
Session Context
The HttpSession interface is implemented by services to provide an association
between an HTTP client and HTTP server. This association, or session, persists over
multiple connections and/or requests during a given time period. Sessions are used to
maintain state and user identity across multiple page requests.
22
04/24/15
Nov/2012
GBS AS Brazil
JSP
Exercise 1: Create a JSP, and use it to submit
values to the servlet
Nov/2012
GBS AS Brazil
internationalization tags
<% taglib uri=http://java.sun.com/jstl/ea/fmt prefix="fmt" />
SQL tags
<% taglib uri=http://java.sun.com/jstl/ea/sql prefix="sql" />
24
04/24/15
Nov/2012
GBS AS Brazil
Nov/2012
GBS AS Brazil
Nov/2012
GBS AS Brazil
Nov/2012
GBS AS Brazil
AJAX
What is AJAX?
Asynchronous JavaScript + XML
Its a bunch of technologies:
Java Script
XHTML
CSS
Document Objeto Model (DOM)
XML and XSLT
Asyncronous data retrival using XMLHttpRequest JS object
Nov/2012
GBS AS Brazil
AJAX
AJAX changes the classic web pages behavior:
The classic behavior submits the entire page content
The AJAX approach breaks it in small requests, changing
small pieces at the page
Nov/2012
GBS AS Brazil
AJAX
Source: http://www.adaptivepath.com/ideas/ajax-newapproach-web-applications
Nov/2012
GBS AS Brazil
Nov/2012
GBS AS Brazil
AJAX
Exercise 3: Ajaxfying the code written at Exercise
3.
Nov/2012