Tài liệu XML by Example- P10 ppt

50 281 0
Tài liệu XML by Example- P10 ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Listing 12.17: XMLServerConsole.java package com.psol.xcommerce; import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; /** * Simple console to create database and enter data in * the SQL database. * * @version Dec 23, 1999 * @author Benoît Marchal <bmarchal@pineapplesoft.com> */ public class XMLServerConsole extends HttpServlet { /** * handles GET request * @param request HTTP request * @param response hold the response * @exception SerlvetException error handling the request * @exception IOException error writing the reply */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doProcess(request,response); } /** * handles GET request * @param request HTTP request * @param response hold the response * @exception SerlvetException error handling the request 435 The Data Tier continues 14 2429 CH12 2.29.2000 2:25 PM Page 435 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. * @exception IOException error writing the reply */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doProcess(request,response); } /** * GET and POST requests are forwarded here * @param request HTTP request * @param response hold the response * @exception SerlvetException error handling the request * @exception IOException error writing the reply */ protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String sqlDriver = getInitParameter(“sql.driver”), sqlURL = getInitParameter(“sql.url”), sqlUser = getInitParameter(“sql.user”), sqlPassword = getInitParameter(“sql.password”); try { Class.forName(sqlDriver); Connection connection = DriverManager.getConnection(sqlURL, sqlUser, sqlPassword); try { String action = request.getParameter(“action”); if(null != action) { if(action.equalsIgnoreCase(“create”)) doUpdates(request,connection,createStatements); 436 Chapter 12: Putting It All Together: An e-Commerce Example Listing 12.17: continued 14 2429 CH12 2.29.2000 2:25 PM Page 436 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. else if(action.equalsIgnoreCase(“drop”)) doUpdates(request,connection,dropStatements); else if(action.equalsIgnoreCase(“delete”)) doDelete(request,connection); else if(action.equalsIgnoreCase(“insert”)) doInsert(request,connection); } doPage(request,response,connection); } finally { connection.close(); } } catch(Exception e) { throw new ServletException(e); } } /** * drop and create statements to delete/create the database */ private static final String[] dropStatements = { “drop table products”, “drop table orders”, }; private static final String[] createStatements = { “create table products (id integer not null constraint idconstraint pri- mary key,” + “name varchar(50),manufacturer varchar(50),” + “img varchar(30),warranty varchar(20),” + “description varchar(150),price real)”, “create table orders (name varchar(50),” + “street varchar(100),region varchar(50),” + “postal_code varchar(15),locality varchar(50),” + 437 The Data Tier continues 14 2429 CH12 2.29.2000 2:25 PM Page 437 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. “country varchar(25),email varchar(50),” + “productid integer,productname varchar(50),” + “productprice real,productquantity integer)” }; /** * execute a number of updates on the database * (typically to create schema) * @param request HTTP request * @param connection database connection * @param statements statements to execute * @throw SQLException one statement throw an exception */ protected void doUpdates(HttpServletRequest request, Connection connection, String[] statements) throws SQLException { Statement stmt = connection.createStatement(); SQLException e = null; try { for(int i = 0;i < statements.length;i++) try { stmt.executeUpdate(statements[i]); } catch(SQLException x) { e = e != null ? e : x; } if(null != e) { throw e; } } finally { stmt.close(); } } 438 Chapter 12: Putting It All Together: An e-Commerce Example Listing 12.17: continued 14 2429 CH12 2.29.2000 2:25 PM Page 438 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. /** * delete one product from the database * @param request HTTP request * @param connection database connection * @return the form to display (the result screen if you like) */ protected void doDelete(HttpServletRequest request, Connection connection) throws SQLException { PreparedStatement stmt = connection.prepareStatement( “delete from products where id = ?”); try { String id = request.getParameter(“id”); stmt.setInt(1,Integer.parseInt(id)); stmt.executeUpdate(); } finally { stmt.close(); } } /** * create a new product in the database * @param request HTTP request * @param connection database connection * @return the form to display (the result screen if you like) */ protected void doInsert(HttpServletRequest request, Connection connection) throws SQLException, Exception { String id = request.getParameter(“id”), name = request.getParameter(“name”), 439 The Data Tier continues 14 2429 CH12 2.29.2000 2:25 PM Page 439 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. manufacturer = request.getParameter(“manufacturer”), image = request.getParameter(“image”), warranty = request.getParameter(“warranty”), description = request.getParameter(“description”), price = request.getParameter(“price”); PreparedStatement stmt = connection.prepareStatement( “insert into products (id,name,manufacturer,img,” + “warranty,description,price) values(?,?,?,?,?,?,?)”); try { stmt.setString(1,id); stmt.setString(2,name); stmt.setString(3,manufacturer); stmt.setString(4,image); stmt.setString(5,warranty); stmt.setString(6,description); stmt.setString(7,price); stmt.executeUpdate(); } finally { stmt.close(); } } /** * check whether the schema has been created * @return true if the schema is complete, false otherwise */ protected boolean isSchemaCreated(Connection connection) throws SQLException { // ask the name of all the tables in the database // check if one of them is “products” DatabaseMetaData meta = connection.getMetaData(); ResultSet rs = meta.getTables(null,null,null,new String[] { “TABLE” }); 440 Chapter 12: Putting It All Together: An e-Commerce Example Listing 12.17: continued 14 2429 CH12 2.29.2000 2:25 PM Page 440 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. int found = 0; while(rs.next()) { String tableName = rs.getString(“TABLE_NAME”); if(tableName.equalsIgnoreCase(“products”) || tableName.equalsIgnoreCase(“orders”)) found++; } rs.close(); return 2 == found; } /** * display the page, etc. * @param request HTTP request * @param connection database connection */ protected void doPage(HttpServletRequest request, HttpServletResponse response, Connection connection) throws SQLException, IOException { Writer writer = response.getWriter(); writer.write(“<HTML><HEAD><TITLE>XML Server Console” + “</TITLE></HEAD><BODY>”); Statement stmt = connection.createStatement(); try { if(isSchemaCreated(connection)) { writer.write(“<P><FORM ACTION=\””); writer.write(request.getServletPath()); writer.write(“\” METHOD=\”POST\”><TABLE>”); writer.write(“<TR><TD>Identifier:</TD>”); writer.write(“<TD><INPUT TYPE=\”TEXT\””); writer.write(“ NAME=\”id\”></TD></TR>”); 441 The Data Tier continues 14 2429 CH12 2.29.2000 2:25 PM Page 441 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. writer.write(“<TR><TD>Name:</TD>”); writer.write(“<TD><INPUT TYPE=\”TEXT\””); writer.write(“ NAME=\”name\”></TD></TR>”); writer.write(“<TR><TD>Manufacturer:</TD>”); writer.write(“<TD><INPUT TYPE=\”TEXT\””); writer.write(“ NAME=\”manufacturer\”></TD></TR>”); writer.write(“<TR><TD>Image:</TD>”); writer.write(“<TD><INPUT TYPE=\”TEXT\””); writer.write(“ NAME=\”image\”></TD></TR>”); writer.write(“<TR><TD>Warranty:</TD>”); writer.write(“<TD><INPUT TYPE=\”TEXT\””); writer.write(“ NAME=\”warranty\”></TD></TR>”); writer.write(“<TR><TD>Description:</TD>”); writer.write(“<TD><INPUT TYPE=\”TEXT\””); writer.write(“ NAME=\”description\”></TD></TR>”); writer.write(“<TR><TD>Price:</TD>”); writer.write(“<TD><INPUT TYPE=\”TEXT\””); writer.write(“ NAME=\”price\”></TD></TR>”); writer.write(“</TABLE><INPUT TYPE=\”SUBMIT\””); writer.write(“ VALUE=\”Create\”>”); writer.write(“<INPUT TYPE=\”HIDDEN\””); writer.write(“ NAME=\”action\” VALUE=\”insert\”>”); writer.write(“</FORM><P>”); ResultSet rs = stmt.executeQuery(“select id, name from products”); writer.write(“<TABLE>”); while(rs.next()) { writer.write(“<TR><TD>”); writer.write(rs.getString(2)); writer.write(“</TD><TD><FORM ACTION=\””); writer.write(request.getServletPath()); writer.write(“\” METHOD=\”POST\”>”); writer.write(“ <INPUT TYPE=\”SUBMIT\””); writer.write(“ VALUE=\”Delete\”>”); writer.write(“<INPUT TYPE=\”HIDDEN\””); writer.write(“ NAME=\”action\” VALUE=\”delete\”>”); 442 Chapter 12: Putting It All Together: An e-Commerce Example Listing 12.17: continued 14 2429 CH12 2.29.2000 2:25 PM Page 442 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. writer.write(“<INPUT TYPE=\”HIDDEN\””); writer.write(“ NAME=\”id\” VALUE=\””); writer.write(rs.getString(1)); writer.write(“\”>”); writer.write(“</FORM></TD></TR>”); } writer.write(“</TABLE>”); rs = stmt.executeQuery(“select name, “ + “productname from orders”); writer.write(“<TABLE>”); while(rs.next()) { writer.write(“<TR><TD>”); writer.write(rs.getString(1)); writer.write(“</TD><TD>”); writer.write(rs.getString(2)); writer.write(“</TD></TR>”); } writer.write(“</TABLE>”); } writer.write(“<P><FORM ACTION=\””); writer.write(request.getServletPath()); writer.write(“\” METHOD=\”POST\”>”); writer.write(“<INPUT TYPE=\”SUBMIT\””); writer.write(“ VALUE=\”Drop tables\”>”); writer.write(“<INPUT TYPE=\”HIDDEN\””); writer.write(“ NAME=\”action\” VALUE=\”drop\”>”); writer.write(“</FORM>”); writer.write(“<FORM ACTION=\””); writer.write(request.getServletPath()); writer.write(“\” METHOD=\”POST\”>”); writer.write(“<INPUT TYPE=\”SUBMIT\””); writer.write(“ VALUE=\”Create tables\”>”); writer.write(“<INPUT TYPE=\”HIDDEN\””); writer.write(“ NAME=\”action\” VALUE=\”create\”>”); writer.write(“</FORM>”); } finally 443 The Data Tier continues 14 2429 CH12 2.29.2000 2:25 PM Page 443 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. { stmt.close(); } writer.write(“</BODY></HTML>”); writer.flush(); } } Viewer and Editor XMLi is a smaller merchant. It doesn’t have a Web site or a database. XMLi creates its list of products manually with the editor shown in Listings 12.18, 12.19, and 12.20. Listing 12.18 is the Java servlet, Listing 12.19 is the JavaScript file, and Listing 12.20 is the XSL style sheet. You edit a list of products through a URL like http://localhost /editor?merchant=xmli . Listing 12.18: Editor.java package com.psol.xcommerce; import java.io.*; import org.w3c.dom.*; import javax.servlet.*; import javax.servlet.http.*; /** * Editor is a web-tool to create product lists * for smaller merchants. * * @version Sep 10, 1999 * @author Benoît Marchal <bmarchal@pineapplesoft.com> */ public class Editor extends HttpServlet { /** * the editor’s style sheet */ protected Document styleSheet; 444 Chapter 12: Putting It All Together: An e-Commerce Example EXAMPLE Listing 12.17: continued 14 2429 CH12 2.29.2000 2:25 PM Page 444 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... products[product] = null; } } function exportProduct(form) { var xmlCode = “”, merchant = form.merchant.value, attribute = “merchant=’” + merchant + “‘“; var i; for(i = 0;i < products.length;i++) if(products[i] != null) xmlCode += products[i].toXML(); xmlCode = element(“products”,attribute,xmlCode); form.xmldata.value = “< ?xml version=’1.0’?>” + xmlCode; } function resetAll(form,document) { priceList = null;... response.sendError(HttpServletResponse.SC_FORBIDDEN); return; } Writer writer = new FileWriter(fname); writer.write (xml) ; writer.close(); writer = response.getWriter(); writer.write(“Confirmation”); writer.write(“”); writer.write(“Your changes were saved as follow:”); XMLUtil.writeInXML(writer ,xml) ; writer.write(“”); writer.flush(); } } Please purchase PDF Split-Merge... Together: An e-Commerce Example Listing 12.19: continued result += element(“description”,””, escapeXML(this.description)); return element(“product”,attrs,result); } Listing 12.20: editor.xsl < ?xml version=”1.0” encoding=”ISO-8859-1”?> Product... The scalability of XML should not be underestimated The same document works for Emailaholic (which needs integration) as for XMLi (which prefers to view the orders online) What’s Next What’s next? Your turn See where XML makes sense in your environment and start your first application today XML is versatile and flexible, so it’s ideal when starting small... Product(id,name,price,description) { this.id = id; this.name = name; this.price = price; this.description = description; this.toXML = product_toXML; } function product_toXML() { var attrs = “id=’” + this.id + “‘“, result = element(“name”,””,escapeXML(this.name)); result += element(“price”,””,escapeXML(this.price)); Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark continues 14 2429 CH12 450 2.29.2000 2:25... merchant = request.getParameter(“merchant”), fname = getInitParameter(merchant + “ .xml ); if(null == merchant || null == fname) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } String pwdRequest = request.getParameter(“pwd”), pwdCheck = getInitParameter(merchant + “.pwd”), xml = request.getParameter(“xmldata”); if(null != pwdCheck && !pwdCheck.equals(pwdRequest)) { response.sendError(HttpServletResponse.SC_FORBIDDEN);... merchant name in the style sheet When creating a new merchant, you must also create the following file (an empty list of products): < ?xml version=’1.0’?> Listing 12.21 and the accompanying style sheet in Listing 12.22 display the orders for XMLi Listing 12.21: Viewer.java package com.psol.xcommerce; import java.io.*; import org.w3c.dom.*; Please purchase PDF Split-Merge on... Document document = XMLUtil.parse(file.getPath()); XMLUtil.transform(document, Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark continues 14 2429 CH12 454 2.29.2000 2:25 PM Page 454 Chapter 12: Putting It All Together: An e-Commerce Example Listing 12.21: continued styleSheet, response.getWriter(), response.getCharacterEncoding()); } } } Listing 12.22: viewing.xsl < ?xml version=”1.0”... function escapeXML(string) { var result = “”, i, c; for(i = 0;i < string.length;i++) { c = string.charAt(i); if(c == ‘ . != null) xmlCode += products[i].toXML(); xmlCode = element(“products”,attribute,xmlCode); form.xmldata.value = “< ?xml version=’1.0’?>” + xmlCode;. description; this.toXML = product_toXML; } function product_toXML() { var attrs = “id=’” + this.id + “‘“, result = element(“name”,””,escapeXML(this.name));

Ngày đăng: 24/12/2013, 04:17

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan