WD Notes 2 (A)
WD Notes 2 (A)
END TERM EXAMINATION 2011 STUDY MATERIAL COURSE: BBA (CAM) Unit: II (Introduction to Active Server Pages)
Subject: Web Designing and Development PART B: [Introduction to Active Server Pages] 1. Introduction to Active Server Pages:
Active Server Pages (ASP) is a server-side scripting technology that can be used to create dynamic and interactive Web applications. An ASP page is an HTML page that contains serverside scripts that are processed by the Web server before being sent to the user's browser. You can combine ASP with Extensible Markup Language (XML), Component Object Model (COM), and Hypertext Markup Language (HTML) to create powerful interactive Web sites. Active Server Pages (ASP) is a Microsoft technology that allows webmasters to create dynamic content. ASP is not a stand-alone programming language. An ASP script uses JavaScript or Visual Basic scripting language to generate dynamic pages on the server side. Active Server Pages were introduced by Microsoft in 1996 as a downloadable feature of Internet Information Server 3.0. The concept is pretty simple: an Active Server Page allows code written in the JavaScript or VBScript languages to be embedded within the HTML tags of a Web page and executed on the Web server. There are great advantages to this, not the least of which is security. Since your code is executed on the Web server, only HTML tags are sent to the browser. The result is that the ASP code is invisible to the end user.
Server-side scripts run when a browser requests an .asp file from the Web server. ASP is called by the Web server, which processes the requested file from top to bottom and executes any script commands. It then formats a standard Web page and sends it to the browser.
It is possible to extend your ASP scripts using COM components and XML. COM extends your scripting capabilities by providing a compact, reusable, and secure means of gaining access to information. You can call components from any script or programming language that supports Automation. XML is a meta-markup language that provides a format to describe structured data by using a set of tags.
ASP and conventional HTML work in almost the same way. One major difference between ASP and conventional HTML is that ASP is dynamic. This is because with ASP, scripting code runs on the server and produces HTML output, which is then sent to the browser for display. When a browser requests an ASP file from the Web server, the ASP interpreter reads through the ASP file, executes any of the ASP commands contained within and sends the resulting HTML to the browser. This means that the same ASP page might produce HTML that appears in the browser differently each time it is requested. An ASP file can contain any combination of HTML and script. Since ASP pages produce standard HTML, they require no specific browser.
Dynamically edit, change, or add any content of a Web page Respond to user queries or data submitted from HTML forms Access any data or databases and return the results to a browser Customize a Web page to make it more useful for individual users The advantages of using ASP instead of CGI and Perl, are those of simplicity and speed Provide security - since ASP code cannot be viewed from the browser Clever ASP programming can minimize the network traffic
2. Inbuilt data caching module: ASP.NET has a data caching module to decide on which data on the ASP page should be cached and when to empty this cache and when to re-query.
3. Support for Tools: With ASP you can drag and drop your web controls just like how it is done in Visual Basic controls. All you have to do is just double click to write a server program for the control. 4. Ease of Use: ASP pages are just HTML pages with ASP programs encapsulated between <% and %> tags. To run an asp file you just have to place it into a directory in your server and run it provide you have proper authorization. All you have to do to make some changes is just edit your asp file and the modification will be executed. 5. Flexibility: With ASP you can code your scripts in both VBScript and Microsoft Jscript. There are also existing modules to support languages like Python, perl along with solutions to incorporate other languages. So even if you are new to ASP you can use your previous knowledge in other programming languages like VB and JavaScript while migrating to ASP. This makes it easier to learn ASP even with just html knowledge.
Disadvantages of ASP
1. Higher costs: To process asp pages you need to install IIS on a Windows platform server. You have to buy MS-SQL to connect to your database in ASP. And you need to add some components as there are no inbuilt features like in PHP. All these activities will cost you money. 2. Lower Speed: ASP is slower than PHP, as it is based on COM architecture that is actually an overhead to the server. 3. Not Platform Compatible: ASP does not have platform compatibility like PHP. This is because it is mostly dependent on Microsoft products and needs a Windows platform along with an ASP-Apache installation at the server side. 4. Needs VB knowledge: The syntax of ASP has a lot of similarity with that of Visual Basic. Hence some VB knowledge is needed to program is ASP.
Therefore, ASP is one of the best options for e-business software development. At the same time we cannot rule out the other languages completely as they have their own advantages.
5. VBScript is the default scripting language which is used for writing ASP. Though VBScript is the most common language, a few other scripting languages like Java Script and Perl Script are also used. Any web page containing ASP Application Development cannot run by just viewing the web page in a particular browser. The web page must be requested and demanded through a server. ASP permits to modify a web page dynamically or insert any content in a web page. The major feature and advantage of using the Active Server Page is its low maintenance. An ASPdriven site is much simpler and less complicated than other sites. ASP codlings provide safety and security since the code cannot be observed from the browser.
Erase
Filter
Join
LBound
ReDim
the Preserve keyword is used, existing elements will not be destroyed in the process. ReDim Preserve aField(nCount + 10) Takes a string argument and creates an array by "splitting" the string into multiple elements. Strings are split based on a "delimitter" which is passed as the second argument. aName = Split("joe,bill,john", ",") Return the upper bound of the array meaning the last possible array index. This is often used to get the size of an array (which in turn can be used in For...Next loops to iterate over the elements) nLast = UBound(aArray)
Split
UBound
Dim
Function
Sub
2. Comparison operators:
8
Comparison operators are used to compare two values and make a decision. Comparison operators return a true or false value. Operator = < > <> Function Equal to Less than Greater than Not equal to Example 9 = 10 aNumber = 5, aNumber < 10 aNumber = 5, aNumber > 10 5 <> 10 Result False True False True
3. Logical operators
Logical operators take the result produced by comparison operators and compares them to create new true or false values. For example, true AND false = false. Operator AND OR Not Function True if both are true, otherwise false Example 9 = 10 AND 10 = 10 Result False True False
True if at least one is true, otherwise false 9 = 10 OR 10 = 10 Reverses truth value 5=5
4. String operators
There is only one string operator and that is the concatenation operator "&". This operator is used to combine text strings.
Example: <% Dim stringOne = "This is " Dim stringTwo = "a text string"
Response.Write(stringOne & stringTwo & "<br />") Response.Write("ASP stands for " & "Active " & " Server " & " Pages ") %>
Output: This is a text string ASP stands for Active Server Pages
10
Event-driven programming allows developers to create code that will execute when a particular event occurs. For example, in the case of ASP, a particular segment of code can be executed when a page is either loaded or unloaded, or a control on a page is clicked. In contrast, ASP uses a linear code-processing model: each line of ASP code is mingled with static HTML and is processed in the order in which it appears in the ASP file. Event-driven programming makes ASP programming more closely parallel Windows programming. The advantages come from the fact that developers only need to write code to respond to events, and they can extend their knowledge of event-driven programming from the development of Windows desktop applications to Web applications. Another advantage of the event-driven programming model is that it allows for separation of the code that manipulates the data from the code that presents the data to the user. For example, ASP allows developers to use a mechanism called "code behind" to separate the logic of the Web application (typically written in VB.NET or C#) from the presentation (typically formatted by HTML). By separating the logic into a VB or C# (pronounced "C-sharp") code file (*.aspx.vb or *.aspx.cs) and a presentation code file (*.aspx), ASP allows multiple pages to use the same code, and makes maintenance easier because developers need not search through HTML statements in order to fix a programming logic problem. The steps are fairly straightforward:
An event happens The event is detected by the application The application responds to the event
The Windows OS will automatically detect when an event has occurred, like a mouse move, mouse click, form load, etc. If the OS doesn't find a VBA procedure relating to that event it will just go with its default behavior for that event. The programmer does not need to determine when a particular event happens as Windows does that for you. If you program a procedure to respond to that particular event, then it will override the default behavior for that event. Therefore, the programmer only needs to code those events where something other than the default behavior should occur. For example, the default behavior if a button is clicked is that the button gains focus. You only need to code any other tasks you wish to be performed when this event occurs.
11
Criticism and Best Practice: Event-driven programming is widely used in graphical user interfaces because it has been adopted by most commercial widget toolkits as the model for interaction. The design of those toolkits has been criticized for promoting an over-simplified model of event-action, leading programmers to create error prone, difficult to extend and excessively complex application code: Such an approach is fertile ground for bugs for at least three reasons:
1.
2.
It is not thread-safe. It can lead to writing code within the event handler for each possible value of
various values in the program, making the source code hard to understand.
3.
effects.
In places where the event code changes variables shared throughout the program,
it can require the programmer to write convoluted algorithms to avoid unwanted ripple
12
As a security precaution, always encode querystring input (or any request data) before using it. A general method of encoding data is to use Server.HTMLEncode. Another method is to write a short function that tests request data for invalid characters. More information can be found by reading chapter 12 of Writing Secure Code. Syntax:
Request.QueryString(variable)[(index)|.Count] Parameters:
variable Specifies the name of the variable in the HTTP query string to retrieve. index An optional parameter that enables you to retrieve one of multiple values forvariable. It can be any integer value in the range 1 to Request.QueryString(variable).Count.
You can use an iterator through a collection using a For Each item in ... Next loop. There is one mandatory argument. Variable The Variable specifies the name of the variable in the query string whose value is to be retrieved. There is one optional argument. Index The optional Index argument is used when the Variable argument has more than one value. It is an integer, ranging from 1 to Request.QueryString(Variable).Count, which is used to select values from the Variable. Count is the total number of multiple values.
13
Code: -----------------------File1.asp---------------------<HTML> <HEAD> </HEAD> <BODY> <A HREF="File2.asp?language=ASP&type=general">Query sample</A> </BODY> </HTML> -----------------------File2.asp---------------------<% For Each item In Request.QueryString Response.Write(item & " = " & Request.QueryString(item) & VbCrLf) Next %> Output: language = ASP type = general
14
An object in ASP is created by passing a name string to theServer.CreateObject function(actually referred to as a method). The string to create a Message object is "CDO.Message". We will be creating a CDO.Message object in this example. Note: Because objects are special there is a special way that you create and destroy them using the Set keyword. These areas are marked in red in the example below. ASP Code:
<% Dim myObject Set myObject = Server.CreateObject("CDO.Message")
'You must Set your objects to "nothing" to free up the 'the computer memory that was allocated to it Set myObject = nothing %>
Objects are a collection of related things that are combined into this blob of programming goo that can be created and destroyed whenever we may need it. For example say that you wanted to make an object that allowed you to send an email... Well there are certain things all emails have: To, From, CC, Subject, etc. This list of variables that are common to every email would be quite tiresome to have to create for every email we sent. Wouldn't it be nice if we could create some sort of Uber-Variable (Object) that would group all these smaller variables into one thing?
15
16