CSCI 4230
Internet Applicaiton Development
Spring 2001
Suggested Solution to Homework #7

(1)    For example:

h7.html

<html>
<head>
<title>Main page for Homework #7, CSCI 4230, Spring 2001</title>
<link rel="stylesheet" type="text/css" href="styles/DynamicLink.css">
</head>
<body bgcolor="#ccccff">
<h2>URL Submitter and Viewer (Simplified)</h2>
This page allows you to
<a href="h7submit.html">submit</a> an URL or
<a href="h7view.asp">view</a> all URL submitted.
</body>
</html>

h7submit.html

<html>
<head>
<title>Insert to DB for Homework #7, CSCI 4230, Spring 2001</title>
<link rel="stylesheet" type="text/css" href="styles/DynamicLink.css">
</head>
<body bgcolor="#ccccff">
<h2>Submit an URL</h2>
<form action="h7insert.asp" method="post">
Your name: <input type="text" name="Submitter" maxlength="50" size="30">
<br>
Your email: <input type="text" name="SubmitterEmail" maxlength="50" size="30">
<br>
URL to submit:
<input type="text" name="Url" maxlength="200" size="60">
<p>
<input type="submit">&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset">
</form>
<p>
Back to <a href="h7.html">main</a>.
</body>
</html>

h7insert.asp

<% Option Explicit  %>
<html>
<head>
<title>Insert to DB for Homework #7, CSCI 4230, Spring 2001</title>
<link rel="stylesheet" type="text/css" href="styles/DynamicLink.css">
</head>
<body bgcolor="#ccccff">

<% Const DSN = "yuep"

 ' Get a HTTP parameter checking both
 Function getParameter(key)
  getParameter = ""
  If (Request.Form(key) <> "") Then
   getParameter = request.Form(key)
  Else
   getParameter = request.QueryString(key)
  End If
 End Function ' getParameter

 ' Display the error page
 Function errorPage(message)
%>
  <h2>Error in processing submission</h2>
  Sorry, an error has errored in your submission.  Please try again
  after correcting the following error.  Thanks.
  <p>
  <span style="color:red"><% = message %></span>
  <p>
  Back to <a href="h7.html">main</a>.
  </body>
  </html>
<%  Response.end()
 End Function ' errorPage

 ' HTTP parameters
 Dim strSubmitter
 Dim strSubmitterEmail
 Dim strURL

 ' Get HTTP parameters
 strURL = getParameter("url")
 if (strURL = "") then
  errorPage("No URL submitted.")
 End if
 strSubmitter = getParameter("submitter")
 strSubmitterEmail = getParameter("submitterEmail")

 ' Open ASPTear
 Dim objASPTear
 Set objASPTear = Server.CreateObject("SOFTWING.ASPtear")
 objASPTear.FollowRedirects = True
 On Error Resume Next

    Dim strURLContents
 strURLContents = ""
 
 ' Retrieve URL Contents
 strURLContents = objASPTear.Retrieve(strURL, 2, "", "", "")
 if (strURLContents = "") then
  errorPage("Cannot retrieve the page with the submitted URL: " & strURL & ".")
 End if

 ' Extract the title, if exists
 Dim strTitle
 strTitle = ""
 Dim regEx, Match, Matches
 Set regEx = New RegExp
 regEx.Pattern = "<title.*?>(.*?)<\/title>"
 regEx.IgnoreCase = True
 Set Matches = regEx.Execute(strURLContents)
 if (Matches.count > 0) then
  strTitle = Matches(0).Submatches(0)
 end if

 ' SQL string and its value part.
 Dim strSQL
 Dim strValue

 Dim conn
    Set conn = Server.CreateObject("adodb.connection")
 conn.open DSN
 ' conn.open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=f:\staff\y\yuep\db\db.mdb")

 ' Check URL existence in the database:
 ' s01h7(Url, Submitter, SubmitterEmail, HTMLTitle, Body, AddDate)
 strSQL = "select * from s01h7 where URL = '" & strURL & "'"
 Dim Result
    Set Result = conn.execute(strSql)
 if (not Result.eof) then
  conn.close
  errorPage("URL " & strURL & " already exists in the database.")
 end if

 ' Store information into the database.
 ' Prepare insert statement.

 ' Convert ' to '' for all fields.
 strURLContents = replace(strURLContents, "'", "''")
 strTitle = replace(strTitle, "'", "''")
 strSubmitter = replace(strSubmitter, "'", "''")
 strSubmitterEmail = replace(strSubmitterEmail, "'", "''")

 strSQL = "insert into s01h7(Url"
 strValue = "values('" & strURL & "'"
 if (strSubmitter <> "") Then
  strSQL = strSQL & ", Submitter"
  strValue = strValue & ", '" & strSubmitter & "'"
 End if

 if (strSubmitterEmail <> "") Then
  strSQL = strSQL & ", SubmitterEmail"
  strValue = strValue & ", '" & strSubmitterEmail & "'"
 End if

 strSQL = strSQL & ", Body"
 strValue = strValue & ", '" & strURLContents & "'"

 if (strTitle <> "") Then
  strSQL = strSQL & ", HTMLTitle"
  strValue = strValue & ", '" & strTitle & "'"
 End if

 strSQL = strSQL & ", AddDate) "
 strValue = strValue & ", '" & Now() & "')"
 strSQL = strSQL & strValue

 ' Response.write ("Sql = " & strSQL)
 ' Response.End

    conn.execute(strSql)
    conn.close
