NAME

Apache2::ASP::Manual::Intro - Introduction to Apache2::ASP

SYNOPSIS

<html>
  <body>
    <%= "Hello, World!" %>
    <br>
    <%
      for( 1...10 ) {
        $Response->Write( "Hello from ASP ($_)<br>" );
      }
    %>
  </body>
</html>

DESCRIPTION

Apache2::ASP is a new implementation of the ASP web programming for the mod_perl2 environment. Its aim is high performance, stability, scalability and ease of use.

If you have used Apache::ASP already then you are already familiar with the basic idea of ASP under Apache.

INTRODUCTION

What is Apache2::ASP?

Apache2::ASP is a web programming environment that helps simplify web programming with Perl under mod_perl2. Apache2::ASP allows you to easily embed Perl into web pages using the "<%" and "%>" tags that are familiar to anyone who has used ASP or JSP in the past.

What does Apache2::ASP offer?

Apache2::ASP offers programmers the ability to program web pages without spending time on details like session state management, file uploads or template systems.

ASP OBJECTS

Like other ASP web programming environments, Apache2::ASP provides the following global objects:

$Request

Represents the incoming HTTP request. Has methods to handle form data, file uploads, read cookies, etc.

Learn more by reading the Apache2::ASP::Request documentation.

$Form

The same as $Request->Form, it is a hashref of all incoming form and querystring values.

$Response

Represents the outbound HTTP communication to the client. Has methods to send content, redirect, set cookies, etc.

Learn more by reading the Apache2::ASP::Response documentation.

$Session

Represents data that should persist beyond the lifetime of a single request. For example, the user's logged in state, user id, etc.

The contents of the $Session object are stored within an SQL database.

Learn more by reading the Apache2::ASP::Session documentation.

$Server

Represents the webserver itself and offers several utility methods that don't fit anywhere else.

Learn more by reading the Apache2::ASP::Server documentation.

$Application

Represents data that should be shared and persisted throughout the entire web application. For example, database connection strings, the number of active users, etc.

The contents of the $Application object are stored within an SQL database.

Learn more by reading the Apache2::ASP::Application documentation.

$Config

Encapsulates all the configuration information for your web application.

Learn more by reading the Apache2::ASP::Config documentation.

INSTALLATION - STEP 1 - LINUX

CPAN

% perl -MCPAN -e shell
cpan> install Apache2::ASP
...
cpan> quit

Then you're done and can skip ahead to "Directory Structure".

Command Line

If (for whatever reason) the CPAN installation doesn't work for you, simply extract Apache2-ASP-xxx.tar.gz, then cd to that directory and run the following commands:

% su non-root-user
% perl Makefile.PL
% make
% make test
% exit
% make install

Because Apache2::ASP makes use of the Apache::Test framework, you cannot run the tests as root unless the "nobody" user can create files in the current directory. That's life.

Directory Structure

You might be wondering, "What does the directory structure for an Apache2::ASP website look like?"

Well, it looks like this:

