Title: Introduction to J2EE Enterprise Architecture The Web Tier JavaServer Pages
1Introduction to J2EE Enterprise Architecture
The Web TierJavaServer Pages
2The Need for JSP
- With servlets, it is easy to
- Read form data
- Read HTTP request headers
- Set HTTP status codes and response headers
- Use cookies and session tracking
- Share data among servlets
- Remember data between requests
- But tedious to
- Use println() statements to generate HTML
- Maintain that HTML
3JSP Framework
- Idea
- Use regular HTML for most of page
- Mark servlet code with special tags
- Entire JavaServer Page gets translated into a
servlet (once), and the generated servlet is what
actually gets invoked (for each request) - Example
- lt!DOCTYPE gt
- lthtmlgt
- ltheadgt lttitlegtOrder Confirmationlt/titlegt
- ltlink relstylesheet hrefstyles.css
type"text/css"gt - lt/headgt
- ltbodygt lth2gtOrder Confirmationlt/h2gt Thanks for
ordering - ltigt lt request.getParameter(itemname") gt lt/igt!
- lt/bodygt
- lt/htmlgt
4Benefits of JSP
- Although JSP technically can't do anything
servlets can't do, JSP makes it easier to - Write HTML
- Read and maintain the HTML
- JSP makes it possible to
- Use standard HTML tools such as Macromedia
DreamWeaver or Adobe GoLive. - JSP encourages you to
- Separate the (Java) code that creates the content
from the (HTML) code that presents it
5Advantages of JSP over competing technologies
- Versus ASP or ColdFusion
- Better language for dynamic part
- Portable to multiple servers and operating
systems - Versus PHP
- Better language for dynamic part
- Better tool support
- Versus pure servlets
- More convenient to create HTML
- Can use standard tools (e.g., DreamWeaver)
- Divide and conquer
- JSP programmers still need to know servlet
programming
6Advantages of JSP over competing technologies
- Versus Velocity or WebMacro
- Standard
- Versus client-side JavaScript (in browser)
- Capabilities mostly do not overlap with JSP, but
- You control server, not client
- Richer language
- Versus server-side JavaScript (e.g., LiveWire,
BroadVision) - Richer language
- Versus static HTML
- Dynamic features
- Adding dynamic features no longer all or
nothing decision
7Example
lt!DOCTYPE HTML PUBLIC "-//W3C//DTD
HTML 4.0 Transitional//EN"gt lthtmlgt ltheadgt
lttitlegtJSP Expressionslt/titlegt ltlink
rel"stylesheet" href"styles.css
type"text/css"gt lt/headgt ltbodygt lth2gtJSP
Expressionslt/h2gt ltulgt ltligtCurrent time
lt new java.util.Date() gt lt/ligt ltligtServer
lt application.getServerInfo() gt lt/ligt
ltligtSession ID lt session.getId() gt lt/ligt
ltligtThe ltcodegttestParamlt/codegt form parameter
lt request.getParameter("testParam") gt
lt/ligt lt/ulgt lt/bodygt lt/htmlgt
8Result
9Common misunderstandings
- Forgetting JSP is server-side
- Very common question
- I cant do such and such with HTML. Will JSP let
me do it? - Why doesnt this question make sense?
- JSP runs entirely on server
- It doesnt change content the client (browser)
can handle - Similar questions
- How do I put a normal applet in a JSP
page?Answer send an ltAPPLETgt tag to the client
- How do I put an image in a JSP page?Answer send
an ltIMG gt tag to the client - How do I use JavaScript/Acrobat/Shockwave/Etc?Ans
wer send the appropriate HTML tags
10Translation/Request time confusion
- What happens at page translation time?
- JSP constructs get translated into servlet code.
- What happens at request time?
- Servlet code gets executed. No interpretation of
JSP occurs at request time. The original JSP page
is totally ignored at request time only the
servlet that resulted from it is used. - When does page translation occur?
- Typically, the first time a JSP page is accessed
after it is modified. This should never happen to
real users (developers should test all JSP pages
they install). - Page translation does not occur for each request.
11JSP/Servlets in the real world
- ofoto.com
- print and manage digital and conventional photos.
- delta.com
- Delta Airlines entire Web site, including
real-time schedule info - americancentury.com
- American Century Investments more than 70 mutual
funds, 90 billion under management, two million
investors - excite.com
- Excite one of the top five Internet portals one
of the ten busiest sites on the Web
12Scripting Elements
- Expressions
- Format lt expression gt
- Expression is evaluated and the result converted
to a String object, then output as part of the
response to the client - E.g. lt new java.util.Date() gt
- Scriplets
- Format lt Java code gt
- Any java code can be inserted here which will be
inserted into the servlets _jspService() method - E.g.
- lt rb (ia.RequestBean)request.getAttribute("reque
st") - if (rb.getType().equalsIgnoreCase("H"))
-
- gt Hardware
- lt else
-
- gt Software
- lt gt
13Scripting Elements
- Declarations
- Format lt! codegt
- Inserted verbatim into the body of the servlet
class, outside of any existing methods - E,g.
- lt! ia.RequestBean rb
- SimpleDateFormat fm2
- new SimpleDateFormat("dd-MMM-yyyy
hhmm") gt - Comments
- JSP supports three types of comments
- XHTML comments
- Format lt!-- and --gt
- Can be placed throughout JSP, but not in
scriplets - JSP comments
- Format lt-- and --gt
- Can be placed throughout JSP, but not in
scriplets - Java comments
- Standard ones // and / /
- Placed only within scriplets
14Implicit Objects
- Provide programmers with access to many servlet
capabilities in the context of a Java Server Page - Four scopes
- Application scope
- Container e.g. Tomcat owns objects with
application scope - Any servlet or JSP can manipulate such objects
- Page scope
- Each page has its own instances of the page-scope
implicit objects - Request scope
- Exists for the duration of the request
- Go out of scope when request is completed with a
response to the client - Session scope
- Exists for the clients entire browsing session
15Implicit Objects
16Example
lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Transitional//EN"gt lthtmlgt ltheadgt
lttitlegtJSP Expressions and Declarationslt/titlegt
lt--ltlink rel"stylesheet" href"styles.css"
type"text/css"gt--gt lt/headgt lt! private int
count0 String bgColor "lightgreen" gt
ltbody bgcolor"lt bgColor gt"gt lth2gtJSP
Expressions and Declarationslt/h2gt ltulgt
ltligtCurrent time lt new java.util.Date() gt
lt/ligt ltligtServer lt application.getServerI
nfo() gt lt/ligt ltligtSession ID lt
session.getId() gt lt/ligt ltligtThe
ltCODEgttestParamlt/CODEgt form parameter
lt request.getParameter("testParam") gt lt/ligt
ltligtnumber of times visited ltcountgt
lt/ligt ltligtback ground color is ltbgColorgt
lt/ligt lt/ulgt lt/bodygt lt/htmlgt
17Results
18Standard Actions
- JSPs standard actions provide access to several
of the most common tasks performed in a JSP, such
as including content from other resources
forwarding requests to other resources and
interacting with JavaBeans. - Format ltjspactionnamegt and lt/jspactionnamegt
where actionname is the name of the standard
action
19Standard Actions
SourceDeitel Deitel
20Further information
- More information about servlets and JavaServer
Pages can be found on the Internet. - http//www.fcet.staffs.ac.uk/gdm1/msc/ia/ for the
Internet Applications module - http//java.sun.com/products/jsp/ for information
from Sun - http//www.google.co.uk/search?hlenqjspmeta
for a search on Google