Tài liệu XML, XSLT, Java, and JSP: A Case Study in Developing a Web Application- P11 ppt

50 527 0
Tài liệu XML, XSLT, Java, and JSP: A Case Study in Developing a Web Application- P11 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

482 Appendix C Source Code for bonForum Web Application C.15 Filename: Projects\bonForum\src\ BonForumRobot.java /*<Imports>*/ import java.io.*; import java.net.*; import java.util.*; import java.applet.*; import java.awt.Font; import java.awt.Color; import java.awt.Graphics; import java.awt.Component; // temp /*</Imports>*/ /** BonForumRobot repeatedly invokes showDocument method of applet. * It can be used from a frame display to request a different frameset. * It can also be used to continually refresh one frame from another. * The applet parameters are: * target, document, increment, limit, message and refresh. * <p>For further information visit the open source * <A HREF=”http://www.bonForum.org”>BonForum Project on SourceForge</A> * @author <A HREF=”mailto://email@bonforum.org”>Westy Rockwell</A> */ public class BonForumRobot extends Applet { URL codeBase = null; String document = “”; String target = “”; String messageLineOne = “”; String messageLineTwo = “”; String message = “”; boolean refresh = false; boolean continueRunning = true; int increment = 0; int limit = 0; int counter = 0; Font font = new Font(“TimesRoman”, Font.ITALIC,24); public void init() { System.out.println(“init()”); // get other plugin parameters target = getParameter(“target”, “_self”); document = getParameter(“document”, “”); increment = getParameter(“increment”, 20000); limit = getParameter(“limit”, 10000); message = getParameter(“message”, “BonForumRobot applet”); refresh = getParameter(“refresh”, false); // see these debugging messages on the Java Console codeBase = this.getCodeBase(); System.out.println(“documentBase:” + this.getDocumentBase().toString()); System.out.println(“codeBase:” + codeBase.toString()); 15 1089-9 XC 6/26/01 7:40 AM Page 482 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 483 C.15 Filename: Projects\bonForum\src\BonForumRobot.java System.out.println(“refresh:” + this.refresh); System.out.println(“target:” + this.target); System.out.println(“document:” + this.document); System.out.println(“increment:” + this.increment); System.out.println(“limit:” + this.limit); System.out.println(“message:” + this.message); // forces application global error displays not to be in a frame: if(document.indexOf(“forum_error”) > -1) { if(!target.equals(“_top”)) { target = “_top”; System.out.println(“changed to forum_error target:” + this.target); } } } public void start() { // kick off thread to do the dirty work setBackground(Color.cyan); System.out.println(“start()”); if (refresh) { RefreshThread thread = new RefreshThread(Long.toString(System.currentTimeMillis()), codeBase); thread.start(); } } public void stopRunning() { stop(); } public void stop() { System.out.println(“stop()”); continueRunning = false; } public void paint(Graphics graphics) { graphics.setFont(font); graphics.setColor(Color.black); if(message.equalsIgnoreCase(“debug”)) { graphics.drawString(messageLineOne,10,20); graphics.drawString(messageLineTwo,10,40); graphics.drawString(target,10,60); graphics.drawString(document,10,80); graphics.drawString(new Boolean(refresh).toString(), 10, 100); graphics.drawString(Integer.toString(increment), 10, 120); graphics.drawString(Integer.toString(limit), 10, 140); } else { graphics.drawString(messageLineOne,10,20); graphics.drawString(messageLineTwo,10,40); } 15 1089-9 XC 6/26/01 7:40 AM Page 483 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 484 Appendix C Source Code for bonForum Web Application } private String getParameter(String name, String defaultValue){ String retval = getParameter(name); if (retval == null || retval.trim() == “”){ retval = defaultValue; } return retval; } private int getParameter(String name, int defaultValue){ int retval = defaultValue; String tmp = getParameter(name); if (tmp != null && tmp.trim() != “”){ try { retval = Integer.parseInt(tmp); } catch (NumberFormatException nfe) { // don’t do anything. // it’s still assigned to the defaultValue! } } return retval; } private boolean getParameter(String name, boolean defaultValue){ boolean retval = defaultValue; String tmp = getParameter(name); if (tmp != null){ if (tmp.equalsIgnoreCase(“true”)) retval = true; if (tmp.equals(“1”)) retval = true; if (tmp.equalsIgnoreCase(“false”)) retval = false; if (tmp.equals(“0”)) retval = false; } return retval; } public class RefreshThread extends Thread { private URL codeBase; public RefreshThread(String s, URL cb){ super(s); codeBase = cb; } public void run() { String uncachedDocument = “”; String errorDocument = codeBase.toString() + “ /forum_error.jsp”; messageLineOne = “”; messageLineTwo = “”; // These are two behaviors that depend on the “target” parameter: // // 1. target = “_top” // // This means we are using the applet to break out of a frameset on the browser. // // 2. target <> “_top” 15 1089-9 XC 6/26/01 7:40 AM Page 484 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 485 C.15 Filename: Projects\bonForum\src\BonForumRobot.java // // This means we are using the applet to periodically refresh a document within a frame // // // The code below here makes this applet repeat an action (showDocument) // roughly every increment seconds, with a maximum controlled by the limit parameter. // However, note that if target is “_top” no repetition of showDocument occurs. counter = 1; while (continueRunning) { // put it to sleep for “increment” milliseconds messageLineOne = “”; getAppletContext().showStatus(“bonForumRobot”); repaint(); try { sleep(3*(increment/4)); } catch (InterruptedException e) {continue;} // put it back to sleep for a “yellow light” messageLineOne = “refreshing .”; repaint(); try { sleep(increment/4); } catch (InterruptedException e) {continue;} // are all iterations done? if(counter > limit) { System.out.println(“counter:” + counter + “ over limit:” + limit); stopRunning(); continue; } // no, do it counter++; String millis = Long.toString(System.currentTimeMillis()); // LATER: we will somehow refresh content only if stale if( (document.indexOf(“forum_error”) > -1) || (document.indexOf(“forum_login”) > -1) || (document.indexOf(“visitor_joins_chat”) > -1) || (document.indexOf(“visitor_starts_chat”) > -1) || (document.indexOf(“guest_executes_chat”) > -1) || (document.indexOf(“guest_exits_chat”) > -1) || (document.indexOf(“guest_executes_command”) > -1) || (document.indexOf(“guest_exits_command”) > -1) || (document.indexOf(“host_executes_chat”) > -1) || (document.indexOf(“host_exits_chat”) > -1) || (document.indexOf(“host_executes_command”) > -1) || (document.indexOf(“host_exits_command”) > -1)) { // // We fixup the filename as a unique filename to prevent browser from // retrieving the last result for a jsp uri request from 15 1089-9 XC 6/26/01 7:40 AM Page 485 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 486 Appendix C Source Code for bonForum Web Application its cache. // // For example, // “http://localhost:8080/bonForum/jsp/forum/visitor_joins_chat.jsp#entry47” // will become something like this: // “http://localhost:8080/bonForum/jsp/forum/visitor_joins_chat.jsp#entry47.962066767 851.tfe” // // The “millis” value added will create a unique filename, so the browser will fetch the jsp // // The “.tfe” at the end of the uncached URL will map to the BonForumEngine servlet. // // One drawback is that although the browser will look for none of these unique names in the cache, // it will nevertheless cache them, and cache them, and cache them! Filling the cache with them! // uncachedDocument = document + millis + “.tfe”; System.out.println(“Created name for uncachedDocument:” + uncachedDocument); } else { // NOTE: applet code must be in subfolder (e.g., “applet”) of folder with forum_login.jsp // so this creates something like “http://freedom:8080/bonForum/jsp/forum/forum_login.jsp” uncachedDocument = errorDocument + millis + “.tfe”; System.out.println(“Document not in list, using an error document:” + uncachedDocument); target = “_top”; message = “Unknown destination, new login required!”; } if(target.equals(“_top”)) { stopRunning(); // after this loop getAppletContext().showStatus(message); } messageLineOne = “loading .”; messageLineTwo = message; repaint(); try { getAppletContext().showDocument(new URL(uncachedDocument), target); } catch(MalformedURLException ee) { System.out.println(“MalformedURLException:” + ee.getMessage()); System.out.println(“MalformedURLException, uncachedDocument:” + uncachedDocument); 15 1089-9 XC 6/26/01 7:40 AM Page 486 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 487 C.16 Filename: Projects\bonForum\src\de\tarent\forum\BonForumEngine.java document = “”; // force errorDocument next time } messageLineTwo = “”; } } } //End of Inner Class } C.16 Filename: Projects\bonForum\src\de\ tarent\forum\BonForumEngine.java package de.tarent.forum; /*<Imports>*/ import java.io.*; import java.util.Hashtable; import javax.servlet.*; import javax.servlet.http.*; /*</Imports>*/ /** BonForumEngine is the central servlet of bonForum web application. * At present, it implements a chat. Its purpose is experimentation. * It is described fully in the book: * <i>XML, XSLT, Java and JSP - A Case Study in Developing a Web Application</i>, * by Westy Rockwell, published by <A HREF=”http://www.newriders.com”>New Riders</A>. * Translation to German published by <A HREF=”http://www.galileocomputing.de”>Galileo Press</A>. * <p>For further information visit the open source * <A HREF=”http://www.bonForum.org”>BonForum Project on SourceForge</A> * @author <A HREF=”mailto://email@bonforum.org”>Westy Rockwell</A> */ public class BonForumEngine extends HttpServlet { // holds and gives access to the data for the forum private static BonForumStore bonForumStore = new BonForumStore(); // ensures nicknames are unique private static Hashtable nicknameRegistry = new Hashtable(); // logs debugging information private static BonLogger logBFE = null; // controls logger output private static String logging = null; // false until logger ready private static boolean loggingInitialized = false; /** Initializes a BonForumEngine instance. * Also sets its logging value from application init param. * Also creates its logger if not done before. * */ public void init() throws ServletException { System.err.println(“ENTERING BonForumEngine init”); if(!loggingInitialized) { 15 1089-9 XC 6/26/01 7:40 AM Page 487 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 488 Appendix C Source Code for bonForum Web Application System.err.println(“BonForumEngine init loggingInitialized:” + loggingInitialized); logging = getServletContext().getInitParameter(“Logging”); System.err.println(“BonForumEngine init logging:” + logging); if(logging != null) { logBFE = new BonLogger(“BonForumEngineLog.txt”, logging); System.err.println(“BonForumEngine init logBFE:” + logBFE); loggingInitialized = true; System.err.println(“BonForumEngine init loggingInitialized:” + loggingInitialized); getBonForumStore().setLogging(logging); System.err.println(“BonForumEngine init getBonForumStore().setLogging(logging)”); } } System.err.println(“LEAVING BonForumEngine init”); } /** Gets the BonForumStore from this BonForumEngine. * * @return BonForumStore */ public BonForumStore getBonForumStore() { return bonForumStore; } private void log(String sessionId, String where, String what) { if(logging != null) { logBFE.logWrite(System.currentTimeMillis(), sessionId, where, what); } } /** Processes requests in context of web application rules. * Called from BonForumEngine service method. * Customizes the HttpServlet based engine as a web application * (a chat in this case). * * @param request HttpServletRequest argument from service method * @param response HttpServletResponse argument from service method * @param session HttpSession current * @param bonForumCommand String routes request to next destination * @return String bonForumCommand parameter, maybe changed by this method, maybe not. * * @throws IOException */ protected String processRequest(HttpServletRequest request, HttpServletResponse response, HttpSession session, String bonForumCommand) throws IOException { BonNode chatNode = null; 15 1089-9 XC 6/26/01 7:40 AM Page 488 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 489 C.16 Filename: Projects\bonForum\src\de\tarent\forum\BonForumEngine.java Object obj = null; String actorNickname = “”; String actorAge = “”; String xalanVersion = “”; String sessionMaxInactiveMinutes = “”; String actorRatingType = “”; String chatModerated = “”; String chatTopic = “”; String chatSubject = “”; String chatItem = “”; String chatGuest = “”; String chatMessage = “”; String chatMessagesNavigator = “”; String chatMessagesPageSize = “”; String nameAndAttributes = “”; String content = “”; String forestHashtableName = “”; String chatNodeKeyKey = “”; request.setAttribute(“serviceStatus”, “InProcessRequestMethod”); String sessionId = session.getId(); // using sessionId for now, later it will be // replaced with userId, when user manager is implemented. bonForumStore.initialize(sessionId); // See if bonForumStore instance is bound to this ServletContext // If not, bind bonForumStore so it can be found elsewhere in web app // Then, you can use code like the following from other classes, to // have access to the methods of the bonForumStore: // // BonForumStore bonForumStore = // (bonForumStore)application.getAttribute(“bonForumStore”); // // Or, from a JSP page: // // if (pageContext.getServletContext().getAttribute(“bonForumStore”) != null) { // BonForumStore bFS = // (bonForumStore)(pageContext.getServletContext().getAttribute( // “bonForumStore”)); // } // // Of course, to use properties, it is simpler to do this: // // <jsp:useBean id=”bonForumStore” // class=”de.tarent.forum.BonForumStore” // scope=”application”/> // Object temp = getServletContext().getAttribute(“bonForumStore”); if (temp == null) { getServletContext().setAttribute(“bonForumStore”, getBonForumStore()); 15 1089-9 XC 6/26/01 7:40 AM Page 489 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 490 Appendix C Source Code for bonForum Web Application } // bonForumCommand selects the next state of the application // Its value will be used to create a JSP filename. // If it has no value here, construct one from bonCommand // request parameter, or else, from other request parameters. bonForumCommand = normalize(bonForumCommand).trim(); if(bonForumCommand.length() < 1) { // As a second alternative the engine uses // the bonCommand request parameter // to tell where to forward the request String bonCommand = normalize((String)request.getParameter(“bonCommand”)).trim(); if(bonCommand.length() > 0) { bonForumCommand = bonCommand; } else { // As a third alternative, the engine can use any combination // of one to three other parameters (actor, action, thing) // to construct the bonForumCommand described above String actorStatus = normalize((String)request.getParameter(“actorStatus”)).trim(); String actionStatus = normalize((String)request.getParameter(“actionStatus”)).trim(); String thingStatus = normalize((String)request.getParameter(“thingStatus”)).trim(); if((actorStatus.length() > 0) || (actionStatus.length() > 0) || (thingStatus.length() > 0)) { bonForumCommand = actorStatus + “_” + actionStatus + “_” + thingStatus; // later, trim off leading and trailing underscore chars, if any } else { bonForumCommand = “forum_error”; request.setAttribute(“serviceStatus”, “ForwardToErrorPage”); log(sessionId, “err”, “No bonForumCommand! Forwarding To Error Page!”); } } } // used for debugging only: session.setAttribute(“bonForumCommand”, bonForumCommand); // // NOTES: ADDING ELEMENTS to bonForumXML ForestHashtable using BonForumStore // // The BonForumStore.add() method automatically finds parent nodeKey three ways: // 15 1089-9 XC 6/26/01 7:40 AM Page 490 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 491 C.16 Filename: Projects\bonForum\src\de\tarent\forum\BonForumEngine.java // 1. If the parent is one of the intrinsic system elements then that // element’s name (“bonForum”, “”actors”, “actions”, “things”, “system”, etc.) // is the key in the nodeNameHashtable for the parent nodeKey. // // 2. If the parent is not one of the intrinsic system elements // (for example, a “message” element inside the “things” element) // then the key in the nodeKeyHashtable is made up of the following: // <sessionId> + “_” + <nodeKey.aKey> + “:” <elementName>. // (for example: “54w5d31sq1_985472754824:message”) // NOTE: there is also an option to leave out the nodeKey.aKey portion of // the key, for a selected list of node names (see ForestHashtable, property // UniqueNodeKeyKeyList. That reduces the size requirements of the // nodeKeyHashtable (for example, by not storing all the message nodeKey keys). // // 3. If the parent is one of certain elements that are loaded into // the bonForumXML (like the “subjects” subtree loaded with the // loadForumXML command, for example), then the nodeKey is in the // corresponding hashtable for that upload. // (For example, subjects elements are in the pathNameHashtable with // a key made from the path from root to the node whose nodeKey is saved. // An example of a subject key is “bonForum.things.subjects.Vehicles.Motorcycles”) // NOTE: EACH add() method call also returns an object // that can be cast to a NodeKey // // 1) YOU CAN USE THESE TO RE-CREATE THE nodeKeyHashtable nodeKey key. // // 2) YOU CAN ALSO USE THESE TO RECALL ELEMENTS BY NodeKey. // // With the cast returned object, you can keep a reference to any // nodeKey around. For the message element just added, we could keep // its nodeKey as follows: // // NodeKey messageNodeKey = (NodeKey)obj; // // Then, using the aKey member of the NodeKey, together with the sessionId // and the name of the node, you can recreate the key for the NodeKey in the // nodeKeyHashTable, and use it to add children to the node. For an example, // see below how we add hostKey to chat. That example uses two different // cast returned objects, one to get the hostNodeKey as a String, // the other to get the chatNodeKeyKey for adding a node (hostKey) with that // String as content to the chat node. That same hostKey value will be 15 1089-9 XC 6/26/01 7:40 AM Page 491 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... attributes // Choices can be offered for behavior of “visitor starts chat” when chat exists // 1 always warn user and ask again for new subject and/ or new topic // 2 if actor was in it, always join with previous status, else warn and ask again // 3 if actor was in it, always join as guest, else warn and ask again // All these choices can be modified re actorRestartingCurrentChat value // For now, we implement... chatMessagesNavigator chatMessagesNavigator = normalize((String)request.getParameter(“chatMessagesNavigator”)); if(chatMessagesNavigator.trim().length() > 0) { session.setAttribute(“chatMessagesNavigator”, chatMessagesNavigator); } // handle chatMessage // If we have a message save it as child of things, // and the messageKey to the chat element chatMessage = normalize((String)request.getParameter(“chatMessage”));... security and sessions Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 15 1089-9 XC 6/26/01 7:40 AM Page 512 512 Appendix C Source Code for bonForum Web Application * (See chapter “Java Servlet and Java Bean - BonForumEngine and BonForumStore” * in the book: XML, XSLT, Java and JSP - A Case Study in Developing a Web Application) * * @param request HttpServletRequest * @param response... nameAndAttributes = “sessionID_”; nameAndAttributes += sessionId; nameAndAttributes += “_”; nameAndAttributes += creationTimeMillis; nameAndAttributes += “ chatTopic=\””; nameAndAttributes += chatTopic; nameAndAttributes += “\””; content = “”; forestHashtableName = “bonForumXML”; obj = bonForumStore.add( “bonAddElement”, chatSubject, nameAndAttributes, content, forestHashtableName, “pathNameHashtable”, sessionId );... NOTICE above, in comment when adding actorNickname to host // add actorAge to guest nameAndAttributes = “actorAge”; content = normalize((String)session.getAttribute(“actorAge”)); forestHashtableName = “bonForumXML”; obj = bonForumStore.add(“bonAddElement”, guestNodeKeyKey, nameAndAttributes, content, forestHashtableName, “nodeNameHashtable”, sessionId); // add actorRating to guest nameAndAttributes = “actorRating”;... session has no itemKey!”); } // NOTE: make sure attribute name=value items are separated by a space // and that there are no spaces next to the “=” between name and value // You can have quotes inside the value string, but only using \” to escape them // to optimize speed, attributes ordered by frequency of access using getAttributeValue() nameAndAttributes = “message”; nameAndAttributes = nameAndAttributes... sessionMaxInactiveMinutes); } } else if(bonForumCommand.indexOf(“system_executes_command”) > -1) { //xalanVersion “Xalan-Java 1” or “Xalan-Java 2” (set in application attribute!) xalanVersion = normalize((String)request.getParameter(“xalanVersion”)); if(xalanVersion.trim().length() > 0) { getServletContext().setAttribute(“xalanVersion”, xalanVersion); } //actorRatingType will set differenct options for actor... normalize((String)request.getParameter(“actorReturning”)); if(actorReturning.trim().length() > 0) { session.setAttribute(“actorReturning”, actorReturning); } else { session.setAttribute(“actorReturning”, “”); } // Incoming request parameters are used as “bonForum variables” // (including actor data, GUI settings, chat messages, etc.) // The parameter values are processed and/ or set in attributes // Most are set in session attributes, but at least... “bonAddElement”, chatNodeKeyKey, nameAndAttributes, content, forestHashtableName, “nodeNameHashtable”, sessionId ); // Make the hostKey available to this session // It is later used for these things: // 1 finding out if an actor is a host in a chat // 2 branding messages with a host as sender session.setAttribute(“hostKey”, content); // Make nodeNameHashtable key // for the chat node key // available to session... this watermark 15 1089-9 XC 6/26/01 7:40 AM Page 514 514 Appendix C Source Code for bonForum Web Application minutes = Integer.parseInt(sessMax); } catch (NumberFormatException nFE) { log(sessionId, “err”, “ERROR! service(), cannot parse maxInactiveInterval app attr as int: “ + sessMax); minutes = -1; } session.setMaxInactiveInterval(minutes); // default to forever //session.setMaxInactiveInterval(30); . ask again // 3. if actor was in it, always join as guest, else warn and ask again // All these choices can be modified re actorRestartingCurrentChat value. chat exists // 1. always warn user and ask again for new subject and/ or new topic // 2 if actor was in it, always join with previous status, else warn and

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

Từ khóa liên quan

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

Tài liệu liên quan