Title: XML for E-commerce IV
1XML for E-commerce IV
2In this part...
- Some solutions for delivering dynamic content
- Example of using XML
3Solutions for delivering dynamic content
- CGI (e.g. Perl, PHP)
- Java servlets
- Java Server Pages (JSP)
- Extensible Server Pages (XSP)
4CGI
- Server starts external programs using CGI
interface (input, output, requests) - a new process is started and the program code is
loaded each time a request occurs (even when
simultaneous requests) - stateless
- any programming languages Perl, PHP are popular
5Perl CGI package
- Assume name is a form field
!/usr/bin/perl use CGI qv(standard) print
header(), start_html(Hello
param(name)!), h1(Hello param(name)!),
end_html()
6PHP
!/usr/local/bin/php lthtml title lt? echo Hello
name ! ?gt gt ltbodygt lt?php
echo Hello name ! ?gt
lt/bodygt lt/htmlgt
7Java servlets
- Java programs that run on a web server and build
web pages - each request handled by a thread (light)
- only one copy of the code (for simultaneous
requests) - maintaining a state easier
8public class helloWWW extends HttpServlet
public void doGet (HttpServletRequest request,
HttpServletResponse
response) throws ServletException,
IOException response.setContentType(text/
html) printWriter out response.getWriter(
) out.println(lt!DOCTYPE HTML PUBLIC gt \n
lthtmlgt \n
ltheadgtlttitlegtHello
request.getParameter(name) !lt/titlegt
lt/headgt\n ltbodygt\n
lth1gtHello
request.getParameter(name) !lt/ lt/h1gt\n
lt/bodygtlt/htmlgt)
9Java Server Pages (JSP)
- static HTML dynamic parts
- within the code, e.g. lt ...scripting gt
- JSP pages are converted to Java servlets (the
first time the page is requested) and the servlet
is compiled
10lt!DOCTYPE HTML PUBLIC gt lthtmlgt
ltheadgtlttitlegtHello lt
request.getParameter(name) gtlt/titlegt
lt/headgt ltbodygt lth1gtHello lt
request.getParameter(name) gtlt/h1gt
lt/bodygt lt/htmlgt
11Extensible Server Pages (XSP)
- XML pages with embedded logic
- processing can be done before the styling and
presentation is done - therefore the result can easily be used in
further processing (cf. JSP is used to generate
output) - and the logic is separated from presentation
12lt?xml version1.0 ?gt lt?xml-stylesheet
hrefmyStylesheet.xsl
typetext/xsl?gt ltxsppage languagejava
xmlnsxsp
http//www.apache.org/1999/XSP/Coregt ltxsplogic
gt private static int numHits 0 private
synchronized int getNumHits() return
numHits lt/xsplogicgt ltpagegtlttitlegtHit
Counterlt/titlegt ltpgtIve been requested
ltxspexprgtgetNumHits() lt/xspexprgt
times.lt/pgtlt/pagegtlt/xsppagegt
13Example
- The Foobar Public Library
- mytechbooks.com
- customers of mytechbooks.com
14The Foobar Public Library
- suppliers can add new books that they are sending
to the library - an HTML page with a form
ltform methodPOST
actionhttp//www.foo.com/cgi/addBook.plgt
Titleltinput typetext nametitle
size20gt Publisherltinput typetext
namepubl size20gt lt/formgt
15addBooks.pl
- a Perl CGI script reads the data received from
the user and produces XML data - each new entry is appended to an existing file
16Writing an XML file
bookFile /home/foobar/books/books.txt use
CGI query new CGI title
query-gtparam(title) publisher
query-gt(publ) if (open(FILE, gtgt .
bookFile)) print FILE ltbook subject\
. subject . \gt\n print FILE
lttitlegtlt!CDATA . title . gtlt/titlegt\n
print FILE lt/bookgt\n\n
17Notes
- Only an XML fragment is generated no
declaration, no root element
ltbook subjectFictiongt lttitlegtlt!CDATASecond
Foundationgtlt/titlegt ltauthorgtlt!CDATAIsaac
Asimovgtlt/authorgt ltpublishergtlt!CDATABantam
Booksgtlt/publishergt ltnumPagesgt279lt/numPagesgt
ltsaleDetailsgtltisbngt0553293362lt/isbngt
ltpricegt5.59lt/price gtlt/saleDetailsgt
ltdescriptiongtlt!CDATAAfter the First
Foundationgt lt/descriptiongt lt/bookgt
18- Some other portion of the librarys application
periodically reads the XML data and updates the
librarys catalog this part also removes the
entries from the list of new entries - providing a listing of new available books add
an XML declaration etc.
19supplyBooks.pl
bookFile /home/foobar/books/books.txt open(F
ILE, bookFILE) die Couldt open
bookFile. print Content-type
text/plain\n\n print lt?xml version\1.0\?gt\n
print ltbooksgt\n while (ltFILEgt) print
_ print lt/booksgt\n close(FILE)
20mytechbooks.com
- can make HTTP requests (supplyBooks.pl) and
receives a list of books in XML - needs a listing of new technical books on their
own Web page - wants to advertise (push technology)
21Filtering the XML data
- only computer-related books are to be included
(Foobar Library has several other subjects as
well) - subject information is an attribute of the book
element - filter out all the books whose subject is not
Computers
22computerBooks.xsl
ltxslstylesheet xmlnshttp//.
version1.0gt ltxsltemplate matchbooksgt
lthtmlgtltheadgtlt/headgt ltbodygt lt!--
user interface details --gt
ltxslapply-templates
selectbook_at_subjectComputers /gt
lt!-- user interface details --gt
lt/bodygt lt/xsltemplategt lt/xslstylesheetgt
23XSL stylesheet book
ltxsltemplate matchbookgt lttablegt
lttrgtlttdgtltxslvalue-of selecttitle
/gtlt/tdgtlt/trgt lttrgtlttdgt Author
ltxslvalue-of selectauthor /gtltbr /gt
Publisher ltxslvalue-of selectpublisher /gtltbr
/gt Pages ltxslvalue-of selectnumPages
/gtltbr /gt Price ltxslvalue-of
selectsaleDetails/price /gtltbr /gt ltbr
/gt ltxslelement nameagt
ltxslattribute namehrefgt
/servlets/BuyBookServlet?isbn
ltxslvalue-of selectsaleDetails/isbn /gt
lt/xslattributegtlt/xslelementgtlt/tdgtlt/trgtlt/tab
legt lt/xsltemplategt
24XSLT from a servlet
import ... public class ListBooksServlet extends
HttpServlet private static final String
hostnamefoobar.com private static final
int portNumber 80 private static final
String file /cgi/supplyBooks.pl private
static final String stylesheet
/home//computerBooks.xsl public void
service(HttpServletRequest req,
HttpServletResponse res)
throws res.setContentType(text/htm
l) URL getBooksURL new
URL(http,hostname,portNumber,file)
InputStream in getBooksURL.openStream()
// transform XML into HTML
25Notes
- Servlet requests the Foobar Public Librarys
application (supplyBooks.pl) through an HTTP
request, and gets the XML response in an
InputStream - this stream is used as a parameter to the XSLT
processor, as well as the XSL stylesheet defined
as a constant in the servlet
26Invoking transformations
- There is current no general API that specifies
how XSLT transformations can occur
programmatically each processor vendor should
have classes that allow a transformation to be
invoked from Java code
27Invoking transformations
- Apache Xalan XSLTProcessor class in the
org.apache.xalan.xslt package - parameters the XML file to process, the
stylesheet to apply, and output stream (servlet
output stream can be used)
28 URL getBooksURL new URL (http,
hostname, portNumber, file) InputStream in
getBooksURL.openStream() try
XSLTProcessor processor
XSLTProcessorFactory.getProcessor()
processor.process(new XSLTInputSource(in),
new XSLTInputSource
( new
FileInputStream(stylesheet)),
new XSLTResultTarget(
res.getOutputStream()))
catch (Exception e)
29Push vs. pull
- Pull the user writes an URL or follows a link,
or an application makes an HTTP request - Push data is delivered to the user without the
users active request e.g. which new items are
available, special offers...
30Personalized start pages and RSS
- Personalized start pages e.g. Netscapes My
Netscape and Yahoos My Yahoo - Rich Site Summary (RSS) an XML application,
which defines channels - a channel provides quick info about e.g. a
companys products (for display in a portal-style
framework)
31RSS channel
- title of the channel
- description of the channel
- image or logo
- items, each item may contain title, description
and hyperlink
32 in our example
- mytechbooks.com would like to create an RSS
channel that provides new book listings
interested clients can jump directly to buying an
item - a channel is registered with Netscape Netcenter
- Netcenter will automatically update RSS channel
content
33lt?xml version1.0gt lt!DOCTYPE rss PUBLIC
-//Netscape Communications//DTD RSS 0.91//EN
http//my.netscape.com/publish/formats/rss-0.9
1.dtdgt ltrss version0.91gt ltchannelgt
lttitlegtmytechbooks.com New Listingslt/titlegt
ltlinkgthttp//www.newInstance.com/javaxml/techbooks
lt/linkgt ltdescriptiongtYour online
source for technical material, computers, and
computing books!lt/descriptiongt
ltlanguagegten-uslt/languagegt
ltimagegtlt/imagegt ltitemgtlt/itemgt
ltitemgtlt/itemgt lt/channelgt lt/rssgt
34ltitemgt lttitlegtJava Servlet
Programminglt/titlegt ltlinkgt
http//mytechbooks.com/buy.xsp?isbn156592391X
lt/linkgt ltdescriptiongt
This book is a superb introduction to Java
servlets and their various
communications mechanisms.
lt/descriptiongt lt/itemgt
35Transformation into RSS
- an XSLT transformation can transform the book
elements to item elements - book -gt item
- title -gt title
- saleDetails/isbn -gt link (isbn as param.)
- description -gt description
36Validating the RSS channel
- http//my.netscape.com/publish/help/
validate.tmpl - there are some constraints that cannot be
enforced by the DTD - RSS channel can be a servlet, CGI script, a
static file...
37Registering the channel
- The channel is published to Netcenter (or other
service provider) - http//my.netscape.com/publish
- once the valid channel is accepted, instructions
how to add the channel to a web page is sent by
email
38Use
- button or link is added to the mytechbooks.com
web page Add this site to My Netscape - inclusion in the Netscape Open Directory
39Not restricted to Netscape
- The same RSS format can (and is) used in
different applications - format is simple and generic suitable for many
purposes