.
|-- conf (+r)
|   |-- apache2-asp-config.xml
|   `-- httpd.conf
|-- etc (+r)
|   |
|   `-- Any other files needed by the site.
|-- MEDIA (+rw)
|-- PAGE_CACHE (+rw) - Compiled ASP scripts will be placed here automatically.
|-- tmp (+rw)
|   |
|   `-- SQLite database files go here.
|-- lib (+r)
|   |
|   `-- your *.pm modules go here (i.e. Class::DBI or DBIx::Class modules, but *not* Handlers).
|--handlers (+r)
|  |--MyHandler.pm
|  `--MyOtherHandler.pm
`-- www (+r)
    |-- GlobalASA.pm
    `-- index.asp

httpd.conf

You need to add the following to your httpd.conf

# Needed for CGI::Apache2::Wrapper to work properly:
LoadModule apreq_module    /usr/local/apache2/modules/mod_apreq2.so

# Set the directory index:
DirectoryIndex index.asp

# Set this variable:
PerlSetEnv APACHE2_ASP_APPLICATION_ROOT /path/to/your/web/application

# Load up some important modules:
PerlModule Apache2::ASP
PerlModule Apache2::ASP::PostConfigHandler
PerlPostConfigHandler Apache2::ASP::PostConfigHandler

# Configuration for MediaManager:
PerlModule        Apache2::ASP::TransHandler
PerlTransHandler  Apache2::ASP::TransHandler

# All *.asp files are handled by Apache2::ASP
<Files ~ (\.asp$)>
  SetHandler  perl-script
  PerlHandler Apache2::ASP
</Files>

# Prevent anyone from getting your GlobalASA.pm
<Files ~ (\.pm$)>
  Order allow,deny
  Deny from all
</Files>

# All requests to /handlers/* will be handled by their respective handler:
<Location /handlers>
  SetHandler  perl-script
  PerlHandler Apache2::ASP
</Location>

# Main website:
<VirtualHost *:80>
  ServerName    yoursite.yourhost.com
  DocumentRoot  /path/to/your/web/application/htdocs
</VirtualHost>

XML Config File

Then create a directory at /path/to/your/website/PAGE_CACHE at the root of your application. The Apache server process should be able to read and write in this directory.

Then, in /path/to/your/website/conf add the file apache2-asp-config.xml. It will contain data like this:

<?xml version="1.0" ?>
<config>
  <web_application>
    <!-- This should be a regex that matches your site. -->
    <domain_re>.*</domain_re>
    <do_reload_on_script_change>1</do_reload_on_script_change>
    <application_name>DefaultApp</application_name>
    <application_root>@ServerRoot@</application_root>
    <handler_root>@ServerRoot@/handlers</handler_root>
    <media_manager_upload_root>@ServerRoot@/MEDIA</media_manager_upload_root>
    <www_root>@ServerRoot@/htdocs</www_root>
    <page_cache_root>@ServerRoot@/PAGE_CACHE</page_cache_root>
    <application_state>
      <manager>Apache2::ASP::ApplicationStateManager::SQLite</manager>
      <dsn>DBI:SQLite:dbname=/tmp/apache2_asp_state</dsn>
      <username></username>
      <password></password>
    </application_state>
    <session_state>
      <manager>Apache2::ASP::SessionStateManager::SQLite</manager>
      <cookie_domain>localhost</cookie_domain>
      <cookie_name>session-id</cookie_name>
      <dsn>DBI:SQLite:dbname=/tmp/apache2_asp_state</dsn>
      <username></username>
      <password></password>
      <session_timeout>30</session_timeout>
    </session_state>
    <settings>
      <lib>@ServerRoot@/lib</lib>
      <dsn>DBI:mysql:databasename:localhost</dsn>
      <username>abc</username>
      <password>123</password>
    </settings>
  </web_application>
  <!-- You can specify more web_application elements below. -->
</config>

INSTALLATION - STEP 1 - WINDOWS

Although performance is poor on Windows, it works fine for testing or developing. I would not recommend running a production web application on Windows however.

INSTALLATION - STEP 2

Then, in your database, create a table with the following structure:

CREATE TABLE asp_sessions (
  session_id CHAR(32) PRIMARY KEY NOT NULL,
  session_data BLOB,
  created_on DATETIME,
  modified_on DATETIME
);

Also create a table with the following structure:

CREATE TABLE asp_applications (
  application_id VARCHAR(100) PRIMARY KEY NOT NULL,
  application_data BLOB
);

Simply (re)start Apache and installation is complete. Now you need some ASP scripts.

If your website is in /var/www/html then create a file "index.asp" in /var/www/html.

Your index.asp could contain something like the following:

<html>
  <body>
    <%= "Hello, World!" %>
    <br>
    <%
      for( 1...10 ) {
        $Response->Write( "Hello from ASP ($_)<br>" );
      }
    %>
  </body>
</html>

Then point your browser to http://yoursite.com/index.asp and see what you get.

If everything was configured correctly, the output would look like:

Hello, World! 
Hello from ASP (1)
Hello from ASP (2)
Hello from ASP (3)
Hello from ASP (4)
Hello from ASP (5)
Hello from ASP (6)
Hello from ASP (7)
Hello from ASP (8)
Hello from ASP (9)
Hello from ASP (10)

If you get an error instead, check out your error log to find out why.

BUGS

It's possible that some bugs have found their way into this release.

Use RT http://rt.cpan.org/NoAuth/Bugs.html?Dist=Apache2-ASP to submit bug reports.

HOMEPAGE

Please visit the Apache2::ASP homepage at http://www.devstack.com/ to see examples of Apache2::ASP in action.

AUTHOR

John Drago mailto:jdrago_999@yahoo.com

COPYRIGHT AND LICENSE

Copyright 2007 John Drago, All rights reserved.

This software is free software. It may be used and distributed under the same terms as Perl itself.