Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2001 Prentice Hall, Inc. All rights reserved. Chapter 15 – Case Study: Message Forum with Active Server Pages Outline 15.1Introduction 15.2Setup and.

Similar presentations


Presentation on theme: " 2001 Prentice Hall, Inc. All rights reserved. Chapter 15 – Case Study: Message Forum with Active Server Pages Outline 15.1Introduction 15.2Setup and."— Presentation transcript:

1  2001 Prentice Hall, Inc. All rights reserved. Chapter 15 – Case Study: Message Forum with Active Server Pages Outline 15.1Introduction 15.2Setup and Message Forum Documents 15.3Forum Navigation 15.4Adding Forums 15.5Forum XML Documents 15.6Posting Messages 15.7Other HTML Documents

2  2001 Prentice Hall, Inc. All rights reserved. 15.1 Introduction Creating a Web-based message forum with XML –Virtual bulletin boards –Use Microsoft’s Active Server Pages (ASP) technology

3  2001 Prentice Hall, Inc. All rights reserved. 15.2 Setup and Message Forum Documents Setup instructions to execute case study Software requirements –Web server software Microsoft Personal Web Server (PWS) OR Microsoft Internet Information Services (IIS) OR Microsoft Internet Information Server (IIS) –Internet Explorer 5.5+ (for XML and XSLT processing) Place files in virtual Web directory –c:\inetpub\wwwroot

4  2001 Prentice Hall, Inc. All rights reserved. 15.2 Setup and Message Forum Documents

5  2001 Prentice Hall, Inc. All rights reserved. 15.2 Setup and Message Forum Documents default.asp –Displays list of available message forums stored in XML document forums.xml –Contains hyperlinks to each XML message forum document and to addForum.asp addForum.asp –Adds forums to document forums.xml –Creates new XML message forum using forum template template.xml formatting.xsl –Transforms each XML message forum document into HTML

6  2001 Prentice Hall, Inc. All rights reserved. 15.2 Setup and Message Forum Documents site.css –CSS document formats HTML for display addPost.asp –posts new messages to forum invalid.html –Displays when errors occur during processing

7  2001 Prentice Hall, Inc. All rights reserved. 15.3 Forum Navigation Fig. 15.2Key interactions between message forum documents.

8  2001 Prentice Hall, Inc. All rights reserved. Outline 1 2 3 4 5 6 7 8 Forum 1 Name 9 10 11 forums.xml XML document that marks up the message forums. Root element forums can hold any number of message forums Stores name of XML document that contains forum’s markup name element marks up name of forum (used as a hyperlink descriptor in default.asp )

9  2001 Prentice Hall, Inc. All rights reserved. Outline 1 2 3 4 5 6 9 10 11 12 13 Deitel Message Forums 14 15 16 17 18 Deitel Message Forums 19 Available Forums 20 21 <% 22 Dim xmlFile, xmlNodes, xmlItem 23 Dim strPath, strTitle, strFileName 24 25 strPath = Server.MapPath( "forums.xml" ) 26 27 Set xmlFile = Server.CreateObject( "Microsoft.XMLDOM" ) 28 xmlFile.Async = False 29 30 If Not xmlFile.Load( strPath ) Then 31 Call Response.Redirect( "invalid.html" ) 32 End If 33 34 Set xmlNodes = xmlFile.DocumentElement.ChildNodes 35 default.asp Message forums main page. Sets scripting language to VBScript All variables must be declared explicitly Gets absolute path for file forums.xml and stores it in variable strPath CreateObject method instantiates a DOMDocument object ( Microsoft.XMLDOM ), assigns object to xmlFile Sets object referenced by xmlFile to behave synchronously Calls method Load to parse the XML document. Returns true if parsing succeeds, otherwise returns false DocumentElement property retrieves root element’s child nodes

10  2001 Prentice Hall, Inc. All rights reserved. Outline 36 For Each xmlItem In xmlNodes 37 strFileName = xmlItem.getAttribute( "filename" ) 38 strTitle = xmlItem.text 39 %> 40 41 "> 42 43 46 47 48 Forum Management 49 50 51 Add a Forum 52 Delete a Forum 53 54 55 56 57 default.asp Message forums main page. For Each loop iterates through all nodes in child nodes collection. getAttribute method returns value of node’s filename attribute and assigns it to strFileName text property returns node’s text content (forum’s name) Creates an anchor with value of strFileName and uses strTitle as text description of anchor.

