Apache-ASP

 view release on metacpan or  search on metacpan

site/articles/perlmonth2_build.html  view on Meta::CPAN

needs to have.  We have 2 pages, the intro, and the 
actual bookmarks page, where we get to view, add, and 
delete the bookmark entries.  We have the user login to 
the bookmarks, and logout, securing access for the user's 
eyes only.
<p>
<center><img src=flow.gif border=0></center>
<p>
You might also design the objects, methods, and functions
that will be used for the site, but this site is so 
simple, that we are going to jump into implementation.

<h3>Implementation</h3>
We start by configuring <tt>.htaccess</tt> file 
of a directory in apache to allow Apache::ASP
to run <tt>.asp</tt> files, and testing the configuration
with a <tt>dummy.asp</tt> file.


	<p>
	<center>
	<table border=0 cellspacing=0 width=90% >
	<tr bgcolor=gray><td><font color=white><b># .htaccess</b></td></tr>
	<tr bgcolor=#c0c0c0><td><pre>
<tt>DirectoryIndex index.asp
&lt;Files ~ \.asp$&gt;
	SetHandler perl-script
	PerlHandler Apache::ASP
	PerlSetVar Global .
	PerlSetVar GlobalPackage My::Bookmarks
	PerlSetVar StateDir /tmp/asp_apps_bookmarks
	PerlSetVar Debug 2
	PerlSetVar SessionTimeout 15
	PerlSetVar StatScripts 1
	PerlSetVar AllowApplicationState 1
	PerlSetVar AllowSessionState 1
&lt;/Files&gt;</tt></pre></td></tr>
	</table>
	</center>
	<p>
	

	<p>
	<center>
	<table border=0 cellspacing=0 width=90% >
	<tr bgcolor=gray><td><font color=white><b># dummy.asp</b></td></tr>
	<tr bgcolor=#c0c0c0><td><pre>
<tt>INTRO &lt;%=$Session%&gt;</tt></pre></td></tr>
	</table>
	</center>
	<p>
	

If the index.asp works on your server, and just prints
<tt>INTRO Apache::ASP::Session=HASH(0x??????)</tt>, 
then we know Apache::ASP is working and $Sessions are 
enabled.

<hr size=1>

Next, we set up the <tt>global.asa</tt> with globals and 
libraries that need to be initialized for the web 
application, and define the relevant event handlers.  
We also set up per request globals, like the document's 
title, which is something that we can do in 
<tt>Script_OnStart</tt>.  Finally, we use
the <tt>Script_OnStart</tt> and <tt>Script_OnEnd</tt>
events to automatically include the header and footer
for each script in our web application, and initialize
relevant globals used by the scripts.
<p>
Notice that each script can process its own <tt>Logout</tt>
request, which was a decision made after the design
because it seemed good to make the first script, <tt>index.asp</tt>,
<tt>$Session</tt> aware.

	<p>
	<center>
	<table border=0 cellspacing=0 width=90% >
	<tr bgcolor=gray><td><font color=white><b># global.asa</b></td></tr>
	<tr bgcolor=#c0c0c0><td><pre>
<tt>use File::Basename;
use DBI;
use DBD::CSV;

use vars qw( $DarkColor $Name %Titles $FontBase $Db $Title $Basename $Form $Query );

$DarkColor = &#39;#0000aa&#39;;
$Name = &quot;MyBookmarks&quot;;
%Titles = (
	   &#39;index.asp&#39; =&gt; &#39;Introduction&#39;,
	   &#39;bookmarks.asp&#39; =&gt; &#39;Viewer&#39;
	  );
$FontBase = &#39;face=verdana,arial&#39;;

