What are Active Server Pages? (http://webreview.com/wr/pub/97/06/27/webdev) Go To Other Topics
Architecture
Application Object Framework ActiveX Server Components DataBase Connectivity
Develop Environment Conclusion
Active Server Pages (ASP). It's a boring name, but an innovative Web development technology that's changing the way dynamic Web pages are developed. ASP is "active" because it enables Web pages to be programmatic; interpreted on the "server" and developed on a "page" basis. Since development is moving away from traditional CGI programming to more powerful server-side scripting, ASP offers reduced development time and provides for production of faster and more interactive Web pages.
This first part in a three-part series on ASP introduces some of the basic concepts of ASP technology and illustrates these concepts by developing a user registration application. The next article will discuss database connectivity in ASP and how to save data from a user registration form. The final article will directly compare Microsoft's ASP and Netscape's server-side JavaScript (LiveWire), and will discuss their similarities and differences.
Active Server Pages is a server-side scripting technology similar to several products currently on the market. The competition includes Netscape's server-side JavaScript (formerly called LiveWire), Allaire's Cold Fusion, Next's WebObjects and NetDynamic's NetDynamics Studio. These products are similar in that they enable the developer to script Web pages that are interpreted at the Web server. Also, all of these tools provide database connectivity. What makes ASP unique is: it's ability to script in multiple languages; the fact that it is a built in component of Microsoft IIS (one of the most popular Web servers); and its flexibility to create ASP components developed in Java, C++ or Visual Basic.
The only real requirement is that you use a Webserver that supports ASP, such as Microsoft's Personal Web Server or Internet Information Server (IIS), or O'Reilly's WebSite.
Architecture
By now, you're probably asking, "How does ASP work?" Well, in the simplest case,
a user requests a .asp
page from an ASP-enabled
Webserver which receives the request and, upon recognizing it as a .asp
page, passes it to the ASP component on the Webserver.
The ASP component of the Webserver interprets the scripting on the page and sends the
result back to the browser. Hence, all of the processing is performed at the server and
the user sees only standard HTML in their Web pages.
ASP Applications
ASP applications are a set of HTML and ASP files which work together to perform a certain
task. Each ASP application requires a virtual root in the Webserver where the files are
stored. All ASP pages must end in the file extension .asp
;
this lets the Webserver know the page contains ASP logic.
Server-Side Delimiters
Special tags must be used to enclose server-side code in ASP. Server-side tags take two
forms, the <% %>
tags and the <SCRIPT></SCRIPT>
tags. Since ASP supports
multiple scripting languages, it necessitates two different tags. The <% %>
tag uses the default language (this can be set
for the whole server or a whole page), while the <SCRIPT></SCRIPT>
tag allows you to specify which language the script is written in. (ASP
currently supports native JavaScript and VBScript.)
ASP Object Framework
ASP applications have five primary objects available to them to store and retrieve data
between pages. They are:
Request
TheRequest
object contains all of the data sent to the Webserver when a browser makes a request. This includes allGET
andPOST
form data, and also includes cookies and HTTP header information. Use theForm
property to retrieve form data from theRequest
object sent by thePOST
method. For instance, specifyRequest.Form("name")
to retrieve the form elementname
from theRequest
object. Use theQueryString
method of theRequest
object to retrieve the query string sent by aGET
method. For example, to retrieve theaddress
field from a query string use:Request.QueryString("address")
Response
TheResponse
object is the counterpart to theRequest
object. TheRequest
object contains data sent to the server for a page request and theResponse
object is used in composing the data which is sent back to the browser. The most commonly used function in theResponse
object is theWrite
method. This method writes the data passed to it back to the browser. The "Hello World" example above uses that method. TheResponse
object also allows the programmer to send cookie data back to the client to be stored, to cause a redirect in the browser and to set the HTTP header data sent back to the browser.Session
TheSession
object provides the ability to save data for each user session. When a browser first requests a page from an ASP application, aSession
object is created. TheSession
object uses the cookies protocol to maintain information, so non-cookies enabled browsers will not be able to save data to theSession
object. To set a property in theSession
object, pass theSession
object the property name. The following example shows the propertyuser_name
being set in theSession
object:Session("user_name") = "Fred"
Application
TheApplication
object stores variables specific to the current application. Creating new properties in theApplication
object is simple. For example, to set the propertyuser_count
:Application("user_count") = 11
Server
TheServer
object is very similar to theApplication
object, but its scope is the whole server. All ASP applications can store and load data from theServer
object. Here is an example of setting the variableads_delivered
:Server("ads_delivered") = 1523
Both the
Application
object and theServer
object havelock
andunlock
methods. These methods allow the objects' properties to be modified from one page without permitting another page to modify it at the same time.Conclusion
We have reviewed several of ASP's main features and have applied some of these features via the user registration application. We will cover how to save the data in the user registration form in the next article of this series.Database Integration with ASP (http://webreview.com/wr/pub/97/07/11/webdev)
This is the second article of a three part series exploring Microsoft Active Server Pages. The previous article covered some basic capabilities of ASP. Now, we'll get connected! This article will cover ASP's
global.asa
file, Active Server Components and database connectivity features. In addition, we'll enhance the user registration application developed in the last article to save the registration data to a database.global.asa
Theglobal.asa
file allows the Web server to execute statements when an application starts or ends and when a session is created or destroyed. Hence, it has four procedures:Application_OnStart Application_OnEnd Session_OnStart Session_OnEndSession_OnStart
is especially powerful in that it allows variables to be set when the user initiates a session with a Web site. It could be used, for example, to set a customer ID:<SCRIPT LANGUAGE=VBScript RUNAT=Server> SUB Session_OnStart Session("customer_id") = customer_count customer_count = customer_counter + 1 END SUB </SCRIPT>An application starts when the user first requests a page in the application and ends when the server is shut down. A session is created when the user requests a page from an application where they do not have a current session. A session generally ends when the session has expired (or when the user has not requested any pages from that application for a certain period of time).ActiveX Server Components
At some point, you may need more functionality than VBScript or JavaScript can provide. Microsoft solves this problem with ActiveX Server Components. ActiveX Server Components allow the programmer to develop components in C++, Visual Basic and Java, which run on the Web server. These components have access to the variables and methods in theRequest
,Response
,Application
andServer
objects via interfaces.ASP is shipped with several ActiveX Server Components including an ad rotator, content-linking and database access components. In order to use an ActiveX Server Component it first must be assigned to an object using the
CreateObject
method of theServer
object. Once assigned to an object, all of the methods and properties of the ActiveX Server Component are available. The ever-popular rotating ad:Set ad = Server.CreateObject("MSWC.AdRotator") Response.Write(ad.GetAdvertisement("ads.txt"))Opening a Database Connection
Microsoft provides the Database Access Component to enable database connectivity inside an.asp
page. You must first have a database that is accessible via ODBC (i.e., Microsoft Access, Microsoft SQLServer, Oracle, Sybase, Informix, etc), then add a System DSN entry for your data source on the machine running the Web server. This is accomplished in the ODBC section of the Windows Control Panel. Having completed these steps, you can establish a connection to the database.A connection object must be created:
Set Conn = Server.CreateObject("ADODB.Connection")Then a connection can be established:
Conn.Open "dsn=sample_db;uid=;pwd="Executing SQL Statements
Once a connection has been established, the SQL statements can be executed with theexecute
method. First, build the SQL statement:sql = "update pricelist set price = 100 where price = 99"Then execute the statement with:
Conn.Execute(sql) Conn.CloseViewing Results of a SQL Statement
Recordsets allow you to view and modify data returned from theexecute
method. SQL cursors are represented as Recordsets in ASP and will have a property for every column returned in the SQL query. By displaying Recordsets one row at a time, you are allowed to view the next row by using theMoveNext
method. Once the Recordset is empty, the propertyEOF
will be true. Here's an example:sql = "select firstname, lastname from employee order by lastname" cursor = Conn.Execute(sql) Do While Not cursor.EOF Response.Write(cursor("firstname") & cursor("lastname") & "<br>") cursor.MoveNext LoopConclusion
In the past two articles, we have discussed the basic capabilities of ASP and developed a user registration application. Look for the third and final article to compare competing products from the two Internet powerhouses, Netscape's server-side JavaScript (known as LiveWire) and Microsoft ASP.ASP vs. LiveWire: A Server-Side Comparison (http://webreview.com/wr/pub/97/07/25/webdev)
The last of three tutorials on Active Server Pages (ASP) compares the features of Microsoft's ASP with Netscape's LiveWire (or server-side JavaScript) and rates them against each other. Who wins? Well, it looks like a photo finish.
Netscape and Microsoft continually battle for dominance in the web browser wars. Yet, while all eyes are on this rivalry, a more subtle war rages on the web server front. The outcome will have a great affect on the way web sites are developed. It is a battle for the minds of developers.
Server-side JavaScript (LiveWire) and ASP (Active Server Pages) are server-side technologies that are part of Netscape's and Microsoft's popular web servers. So what are the differences? Which one should you use? Well, as a sage old programmer would say, that depends.
We'll compare and contrast some features from both technologies. We'll examine the session management features, database connectivity, calling external libraries and the development environment. When we're finished, you should have a grasp on what ASP and Netscape server-side JavaScript can accomplish and what sets them apart.
Concepts
First, let's explain what Microsoft ASP and Netscape server-side JavaScript do. The concept for both technologies is to allow you to add programmatic logic to your web pages. This programmatic logic is surrounded by special tags and is interpreted by the web server when you request a page.For example, if you request a page ("webdev.html","webdev.asp") that is a Microsoft ASP or Netscape server-side JavaScript page from your browser, a few things happen. The web server receives the request and notices that the page requested is an ASP or server-side JavaScript page. It interprets the logic designated to run on the server and delivers the request back to the browser.
Common Functionality
ASP and server-side JavaScript have so much in common that many developers say their differences are small or non-existent. Having learned one, learning the other will take minimal time.Both are scripting languages. This makes them easy to learn. Both are interpreted on the web server. Both provide database connectivity and session management. Both have the ability to call external libraries. Both run in the same process space as the web server and are therefore much faster executing than CGIs. To see just how similar they are, look at the previous ASP and LiveWire tutorials.
Platforms
Netscape server-side JavaScript has the edge in the platform category. Server-side JavaScript runs on many UNIX platforms, OS/2, Novell, and Windows NT. Microsoft ASP only runs on Windows95 and NT. Therefore, if your internet/intranet platform is NT, ASP may be a perfect fit. However, if your internet/intranet uses other platforms, server-side JavaScript is the answer. Server-side JavaScript wins this category for its support of multiple platforms.Languages
Netscape server-side JavaScript, as the name might indicate, is tied to one language, JavaScript. Microsoft ASP currently supports two languages, VBScript and Jscript (JavaScript), and has a mechanism to support others. ASP wins this category for being multilingual.Session Maintenance
The ability to save and retrieve variables per user session is one of the most important features of ASP and Server-side JavaScript. It's one of the big advantages these technologies have over CGIs. Here's a comparison of the types of objects ASP and server-side JavaScript provide:
Type of Object Microsoft ASP Netscape LiveWire Per user Session object Client object Per Application Application object Project object Per Server Server object Server object Per HTTP Request Request object Request object Per HTTP Response Response object N/A Hmmm...it almost seems like one copied the other. ASP and server-side JavaScript tie in this category.
Database Connectivity
Both ASP and server-side JavaScript provide extensive database connectivity. The main difference between the two lies in the way they connect to the database. Microsoft ASP connects to a database via ODBC. Server-side JavaScript also provides connections via ODBC. However, it also allows you to connect via native drivers for Oracle, Sybase, Informix and DB2. Both technologies pool the database connections for better performance and provide access to database cursors. Additionally, they provide for the execution of SQL statements and stored procedures (when using ODBC, availability of these features depends on the implementation of your ODBC driver). This is a close one. I'll give server-side JavaScript a slight edge for its support of native database drivers.Calling External Library
Both ASP and server-side JavaScript enable server-side scripts to call an external library. ASP provides interfaces to C++, Visual Basic and Java. These interfaces enable access to the Session maintenance objects (Request, Response, Session, etc). Server-side JavaScript has interfaces for C, C++ and Java. With server-side JavaScript, server-side LiveConnect can create JavaScript objects that can call Java Classes and Java Classes that can call JavaScript. ASP has a slight edge in this category for its multiple interfaces to the Session management objects. However, the server-side LiveConnect functionality in server-side JavaScript is very powerful.Development Environment
The development environments for these two technologies differ slightly. When developing server-side JavaScript applications, HTML files must be created, then compiled, then installed in the JavaScript Application Manager. In ASP, a virtual directory must be created for the application, then the ASP files must be moved into the appropriate directory. Thus, ASP lacks the compilation step required in Server-side JavaScript.For debugging, server-side JavaScript sports a debugging feature built into the JavaScript Application Manager. This is very useful for debugging SQL statements and other errors. ASP does not currently have a debugging environment, but Microsoft has announced the availability of one in the near future.
Microsoft ASP has a visual development environment for developing ASP applications. It includes wizards for database access and site management tools (FrontPage). Currently, there is no released product for visual development in server-side JavaScript. Although, Netscape has released a beta of Visual JavaScript, which appears to be a power tool.
It's a close call for development environments, but I'll give the edge to ASP for the current availability of its Visual InterDev product.
Conclusion
It's a draw! Both of these technologies allow the developer to create fast, complex, database-driven web sites. Most likely, if you are developing solely in Windows NT, ASP (and IIS) will be your best choice. If you are developing on multiple platforms, then go with server-side JavaScript (and Enterprise Server). Whichever you choose, as long as this rivalry continues, expect to see many more features and enhancements headed your way.