11  2001 Prentice Hall, Inc. All rights reserved. Outline 1 2 3 4 5 6 7 template.xml Template for message forum XML documents. The stylesheet processing instruction references formatting.xsl.

12  2001 Prentice Hall, Inc. All rights reserved. Outline 1 2 3 4 5 6 Empty Then 11 12 If Request( "name" ) <> Empty And _ 13 Request( "filename" ) <> Empty And _ 14 Request( "user" ) <> Empty And _ 15 Request( "title" ) <> Empty And _ 16 Request( "text" ) <> Empty Then 17 18 ' Lock application. No modifications but ours. 19 Call Application.Lock() 20 21 ' Creating a new XML file. 22 strPath = Server.MapPath( Request( "filename" ) ) 23 24 Set xmlFile = Server.CreateObject( "Microsoft.XMLDOM" ) 25 xmlFile.Async = False 26 27 If xmlFile.Load( strPath ) Then 28 Call Application.Unlock() 29 Call Response.Redirect( "invalid.html" ) 30 End If 31 32 ' Set up the file. 33 Call xmlFile.Load( Server.MapPath( "template.xml" ) ) 34 addForum.asp Page to add a forum. Test to see if the form was submitted by testing the form’s Submit field for a value. Check to see if a value was entered in each of the form fields. Method Load loads the template XML document.

13  2001 Prentice Hall, Inc. All rights reserved. Outline 35 ' Get the root node. 36 Set xmlRoot = xmlFile.DocumentElement 37 38 ' Set the filename. 39 Call xmlRoot.SetAttribute( "filename", _ 40 Request( "filename" ) ) 41 42 ' Create Name node. 43 Set xmlNode = xmlFile.CreateElement( "name" ) 44 xmlNode.Text = Request( "name" ) 45 Call xmlRoot.AppendChild( xmlNode ) 46 47 ' Create first message. 48 Set xmlNode = xmlFile.CreateElement( "message" ) 49 Call xmlNode.SetAttribute( "timestamp", Now & " EST" ) 50 Call xmlRoot.AppendChild( xmlNode ) 51 52 Set xmlRoot = xmlNode 53 54 ' Create user node. 55 Set xmlNode = xmlFile.CreateElement( "user" ) 56 xmlNode.Text = Request( "user" ) 57 Call xmlRoot.AppendChild( xmlNode ) 58 59 ' Create title node. 60 Set xmlNode = xmlFile.CreateElement( "title" ) 61 xmlNode.Text = Request( "title" ) 62 Call xmlRoot.AppendChild( xmlNode ) 63 64 ' Create text node. 65 Set xmlNode = xmlFile.CreateElement( "text" ) 66 xmlNode.Text = Request( "text" ) 67 Call xmlRoot.AppendChild( xmlNode ) 68 69 Call xmlFile.Save( strPath ) ' Save the file. addForum.asp Page to add a forum. Method setAttribute creates an attribute node named filename that has the value contained in form field filename. Method AppendChild appends the newly created element name node to the root element. Save method saves the XML document to disk.

14  2001 Prentice Hall, Inc. All rights reserved. Outline 70 71 ' Load XML file. 72 strPath = Server.MapPath( "forums.xml" ) 73 74 Set xmlFile = Server.CreateObject( "Microsoft.XMLDOM" ) 75 xmlFile.Async = False 76 77 If Not xmlFile.Load( strPath ) Then 78 Call Application.Unlock() 79 Call Response.Redirect( "invalid.html" ) 80 End If 81 82 ' Get the root node. 83 Set xmlRoot = xmlFile.DocumentElement 84 85 ' Create Nodes. 86 Set xmlNode = xmlFile.CreateElement( "forum" ) 87 Call xmlNode.SetAttribute( "filename", _ 88 Request( "filename" ) ) 89 xmlNode.Text = Request( "name" ) 90 Call xmlRoot.AppendChild( xmlNode ) 91 92 Call xmlFile.Save( strPath ) ' Save the file. 93 94 ' Finished processing. 95 Call Application.Unlock() 96 Call Response.Redirect( "default.asp" ) 97 Else 98 strError = "ERROR: Invalid input." 99 End If 100 101 End If 102 %> 103 addForum.asp Page to add a forum. Open XML document forums.xml. Modify forums.xml. Save forums.xml with its new contents to disk.

