Você está na página 1de 32

Global Business Services AS Brazil

Advanced Java Training Day 2

Servlets and JSPs technology

Nov/2012

2012 IBM Corporation

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

2012 IBM Corporation

GBS AS Brazil

JSP (Java Server Pages)


Dinamically generates HTML, XML or other documents
responding to a WEB client request;
Uses XML-like tags that encapsulate the logic;
Its an extension of Java Servlet Technology;
It looks like HTML more than Servlets;

04/24/15

Nov/2012

2012 IBM Corporation

GBS AS Brazil

JSP (Java Server Pages)


File extesion should be .jsp, instead of .html or .htm;
Must be deployed on a server that supports JSP;
Pages built using JSP are compiled into a Java Servlet at the
first time the page is called;
After that, subsequent requests are redirected to the servlet
generated by that page, having very fast response times;

04/24/15

Nov/2012

2012 IBM Corporation

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

2012 IBM Corporation

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

2012 IBM Corporation

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

2012 IBM Corporation

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

2012 IBM Corporation

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

2012 IBM Corporation

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

name and scope


<jsp:getProperty> : Inserts the value of a bean property into

the result
<jsp:forward> : Forwards a request to a web resource

10

04/24/15

Nov/2012

2012 IBM Corporation

GBS AS Brazil

JSP Taglib Directive


Defines a tag library and prefix for the custom tags used in
the JSP page
It declares that the JSP page uses custom tags, names the
tag library that defines them, and specifies their tag prefix
must use a taglib directive before you use the custom tag in
a JSP page
More than one taglib can be used, but prefix defined must be
unique

11

04/24/15

Nov/2012

2012 IBM Corporation

GBS AS Brazil

JSP Taglib Directive


JSP Syntax:
<%@ taglib uri="URIForLibrary" prefix="tagPrefix" %>

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

2012 IBM Corporation

GBS AS Brazil

JSP Custom Tags


User-defined JSP language element;
URI that identifies prefix doesnt need to be real (its just a identifier)
Relationship between identifier and the library occurs on web.xml file.
<web-app>

<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

2012 IBM Corporation

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

2012 IBM Corporation

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

2012 IBM Corporation

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

JSP syntax errors are


caught in this phase

Nov/2012

2012 IBM Corporation

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

2012 IBM Corporation

GBS AS Brazil

JSP Lifecycle
Container loads the newly-generated servlet class

Container
load

MyJSP_jsp.class

Nov/2012

2012 IBM Corporation

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

2012 IBM Corporation

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

2012 IBM Corporation

GBS AS Brazil

JSP lifecycle
Translation and compilation happens only once
At the first client access

Generated servlet eventually sends a response


back to the client or forwards the request to
another web application componet
Important methods
jspInit()
jspDestroy()
_jspService()

Nov/2012

2012 IBM Corporation

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.

Servlet Context (Application Context)


It gives servlets access to information about their environment, and allows them to log
significant events. The interface is implemented by services, and used by servlets.

22

04/24/15

Nov/2012

2012 IBM Corporation

GBS AS Brazil

JSP
Exercise 1: Create a JSP, and use it to submit
values to the servlet

Nov/2012

2012 IBM Corporation

GBS AS Brazil

JSTL JSP Standard Tag Library


Encapsulates as simple tags the core functionality common
to many Web applications
iteration and conditionals
<% taglib uri=http://java.sun.com/jstl/ea/core prefix="c" />

tags for manipulating XML documents


<% taglib uri=http://java.sun.com/jstl/ea/xml prefix="xml" />

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

2012 IBM Corporation

GBS AS Brazil

JSTL JSP Standard Tag Library


How to use it?
Get the JSTL library from http://jstl.java.net/
Add it to the application classpath(WEB-INF/lib)
Declare the tags at the JSP

Nov/2012

2012 IBM Corporation

GBS AS Brazil

JSTL JSP Standard Tag Library


JSP with JSTL example:

Nov/2012

2012 IBM Corporation

GBS AS Brazil

JSTL JSP Standard Tag Library


Exercise 2: Create a servlet which puts a
collection into the request, forward the response
to the JSP and use JSTL to show the collection
content

Nov/2012

2012 IBM Corporation

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

2012 IBM Corporation

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

2012 IBM Corporation

GBS AS Brazil

AJAX

Source: http://www.adaptivepath.com/ideas/ajax-newapproach-web-applications

Nov/2012

2012 IBM Corporation

GBS AS Brazil

How to use AJAX

Nov/2012

2012 IBM Corporation

GBS AS Brazil

AJAX
Exercise 3: Ajaxfying the code written at Exercise
3.

Nov/2012

2012 IBM Corporation

Você também pode gostar