Title: Application Architectures for Interactive Web Applications: Java JSP and Model/View/Controller Techniques
1Application Architectures for Interactive Web
Applications Java JSP and Model/View/Controller
Techniques
- Jeff Schmitt
- November 16, 2001
2ABSTRACT
- This talk is the report of my sabbatical leave
during Spring 2001 in - which I explored some new techniques in using the
Java language to - develop interactive web applications.
- Java Server Pages technology (JSP) facilitates
the development of - interactive websites with dynamically generated
content. JSP programs - contain ordinary HTML tags mixed with Java code.
Custom tag libraries - and Template processing are techniques which
allow the Java code to be - isolated from the HTML. This application
architecture is - based on the principles of Model/View/Controller.
3Web Application Components
- Client -- typically a Web Browser
- HTTP -- request, response protocol between client
and server - HTML files -- static web content
- Webserver -- serves HTML files and
- looks for certain request patterns, then forwards
request to servlet engine, for example
/servlet/ - Servlet Engine -- supports execution of servlets
- Servlet -- handles request from client, generates
response to client - JSP Engine -- supports the compilation of files
into Servlets - Web Application -- a collection of HTML files,
Servlets and JSP pages to implement an
information system on the web.
4Tiered Systems
5Servlet Lifecycle
service()
init()
destroy()
6Servlet Sequence Diagram
Client
Webserver
Servlet
7Servlet Multithreading
Client 1
Servlet
Client 2
8ServletsAdvantages Disadvantages
- Servlet mapping(servlet is not a web page)
- Hard for Web Designer to change the HTML part of
the code - HTML (presentation) and Java code (business
logic) mixed together
- Scalable
- Persistent
- Simple
- Flexible
- Support
- Stable API
- Run in separate memory space from Server
9XML Notation
- Begin and end within same tag ltsomething/gt
- Begin and end as two separate tags ltsomethinggt
lt/somethinggt - Document Type Definitionlt?xml version"1.0"
encoding"ISO-8859-1"?gt - lt!DOCTYPE web-app PUBLIC "-//Sun Microsystems,
Inc.//DTD Web Application 2.2//EN""http//java.su
n.com/j2ee/dtds/web-app_2_2.dtd"gt
10Deployment Descriptor web.xml
lt?xml version"1.0" encoding"ISO-8859-1"?gt lt!DOCT
YPE web-app PUBLIC "-//Sun Microsystems,
Inc.//DTD Web Application 2.2//EN""http//java.su
n.com/j2ee/dtds/web-app_2_2.dtd"gt ltweb-appgt ltservl
etgtltservlet-namegt SnoopServlet
lt/servlet-namegtltservlet-classgt test.SnoopServlet
lt/servlet-classgtltload-on-startupgt -454677
lt/load-on-startupgt lt/servletgt ltservlet-mappinggt
ltservlet-namegt SnoopServlet lt/servlet-namegtlturl-p
atterngt SnoopServlet lt/url-patterngt lt/servlet-mapp
inggt ltservlet-mappinggtltservlet-namegt
SnoopServlet lt/servlet-namegtlturl-patterngt
/snoop/ lt/url-patterngt lt/servlet-mappinggt
lttaglibgtlttaglib-urigthttp//jakarta.apache.org/t
aglibs/dbtagslt/taglib-urigtlttaglib-locationgt/WEB-I
NF/dbtags.tldlt/taglib-locationgt lt/taglibgt
11JSP -- Java Server Pages
- Similar to servlets, but they resemble HTML pages
- Placed in web folders mixed with HTML files, acts
like an HTML file - Compiled automatically to a servlet when first
referenced or when the file is changed by
programmer. - Intended to help separate the presentation (HTML
response) from the logic (processing logic). - Similar to Microsoft's Application Server Pages
(ASP)
12JSP Lifecycle
Client
Webserver
Servlet
JSP
13JSP scripting elements
- JSP Commentslt-- jsp comment --gt
- Declarations placed in static part of
servletlt! String filePath"/users"gt - Expressions evaluate to a stringlt
request.getParameter("NAME")gt - Scriptlets any Java statementslt (java
statements) gt - Template Text inserted into responseltTABLE
BORDER1gtltTRgtltTHgtNameltTDgtltINPUT NAME"NAME"
size50 MAXLENGTH50gt
14JSP Default Objects
15JSP Object Scope
16Custom JSP tags
- Assemble reusable JSP code for easy use
- An XML-like front end to access Java programming
- Designed to bridge the intersection between java
programmers and web designers - Tag library holds definitions of custom tagslt_at_
taglib uri"jakarta-taglib/dbtags" prefix"sql"
gt - Custom tags save in Tag Library File .tld
- Many useful Tag Libraries exist for the common
tasks such as database access, email, file access
17DBTags
- From Apache Jakarta project
- Allow use of JDBC with Java details hidden
- Easy to use
- Invisible exception handling
18ltsqlconnectiongt
- establish connection to server
lt-- open a database connection --gt
ltsqlconnection id"conn1"gt lt-- required
--gt ltsqlurlgtjdbcmysql//localhost/testlt/
sqlurlgt lt-- optional --gt
ltsqldrivergtorg.gjt.mm.mysql.Driverlt/sqldrivergt
lt-- optional --gt ltsqluserIdgtrootlt/sqlu
serIdgt lt-- optional --gt
ltsqlpasswordgtnotVerySecurelt/sqlpasswordgt
lt/sqlconnectiongt
19Statement and Query
- The "escapeSql" tag can be used inside a SQL
query to SQL-escape your input values if they
contain single quotes.
lt-- insert a row into the database
--gt ltsqlstatement id"stmt1" conn"conn1"gt
lt-- set the SQL query --gt ltsqlquerygt
insert into test_books (id, name) values (3,
'ltsqlescapeSqlgt ltrequest.getParameter("book_
title")gtlt/sqlescapeSqlgt') lt/sqlquerygt lt--
execute the query --gt ltsqlexecute/gt
lt/sqlstatementgt
20ltsqlresultsetgt
- lttablegt
- ltsqlstatement id"stmt1" conn"conn1"gt
- ltsqlquerygt
- select id, name, description from test_books
order by 1 - lt/sqlquerygt
- lt-- loop through the rows of your query --gt
- ltsqlresultSet id"rset2"gt
- lttrgt lttdgtltsqlgetColumn position"1"/gtlt/tdgt
- lttdgtltsqlgetColumn position"2"/gtlt/tdgt
- lttdgtltsqlgetColumn position"3"/gt
- lt-- print out a comment if the book has no
description --gt - ltsqlwasNullgtno descriptionlt/sqlwasNullgtlt/td
gt - lt/trgt
- lt/sqlresultSetgt
- lt/sqlstatementgt
- lt/tablegt
21JSP Action Elements
- Control Tags for request-time include
- Optional parameters for Control tags
- Allow JSP programs to implement the Controller
aspect of the Model-View-Controller architecture
ltjspinclude page"header.jsp"/gt ltjspforward
page"html.jsp/gt
ltjspforward page"html.jsp"gt ltjspparam
name"page" value"guestview"/gt lt/jspforwardgt
22JSP Forward
Client
Webserver
Servlet 1
JSP
Servlet 2
23Classical Model-View-Controller Architecture
Model Queries
View
Model
Change Notification
User Events
Select a View
Model Changes
Controller
Example A Clock Application
24Problems with JSP
- Although intended to help separate the
presentation logic (VIEW) from the business logic
(MODEL), JSP does not go far enough - JSP programs are often a mix of HTML and business
logic, which creates a problem if the design of
the page (VIEW) needs to be changed - HTML changes need to be made by web designers who
are not necessarily programmers - Solutions to this problem include WebMacro,
Struts, FreeMarker, Velocity
25FreeMarker Template Processing
- Makes substitutions of text in a template based
on a set of identifiers in a table ModelRoot) - Works with any form of text files including HTML,
XML. and Data files
Template Cache
Freemarker
ModelRoot
Resulting web page
26FreeMarker Data Structures
- SimpleHash -- a random access table of
words.Each word is associated with a value which
can be any of the three Freemarker structures - SimpleList -- a sequential access list of any of
the three Freemarker structures - SimpleScalar -- a String of text
- ModelRoot -- the root of the structuremust be a
SimpleHash
27Freemarker RootHash
rootHash
NAME AFF1 AFF2 EMAIL
Joe
Towson U
guestbook
COSC
joe_at_towson
NAME AFF1 AFF2 EMAIL
NAME AFF1 AFF2 EMAIL
28Freemarker Template Scripting
- Value Substitution from rootHashrequest.NAME
- Freemarker Commentsltcommentgt . . . lt/commentgt
- List iterationltloop guestbook as entrygt . . .
lt/listgt - Conditionalltifgt . . . ltelsegt . . . lt/ifgt
- Text inclusion from another template
fileltinclude "footer.html"gt
29FreeMarker Template Cache
- Manages the folder where templates are stored
- Loads and compiles templates into tokenized form
for efficiency - Recognizes changes to templates while application
is running
30Guestbook Java Web Application
html.jsp?pageguestbook
jspforward
action or link
31Guestbook Demo
http//www.mycgiserver.com/fontanini/introfm/html
.jsp?pageguestbook
32I18N
- Internationalization
- Language and Cultural differences
- Dates Times
- Browser sends header Accept-language en-us
- First two characters choose template file
- guestbook_en.html
- guestbook_it.html
- Example
- http//www.mycgiserver.com/fontanini/fm/guestbook
.jsp
33Conclusion
- JSP is Java that looks like HTML
- Custom Tags make programming easy
- Separation of business logic and presentation is
desirable for larger Enterprise projects - JSP alone does not achieve this separation
- EJB, and other technologies are complicated
- Template processing provides the desired
separation, rather easily and elegantly. - Thank you for attending. Any questions?