15  2001 Prentice Hall, Inc. All rights reserved. Outline 104 107 108 109 110 Add a Forum 111 112 113 114 115 116 117 118 119 120 Forum Name: 121 "> 123 124 125 126 Forum File Name: 127 "> 129 130 131 132 User: 133 "> 135 136 137 138 Message Title: addForum.asp Page to add a forum. XHTML form allows user to input name, filename, user name, message title and message text to create a new forum.

16  2001 Prentice Hall, Inc. All rights reserved. Outline 139 "> 141 142 143 144 Message Text: 145 147 148 149 150 151 152 153 154 155 156 157 Return to Main Page 158 159 160 161 162 addForum.asp Page to add a forum. Submit button submits form and values from form fields. Button with input type reset deletes all values in form fields

17  2001 Prentice Hall, Inc. All rights reserved. Outline addForum.asp Page to add a forum.

18  2001 Prentice Hall, Inc. All rights reserved. Outline 1 2 3 4 5 6 7 8 9 Forum 1 Name 10 11 12 Person1 13 Title One 14 Text of message of Title One 15 16 17 18 Person2 19 Title Two 20 Text of message of Title Two 21 22 23 24 Person1 25 Title Three 26 Text of message of Title Three 27 28 29 forum1.xml Sample forum. Name of forum. Message elements and child elements contain the information for a post.

19  2001 Prentice Hall, Inc. All rights reserved. Outline 1 2 3 4 5 7 8 9 10 11 12 13 14 15 16 17 18 20 21 22 23 24 26 27 28 29 30 31 32 33 formatting.xsl XSLT to transform XML forum document into HTML. Element xsl:stylesheet is the XSLT document’s root element Attribute version specifies the XSLT version to which this style sheet conforms. The attribute xmlns creates a namespace prefix xsl. This template that match es the XML document root ( / ). Get value of name element. Apply message template.

20  2001 Prentice Hall, Inc. All rights reserved. Outline 34 35 36 addPost.asp?file= 39 40 Post a Message 41 Return to Main Page 42 43 44 45 46 47 48 49 50 52 53 54 55 56 57 58 59 60 61 by 62 63 at 64 65 66 67 68 formatting.xsl XSLT to transform XML forum document into HTML. Get the value of attribute filename. Begin template message.Get value of user element.Get value of attribute timestamp.Get value of title element

21  2001 Prentice Hall, Inc. All rights reserved. Outline 69 70 71 72 73 74 75 76 77 78 79 80 formatting.xsl XSLT to transform XML forum document into HTML. Apply text property from template

22  2001 Prentice Hall, Inc. All rights reserved. Outline 1 2 3 4 Forum 1 Name 5 6 7 8 9 10 11 Forum 1 Name 12 13 14 15 16 17 Title One 18 19 20 21 by 22 Person1 23 at 24 06/28/00 14:22 25 26 27 28 Text of message of Title One 29 30 31 32 33 34 Title Two 35 Output of the transformation of the forum XML document. Forum title.Message title.Message author.Time and date posted. Message text.

23  2001 Prentice Hall, Inc. All rights reserved. Outline 36 37 38 by 39 Person2 40 at 41 06/29/00 14:22 42 43 44 45 Text of message of Title Two 46 47 48 49 50 51 Title Three 52 53 54 55 by 56 Person1 57 at 58 06/29/00 14:28 59 60 61 62 Text of message of Title Three 63 64 65 66 67 Post a Message Output of the transformation of the forum XML document. Message author. Time and date posted. Message title. Message author.Time and date posted.Message text.Link to add a new post.

24  2001 Prentice Hall, Inc. All rights reserved. Outline 68 69 Return to Main Page 70 71 72 73 Output of the transformation of the forum XML document.

25  2001 Prentice Hall, Inc. All rights reserved. Outline 1 2 3 4 5 6 Empty Then 11 12 If Request( "file" ) <> Empty And _ 13 Request( "user" ) <> Empty And _ 14 Request( "title" ) <> Empty And _ 15 Request( "text" ) <> Empty Then 16 17 ' Lock application. No modifications but ours. 18 Call Application.Lock() 19 20 strPath = Server.MapPath( Request( "file" ) ) 21 22 Set xmlFile = Server.CreateObject( "Microsoft.XMLDOM" ) 23 xmlFile.Async = False 24 25 If Not xmlFile.Load( strPath ) Then 26 Call Application.Unlock() 27 Call Response.Redirect( "invalid.html" ) 28 End If 29 30 ' Get the root node. 31 Set xmlRoot = xmlFile.DocumentElement 32 33 ' Create first message. 34 Set xmlNode = xmlFile.CreateElement( "message" ) 35 Call xmlNode.SetAttribute( "timestamp", Now & " EST" ) addPost.asp Adding a message to a forum. Test to see if the form was submitted by testing the form’s Submit field for a value. Check to see if a value was entered in each of the form fields. Method Load loads the forum XML document. Create a message node.Create a timeStamp attribute for the message node.

