

ASP ADVANCED
This section shows how to
use the Content Linking component to create a simple newsgroup.
Users of the newsgroup can post new messages and read postings from
other users.
The Content Linking
component is used in this example to organize the messages in the
newsgroup. The component
enables a user to view a list of all postings in the newsgroup, and lets
the user move easily from one posting to the next.
To create the newsgroup,
you need to create the following four files:
- The Post page.
This page is used to post new messages.
- The Include file.
This file is included at the bottom of every message
posted.
- The New Item
page. This page is
used to dynamically generate new Active Server
Pages containing the postings.
- The News page.
This page contains a list of all messages posted in the
newsgroup.
The Post Page
The purpose of the
Post page is to enable users to post new messages. It's a normal HTML page
containing an HTML form, as shown in Listing below. The form has a subject
line so that the user can enter a subject for his or her posting.
The form also has a text area for entering a message.
The WRAP=VIRTUAL attribute is used to allow word wrapping in the
text area.
<HTML>
<HEAD><TITLE> Post Page </TITLE></HEAD>
<BODY>
<H2>Post A New Message</H2>
<FORM METHOD="POST" ACTION="newitem.asp">
SUBJECT: <INPUT
NAME="subject" TYPE="TEXT" SIZE="50"
MAXLENGTH=50>
<BR>
<TEXTAREA NAME="posting" COLS=60 ROWS=10
WRAP="VIRTUAL"></TEXTAREA>
<P>
<INPUT TYPE="RESET" VALUE="CLEAR">
<INPUT TYPE="SUBMIT" VALUE="POST THIS">
</FORM>
</BODY>
</HTML>
The Include File
Each message in the
newsgroup will include a link to the previous and next messages, as
Listing above shows. It will
also include a link back to the News page. These links are added to the
message in the Include file. This
file is automatically included in every new Active Server Page generated
by the New Item page. You'll
see how this is done in the next section.
Script for news.inc
<HR>
<%
Set MyLinks=Server.CreateObject("MSWC.NextLink")
IF mylinks.GetListIndex("news.txt")>1 THEN
%>
<A HREF="<%=mylinks.GetPreviousURL("news.txt")%>">
Previous Posting</A>
<% END IF %>
<A HREF="<%=mylinks.GetNextURL("news.txt")%>">
Next Posting</A>
<P><A HREF="news.asp">Back To News Page</A>
The New Item Page
After a user posts a
message, she is brought to the New Item page.
The New Item page looks boring. However, behind the scenes, the
page actually does quite a lot.
The New Item page is used
to dynamically generate new Active Server Pages.
When a user posts a new message, that message is saved in a text
file. However, the text file
is no ordinary text file. It's
an Active Server Page. The
New Item page creates the Active Server Page from the information the user
entered into the HTML form.
The New Item page
also updates the Content Linking List file.
The page appends new information about the new posting to this
file. This enables the
Content Linking component to
reflect new postings accurately and automatically.
The listing for the New
Item page is longer than any of the examples previously discussed.
The approach here is to first present the listing for the page in
its entirety (see Listing below). Each section of the page is then
discussed in detail.
<%
' Create the posting
TheSubj=Server.HTMLEncode(Request.Form("subject"))
IF TheSubj=" " THEN TheSubj="No Subject"
ThoPost="<HTML><HEAD><TITLE>"&TheSubj&"</TITLE></HEAD><BODY>"
ThePost=ThePost&"Date
Posted:"&NOW&"<HR>"
ThePost=ThePost&Server.HTMLEncode(Request.Form("posting"))
ThePost=ThePost&"<!-- #INCLUDE VIRTUAL=" " news.inc"
-->"
ThePost=ThePost&"</BODY></HTML>"
' Create unique file name
Set mylinks=Server.CreateObject("MSWC.NextLink")
TheName="item"&mylinks.GetListCount("news.txt")+1&".asp"
TheNamePath=Server.MapPath(TheName)
' Save the new posting file
Set MyFileObj=Server.CreateObject("Scripting.FileSystemObject")
Set MyOutStream=MyFileObj.CreateTextFile(TheNamePath)
MyOutStream.Write ThaPost
MyOutStream.Close
' Update the Content
Linking List File
TheNews=Server.MapPath("news.txt")
Set MyNews=MyFileObj.OpenTextFile(TheNews,8,TRUE)
MyNews.Writeline TheName&vbTab&TheSubj
MyNews.Close
%>
<HTML>
<HEAD><TITLE>New Item</TITLE></HEAD>
<BODY>
<H1> Thank you for posting a new message! </H1>
<A HREF="news.asp">News Page</A>
</BODY>
</HTML>
In the first Section of
this page, a variable named ThePost is created.
The value of this variable is actually an Active Server Page.
The Active Server Page is created as one long string.
Here's the portion of the page that does this:
' Create the
posting
TheSubj=Server.HTMLEncode(Request.Form("subject"))
IF TheSubj=" " THEN TheSubj="No Subject"
ThePost="<HTML><HEAD><TITLE>"&TheSubj&"</TITLE></HEAD><BODY>"
ThePost=ThePost&"Date
Posted:"&NOW&"<HR>"
ThePost=ThePost&Server.HTMLEncode(Request.Form("posting"))
ThePost=ThePost&"<!-- #INCLUDE VIRTUAL="news.inc"
"-->"
ThePost=ThePost&"</BODY></HTML>"
The Active Server Page
that's created includes the text the user entered into the HTML form.
It also includes the current date and time.
Finally, the INCLUDE directive is added to automatically include
the news.inc file discussed earlier.
The next section of the
script provides the new Active Server Page with a unique name.
You wouldn't want a message posted by one user to overwrite a
message posted by a previous user. Before
the Active Server Page can be saved, a unique name must be generated.
This portion of the script creates the unique name:
' Create
unique file name
Set mylinks=Server.CreateObject("MSWC.NextLink")
TheName="item"&mylinks.GetListCount("news.txt")+1&".asp"
TheNamePath=Server.MapPath(TheName)
To create a unique name,
the Content Linking component is used.
Each new posting will be included in the Content Linking List file.
By retrieving a count of the number of entries in this file and
adding one, a unique name for the new Active Server Page is generated.
The first message posted will have the name item1.asp, the second
message will have the name item2.asp, and so on.
Each message posted corresponds to a unique Active Server Page.
The third section of
the New Item page script saves the new Active Server Page.
The value of
ThePost is saved to a text file. This
file is saved with the unique name contained in the variable
TheNamePath. The
FileSystemObject is used to save the file to disk:
' Save
the new posting file
Set MyFileObj=Server.CreateObject("Scripting.FileSystemObject")
Set MyOutStream=MyFileObj.CreateTextFile(TheNamePath)
MyOutStream.Write ThePost
MyOutStream.Close
The final section of
the script updates the Content Linking List file.
The path and name of the new Active Server Pages file is appended
to the Content Linking List file. Here's the portion of
the script that does the updating:
' Update
the Content Linking List File
TheNews=Server.mapPath("news.txt")
Set MyNews=MyFileObj.OpenTextFile(TheNews,8,TRUE)
MyNews.Writeline TheName&vbTab&TheSubj
myNews.Close
If the Content Linking List
file doesn't exist, this script automatically creates it.
The Content Linking List file that the script creates is named
news.txt. When the first
message is posted in the newsgroup, the Content Linking List file is
created.
Notice how the VBScript
constant vbTab is used in this script.
The columns in the Content Linking List file must be separated by a
single tab character. The
vbTab constant is used to insert this tab character.
The News Page
The final page needed
to create the newsgroup is the News page.
The News page displays the
number of messages in the newsgroup and lists all of the messages by their
subject lines. Listing below shows the script for the News page.
Script for news. asp.
<%
' Create the Content Linking component
Set mylinks=Server.CreateObject("MSWC.NextLink")
%>
<HTML>
<HEAD><TITLE> News Page </TITLE></HEAD>
<BODY>
Posted Items: <%=mylinks.GetListCount("news.txt")%>
<HR>
[<A HREF="Post.htm">Post New Message</A>]
<UL>
<%
' Display the list of messages
FOR i=1 TO mylinks.GetListCount("news.txt")
%>
<LI>
<A HREF="<%=mylinks.GetNthURL("news.txt",i)%>">
<%=mylinks.GetNthDescription("news.txt",i)%></A>
<%
NEXT
%>
</UL>
</BODY>
</HTML>
In this page, the Content
Linking component is used to retrieve the number of entries in the Content
Linking List file. This will
correspond to the number of messages posted.
Second, all the entries in the Content Linking List file are
displayed. If a user clicks
any of the entries, he'll be linked to the contents of the posting
associated with the entry.
Extending the
Newsgroup Example
The simple newsgroup
discussed in the preceding sections could be used to track only a small
number of postings for a small number of users.
Problems would arise if multiple users attempted to post new
messages at the same time. The
Content Linking component wasn't designed for this purpose.
A much better approach to
creating a newsgroup would be to store the message in a database such as
Microsoft SQL Server. To do
this, you could use the ActiveX Data Objects (for details, see Chapter
"ActiveX Data Objects"). Unlike
the Content Linking component, databases are designed to store and
retrieve large amounts of information efficiently.
However, this sample
application does illustrate how the Content Linking component can be used
to easily link large numbers of Active Server Pages together.
All the postings in the newsgroup are automatically linked in a
series. The Content Linking
component makes this easy to do.
|