Download presentation
Presentation is loading. Please wait.
Published byCaroline Linda Hines Modified over 9 years ago
1
XML for E-commerce IV Helena Ahonen-Myka
2
In this part... n Some solutions for delivering dynamic content n Example of using XML
3
Solutions for delivering dynamic content n CGI (e.g. Perl, PHP) n Java servlets n Java Server Pages (JSP) n Extensible Server Pages (XSP)
4
CGI n Server starts external programs using CGI interface (input, output, requests) n a new process is started and the program code is loaded each time a request occurs (even when simultaneous requests) n stateless n any programming languages: Perl, PHP are popular
5
Perl CGI package #!/usr/bin/perl use CGI qv(:standard); print header(), start_html(”Hello param($name)!”), h1(”Hello param($name)!”), end_html(); n Assume: name is a form field:
6
PHP #!/usr/local/bin/php > <?php echo ”Hello $name !”; ?>
7
Java servlets n Java programs that run on a web server and build web pages n each request handled by a thread (light) n only one copy of the code (for simultaneous requests) n maintaining a state easier
8
public class helloWWW extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(”text/html”); printWriter out = response.getWriter(); out.println(” \n” + ” \n” + ” Hello” + request.getParameter(”name”) + ”! ” + ” \n” + ” Hello” + request.getParameter(”name”) + ”! \n” + ” ”); }}
9
Java Server Pages (JSP) n static HTML + dynamic parts n within the code, e.g. n JSP pages are converted to Java servlets (the first time the page is requested) and the servlet is compiled
10
Hello <% request.getParameter(”name”) %> Hello <% request.getParameter(”name”) %>
11
Extensible Server Pages (XSP) n XML pages with embedded logic n processing can be done before the styling and presentation is done n therefore the result can easily be used in further processing (cf. JSP is used to generate output) n and the logic is separated from presentation
12
<?xml-stylesheet href=”myStylesheet.xsl” type=”text/xsl”?> <xsp:page language=”java” xmlns:xsp= ”http://www.apache.org/1999/XSP/Core”> private static int numHits = 0; private synchronized int getNumHits() { return ++numHits;} Hit Counter I’ve been requested getNumHits() times.
13
Example n The Foobar Public Library n mytechbooks.com n customers of mytechbooks.com
14
The Foobar Public Library n suppliers can add new books that they are sending to the library n an HTML page with a form: <form method=”POST” action=”http://www.foo.com/cgi/addBook.pl”> … Title: Publisher: …
15
addBooks.pl n a Perl CGI script: reads the data received from the user and produces XML data n each new entry is appended to an existing file
16
Writing an XML file $bookFile = ”/home/foobar/books/books.txt”; use CGI; $query = new CGI; $title = $query->param(’title’); $publisher = $query->(’publ’); … if (open(FILE, ”>>”. $bookFile)) { print FILE ” \n”; print FILE ” \n; … print FILE ” \n\n”;
17
Notes: n Only an XML fragment is generated: no declaration, no root element 279 0553293362 5.59
18
n Some other portion of the library’s application periodically reads the XML data and updates the library’s catalog: this part also removes the entries from the list of new entries n providing a listing of new available books: add an XML declaration etc.
19
supplyBooks.pl $bookFile = ”/home/foobar/books/books.txt” open(FILE, $bookFILE) || die ”Could’t open $bookFile.”; print ”Content-type: text/plain\n\n”; print ” \n”; while ( ) { print ”$_”; } print ” \n”; close(FILE);
20
mytechbooks.com n can make HTTP requests (supplyBooks.pl) and receives a list of books in XML n needs a listing of new technical books on their own Web page n wants to advertise (push technology)
21
Filtering the XML data n only computer-related books are to be included (Foobar Library has several other subjects as well) n subject information is an attribute of the book element n filter out all the books whose subject is not ”Computers”
22
computerBooks.xsl … <xsl:apply-templates select=”book[@subject=’Computers’]” />
23
XSL stylesheet: book Author: Publisher: Pages: Price: /servlets/BuyBookServlet?isbn=
24
XSLT from a servlet import... public class ListBooksServlet extends HttpServlet { private static final String hostname=”foobar.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/html”); URL getBooksURL = new URL(”http”,hostname,portNumber,file); InputStream in = getBooksURL.openStream(); // transform XML into HTML }}
25
Notes: n Servlet requests the Foobar Public Library’s application (supplyBooks.pl) through an HTTP request, and gets the XML response in an InputStream n this stream is used as a parameter to the XSLT processor, as well as the XSL stylesheet defined as a constant in the servlet
26
Invoking transformations n 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
27
Invoking transformations n Apache Xalan: XSLTProcessor class in the org.apache.xalan.xslt package n 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) {…} }
29
Push vs. pull n Pull: the user writes an URL or follows a link, or an application makes an HTTP request n Push: data is delivered to the user without the user’s active request; e.g. which new items are available, special offers...
30
Personalized start pages and RSS n Personalized start pages: e.g. Netscape’s My Netscape and Yahoo’s My Yahoo n Rich Site Summary (RSS): an XML application, which defines channels n a channel provides quick info about e.g. a company’s products (for display in a portal-style framework)
31
RSS channel n title of the channel n description of the channel n image or logo n items, each item may contain title, description and hyperlink
32
… in our example n mytechbooks.com would like to create an RSS channel that provides new book listings: interested clients can jump directly to buying an item n a channel is registered with Netscape Netcenter n Netcenter will automatically update RSS channel content
33
<!DOCTYPE rss PUBLIC ”-//Netscape Communications//DTD RSS 0.91//EN” ”http://my.netscape.com/publish/formats/rss-0.91.dtd”> mytechbooks.com New Listings http://www.newInstance.com/javaxml/techbooks Your online source for technical material, computers, and computing books! en-us …
34
Java Servlet Programming http://mytechbooks.com/buy.xsp?isbn=156592391X This book is a superb introduction to Java servlets and their various communications mechanisms.
35
Transformation into RSS n an XSLT transformation can transform the book elements to item elements: n book -> item n title -> title n saleDetails/isbn -> link (isbn as param.) n description -> description
36
Validating the RSS channel n http://my.netscape.com/publish/help/ validate.tmpl n there are some constraints that cannot be enforced by the DTD n RSS channel can be a servlet, CGI script, a static file...
37
Registering the channel n The channel is published to Netcenter (or other service provider) n http://my.netscape.com/publish n once the valid channel is accepted, instructions how to add the channel to a web page is sent by email
38
Use n button or link is added to the mytechbooks.com web page: ”Add this site to My Netscape” n inclusion in the Netscape Open Directory
39
Not restricted to Netscape n The same RSS format can (and is) used in different applications n format is simple and generic: suitable for many purposes
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.