26  2001 Prentice Hall, Inc. All rights reserved. Outline 36 Call xmlRoot.AppendChild( xmlNode ) 37 38 Set xmlRoot = xmlNode 39 40 ' Create user node. 41 Set xmlNode = xmlFile.CreateElement( "user" ) 42 xmlNode.Text = Request( "user" ) 43 Call xmlRoot.AppendChild( xmlNode ) 44 45 ' Create title node. 46 Set xmlNode = xmlFile.CreateElement( "title" ) 47 xmlNode.Text = Request( "title" ) 48 Call xmlRoot.AppendChild( xmlNode ) 49 50 ' Create text node. 51 Set xmlNode = xmlFile.CreateElement( "text" ) 52 xmlNode.Text = Request( "text" ) 53 Call xmlRoot.AppendChild( xmlNode ) 54 55 Call xmlFile.Save( strPath ) ' Save the file. 56 57 ' Finished processing. 58 Call Application.Unlock() 59 Call Response.Redirect( Request( "file" ) ) 60 Else 61 strError = "ERROR: Invalid input." 62 End If 63 64 End If 65 %> 66 67 70 addPost.asp Adding a message to a forum. Create the children of the message node: user, title and text.

27  2001 Prentice Hall, Inc. All rights reserved. Outline 71 72 73 Post a Message 74 75 76 77 78 79 80 81 82 User: 83 "> 85 86 87 Message Title: 88 "> 90 91 92 Message Text: 93 95 96 97 "> 99 100 101 102 103 addPost.asp Adding a message to a forum. Form allows the user to create a post by entering the values into the form fields.

28  2001 Prentice Hall, Inc. All rights reserved. Outline 104 105 ">Return to Forum 106 107 108 109 addPost.asp Adding a message to a forum. Program Output

29  2001 Prentice Hall, Inc. All rights reserved. Outline 1 4 5 6 7 8 9 10 Deitel Book Organization 11 12 13 14 15 Invalid Request. 16 17 Return to Main Page 18 19 20 21 invalid.html Document showing that an error has occurred. Link that references CSS style sheet site.css.

30  2001 Prentice Hall, Inc. All rights reserved. Outline 1 /* Fig. 15.12 : site.css */ 2 3 BODY 4 { 5 background: white; 6 color: black; 7 font-family: Arial, sans-serif; 8 font-size: 10pt; 9 } 10 11 a 12 { 13 background: transparent; 14 color: blue; 15 text-decoration: none; 16 } 17 18 a:hover 19 { 20 text-decoration: underline; 21 } 22 23 table 24 { 25 border-width: 1px; 26 border-style: solid; 27 } 28 29.forumTitle 30 { 31 background: lime; 32 color: black; 33 font-size: 12pt; 34 font-weight: bold; 35 text-align: center; site.css CSS document for HTML pages. Define styles for the body element.Define styles for the anchor element.Define styles for the table element.

31  2001 Prentice Hall, Inc. All rights reserved. Outline 36 } 37 38.msgTitle 39 { 40 background: silver; 41 color: black; 42 font-size: 10pt; 43 font-weight: bold; 44 } 45 46.msgInfo 47 { 48 background: silver; 49 color: black; 50 font-size: 10pt; 51 } 52 53.msgPost 54 { 55 background: silver; 56 color: black; 57 font-size: 8pt; 58 } 59 60.msgText 61 { 62 font-size: 10pt; 63 padding-left: 10px; 64 } 65 66.date 67 { 68 font-size: 8pt; 69 } site.css CSS document for HTML pages.


Download ppt " 2001 Prentice Hall, Inc. All rights reserved. Chapter 15 – Case Study: Message Forum with Active Server Pages Outline 15.1Introduction 15.2Setup and."

Similar presentations


Ads by Google