$Db = DBI-&gt;connect(&quot;DBI:CSV:f_dir=&quot;.Apache-&gt;dir_config(&#39;StateDir&#39;), &#39;&#39;, &#39;&#39;, 
		   { RaiseError =&gt; 1 })
  or die &quot;Cannot connect: &quot; . $DBI::errstr;

# setup bookmark database if first time
unless(eval { $Db-&gt;do(&quot;select bookmark_id,username,title,url from bookmarks&quot;) }) {
    eval { $Db-&gt;do(&quot;drop table bookmarks&quot;); };
    $Db-&gt;do(&lt;&lt;CREATE) || die(&quot;can&#39;t create table $DBI::errstr&quot;);
    create table bookmarks (
			    bookmark_id varchar(15),
			    username varchar(30),
			    title varchar(60),
			    url varchar(120)
			   )
CREATE
  ;
}

$Db-&gt;do(&quot;select * from bookmarks&quot;)
  || die(&quot;can&#39;t do select against bookmarks: $DBI::errstr&quot;);

sub Script_OnStart {
    $Basename = basename($0);
    $Title = $Name.&#39; / &#39;.$Titles{$Basename};
    $Response-&gt;Include(&#39;header.inc&#39;);
    $Form = $Request-&gt;Form();
    $Query = $Request-&gt;QueryString();
    $Response-&gt;Expires(0);

    # a user may logout from any script, destroy session, and go
    # to login / intro page
    if($Form-&gt;{logout}) {
	$Session-&gt;Abandon();
	$Response-&gt;Redirect(&quot;index.asp?abandon=&quot;.
			    ++$Application-&gt;{abandon});
    }
}

sub Script_OnEnd {
    $Response-&gt;Include(&#39;footer.inc&#39;);
}

sub Application_OnStart {
    # use max_bookmark_id as a pseudo sequence
    $Application-&gt;Lock();
    my $sth = $Db-&gt;prepare_cached
      (&quot;select bookmark_id from bookmarks order by bookmark_id desc&quot;);
    $sth-&gt;execute();
    $Application-&gt;{max_bookmark_id} = $sth-&gt;fetchrow_array();
    $Application-&gt;UnLock();
}</tt></pre></td></tr>
	</table>
	</center>
	<p>
	

<hr size=1>

Next we set up the headers and footers for each page.
One problem with <tt>HTML</tt> is that it requires you to specify
the unique titles of the document before the standard
body style for your site, so we cheated this and
created the per page titles already in the <tt>Script_OnStart</tt>
of the <tt>global.asa</tt>.


	<p>
	<center>
	<table border=0 cellspacing=0 width=90% >
	<tr bgcolor=gray><td><font color=white><b># header.inc</b></td></tr>
	<tr bgcolor=#c0c0c0><td><pre>
<tt>&lt;html&gt;
&lt;head&gt;&lt;title&gt;&lt;%=$Title%&gt;&lt;/title&gt;&lt;/head&gt;
&lt;body bgcolor=white link=purple alink=yellow vlink=gray&gt;

&lt;form src=&lt;%=$Basename%&gt; method=POST&gt;
&lt;table border=0 width=100% cellpadding=5 cellspacing=0&gt;
&lt;tr bgcolor=&lt;%= $DarkColor %&gt;&gt;
	&lt;td&gt;
	&lt;b&gt;&lt;font &lt;%=$FontBase%&gt; size=+1 color=yellow&gt;
		&lt;%=$Title%&gt;
		&lt;% if($Session-&gt;{user}) { %&gt;
		  for &lt;%= $Session-&gt;{user} %&gt;
		&lt;% } %&gt;
	&lt;/font&gt;&lt;/b&gt;
	&lt;/td&gt;
	&lt;td align=right&gt;
	&lt;font &lt;%=$FontBase%&gt;&gt;
	&lt;% if($Session-&gt;{&#39;user&#39;}) { %&gt;
		&lt;input type=submit name=logout value=Logout&gt;
	&lt;% } else { %&gt;
		&amp;nbsp;
	&lt;% } %&gt;
	&lt;/font&gt;
	&lt;/td&gt;
&lt;/tr&gt;
&lt;/form&gt;
&lt;/table&gt;

&lt;table border=0 cellpadding=5 width=100% &gt;&lt;tr&gt;&lt;td valign=top&gt;
&lt;font &lt;%=$FontBase%&gt; size=+0&gt;</tt></pre></td></tr>
	</table>
	</center>
	<p>
	

	<p>
	<center>
	<table border=0 cellspacing=0 width=90% >
	<tr bgcolor=gray><td><font color=white><b># footer.inc</b></td></tr>
	<tr bgcolor=#c0c0c0><td><pre>
<tt>&lt;/font&gt;
&lt;/table&gt;

&lt;table border=0 width=100% cellpadding=5&gt;
&lt;tr&gt;
	&lt;td bgcolor=yellow align=center&gt;
	&lt;font &lt;%=$FontBase%&gt; size=-1 color=&lt;%= $DarkColor %&gt;&gt;
	&lt;b&gt;
		My-NotExists-Bookmarks 
		Cool Technologies Etc., ???, &amp;copy; &lt;%= (localtime())[5] + 1900 %&gt;
	&lt;/b&gt;
	&lt;/font&gt;
	&lt;/td&gt;



( run in 1.020 second using v1.01-cache-2.11-cpan-39bf76dae61 )