%>
<h2>Thank you for your submission</h2>
You can go back to the <a href="h7.html">main page</a>.
</body>
</html>

h7view.asp

<% Option Explicit  %>
<html>
<head>
<title>Viewer for Homework #7, CSCI 4230, Spring 2001</title>
<link rel="stylesheet" type="text/css" href="styles/DynamicLink.css">
</head>
<body bgcolor="#ccccff">
<h2>URL submitted to this database</h2>

<% Const DSN = "yuep"
 Dim conn
    Set conn = Server.CreateObject("adodb.connection")
 conn.open DSN
 ' conn.open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=f:\staff\y\yuep\db\db.mdb")

 ' SQL string and its value part.
 ' s01h7(Url, Submitter, SubmitterEmail, HTMLTitle, Body, AddDate)
 Dim strSQL
 strSQL = "select Url, Submitter, SubmitterEmail, HTMLTitle, AddDate from s01h7"

 Dim Result
    Set Result = conn.execute(strSQL)

 if (Result.Eof) Then
  Response.write("Sorry, no URL is found in the database.")
 else
%>
The following url are found in the database.
<p>
<ul>
<%
  do while (not Result.Eof)
   Response.write("<li>")
   if (Result.fields("Submitter") <> "") Then
    Response.write("Submitted by " & Result.fields("Submitter") & ": ")
   end if
   Response.write("<a href='" & Result.fields("Url") & "'>")
   if (Result.fields("HTMLTitle") <> "") then
    Response.write(Result.fields("HTMLTitle"))
   else
    Response.write(Result.fields("Url"))
   end if
   Response.write("</a>, cached at " & Result.fields("AddDate") & ": <a href='h7cache.asp?type=page&url=" _
                  & Result.fields("Url") & "'>page</a>, " _
                  & "<a href='h7cache.asp?type=source&url=" & Result.fields("Url") & "'>source</a>. <br> ")
   Result.Movenext
  loop
 end if
 conn.close()
%>
</ul>
<p>
Back to <a href="h7.html">main</a>.

</body>
</html>

h7cache.asp

<% Option Explicit  %>
<%
 ' Get a HTTP parameter checking both
 Function getParameter(key)
  getParameter = ""
  If (Request.Form(key) <> "") Then
   getParameter = request.Form(key)
  Else
   getParameter = request.QueryString(key)
  End If
 End Function ' getParameter

 ' Display the error page
 Function errorPage(message)
%>
  <html>
  <head>
  <title>Error in Cached Page Viewer for Homework #7, CSCI 4230, Spring 2001</title>
  <link rel="stylesheet" type="text/css" href="styles/DynamicLink.css">
  </head>
  <body>
  <h2>Error in processing submission</h2>
  Sorry, an error has errored in your submission.  Please try again
  after correcting the following error.  Thanks.
  <p>
  <span style="color:red"><% = message %></span>
  <p>
  Back to <a href="h7.html">main</a>.
  </body>
  </html>
<%  Response.end()
 End Function ' errorPage

 ' HTTP parameters
 Dim strType
 Dim strURL

 ' Get HTTP parameters
 strURL = getParameter("url")
 if (strURL = "") then
  errorPage("No URL is given.")
 End if
 strType = getParameter("type")

 ' Open database connection.
 Dim conn
    Set conn = Server.CreateObject("adodb.connection")
 conn.open ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=f:\staff\y\yuep\db\db.mdb")

 ' SQL string and its value part.
 ' s01h7(Url, Submitter, SubmitterEmail, Title, Body, AddDate)
 Dim strSQL
 strSQL = "select Body from s01h7 where URL = '" & strUrl & "'"

 Dim Result
    Set Result = conn.execute(strSql)

 if (Result.Eof) Then
  errorPage("No page for the URL " & strUrl & " is cached in the database.")
 end if
 
 if (strType <> "page" and strType <> "source") then
  errorPage("Unknown view type.")
 else
  if (strType = "page") then
   Response.write(Result.fields("Body"))
  else
   Dim Body
   Body = Server.HTMLEncode(Result.fields("Body"))
   Body = "<pre>" & Body & "</per>"
%>
  <html>
  <head>
  <title>Cached Page Viewer for Homework #7, CSCI 4230, Spring 2001</title>
  <link rel="stylesheet" type="text/css" href="styles/DynamicLink.css">
  </head>
  <body>
  <h2>Source Code of Cached Page</h2>
  <p>
  Back to <a href="h7.html">main</a>.
  <p>
  The following is the source code of the cache page <span style="color:red"><% = strUrl %></span>:
  <hr>
  <% =Body %>
  </body>
  </html>

<%
  end if
 end if
%>