Thursday 19 March 2015

ASP Basic Tutorial

ASP

Active Server Pages

ASP is the server side scripting language. Web application can be created. Virtual directory must be created which is like web server where files are stored in this directory.

Creation of Virtual Directory

1.            Go to My Computer à Interpub folder à wwwroot folder.
2.            Go to File Menu à New à Folder à Give any name to the new folder.
3.            Select it and right click.
4.            Select Properties option à Web Sharing tab and à Click Share this folder.
5.            Select the Read, Execute and Script check boxes and give Ok button.

Important Points
1.            ASP will run in IIS (Internet Information Server).
2.            It needs PWS (Personal Web Server) which is installed in Win95 / Win98.
3.            In Windows NT, it is installed in Windows NT 4.0 Option Pack.
4.            ASP is developed by Microsoft Corporation.
5.            IIS will run only Windows NT 4.0 Option Pack.
6.            PWS will run only in Win95 / Win98.

ASP Rules

1.            ASP coding is written in Notepad.
2.            It also contains HTML tags and VB Scripting statements.
3.            ASP file contains Server Script surrounded by the delimiters (<%….%>).
4.            ASP coding are case sensitive.
5.            Starting tag for ASP is <% @Language=”VBSCRIPT” %>.
6.            For html and VB scripting files must have .html as file extension.
7.            ASP file must have .asp as file extension.
8.            ASP files are executed in Browser (Netscape Navigator or Microsoft Internet Explorer).

Calling ASP file in HTML file

Syntax:

Attributes

Name=”form name”             à To give the variable name to the form.
Action=”asp file location”    à To give the asp file stored location or path.
Method=”post/get”                à To give the type of method for the data to pass.
Post method  à To pass large packs or data.
Get method   à To pass small pack or data.

ASP file location Information

“http://personal web server/virtual directory name/file name.asp or .html”
http (Hyper Text Transfer Protocol)
HTML file à Client                                      ASP file à Web Server

Declaration Statement

Syntax:
Dim ,,…
To declare the variables to store values.

ASP Built-in Objects

1.            Response
2.            Request
3.            Server

1.         Response Object
Syntax:
<% response.write(“message or html tags” & ) %>
To print any message or value of the variable.

2.            Request Object
Syntax:
<% asp variable name = request.form(“html variable name”) %>
To retrieve the values from the client and pass it to web server.

3.         Server Object
Syntax:
<% server.method(“Program ID”) %>
method= CreateObject à Creates an instance of a server component.
Program ID à Specify the type of the object to create.

Programs

File Name 1: Msg.asp

<% @Language="vbscript" %>
<%
            response.write("
Welcome to ASP Page
")
            response.write("Active Server Page")
%>
To execute in browser : http://localhost/612/msg.asp

File Name 2: headloop.asp


Program to print the heading in different levels

<%
            Dim a
            for a = 1 to 6
                  response.write(" Header Levels " & a & "")
            next
%>
html>

File Name 3: Proc1.asp

<%
            sub proc(a,b)
                  response.write(a*b)
            end sub
%>
           

Result of the Calculation = <% call proc(5,5) %>



File Name 4(a):        Test1.html

ASP Pages

Enter name :

File Name 4(b):        Test1.asp

<%@ language="vbscript" %>
<%
            s=request.form("t1")
            y=request.form("b1")

            response.write("")
            response.write("")
            response.write("
")
            response.write("")       
            response.write(s)                
            response.write("
")
            response.write("
")
            response.write("
")
            response.write("
")
%>


File Name 5(a):        Res.html

                        Enter the name        :
            Enter the age            :
            Enter the gender      :
           

File Name 5(b):        Res.asp

<%
            dim na,age,gen

            na=request.form("na")
            age=request.form("age")
            gen=request.form("gen")

            response.write("")
            response.write ("YOUR NAME IS :"+na)
            response.write("")
            response.write ("YOUR AGE IS    :"+age)
            response.write("")
            response.write ("YOUR GENDER IS       :"+gen)
%>

File Name 6(a):        Id.html


get method   --> will display the passing data in address bar
post method --> will display the passing data in the document window


            Enter Username       :
            Enter Password        :
           
           

File Name 6(b):        Id.asp

<% @language="vbscript" %>
<%
            a1=request.form("u")
            a2=request.form("p")
            a3=request.form("b1")

            response.write("")
            response.write("User name :" & a1 & "
")
            response.write("Password   :" & a2)
            response.write("
")
%>

ADO (ActiveX Data Object)
1.            In ADO you can access or can do any changes in data in database table file.
2.            Data can be moved from server to client to do any changes and again return it to the server with the help of RDS (Remote Data Service), which is ADO.
3.            ADO and RDO are automatically installed with Microsoft IIS.

Steps to access database from an ASP page.
1.            Create ADO connection to a database.
2.            Open the database connection.
3.            Create ADO record set.
4.            Open the record set.
5.            Extract the data you need from the record set.
6.            Close the record set.
7.            Close the connection.
8.            Create the DSN (Data Source Name).

SQL Database and Table Creation Steps

1.            To create the database, give “CREATE DATABASE ”.
2.            To create the table, give “CREATE TABLE (col datatype,…)”.
3.            To insert the record, give “INSERT INTO VALUES(value1,…)”.
4.            To View the record, give “SELECT * FROM ”.

To execute SQL statements

1.            Type the command.
2.            Select the command with Shift + Arrow keys.
3.            Give F5 to execute the command.

ADO Connection statement

Syntax:
Set =Server.CreateObject(“Connection Name”)
To connect the database source.
Eg:      Set a=Server.CreateObject(“ADODB.Connection”)

Open the Connection

Syntax:
.Open “dsn=”,””,
To open the connection using dsn name.
Eg:      a.Open “dsn=xyz”,”student”,”csc”

ADO Recordset Statement

Syntax:
Set =Server.CreateObject(“ADODB.Recordset”)
To attach the record set of the database.
Eg:      Set r=Server.CreateObject(“ADODB.Recordset”)

Open the Recordset

Syntax:
.Open “tb name”,
To open the recordset.
Eg:      r.Open “emp”,a

Close the Connection

Syntax:
.Close()
To close the connection.
Eg:      a.Close()

File Name 7(a):        Emp1.html

Enter code     :
Enter name   :
Enter salary   :
                       
                       

File Name 7(b):        Emp1.asp

<% @language ="vbscript" %>
<%
            a2=request.querystring("c")
            a3=request.querystring("n")
            a4=request.querystring("s")
            a5=request.querystring("b")

            set cn=Server.CreateObject("ADODB.connection")
            cn.open "dsn=jeb","student","csc"
            set rs=Server.CreateObject("ADODB.recordset")
            rs.open "emp",cn
            cn.execute "insert into emp values('"&a2&"','"&a3&"','"&a4&"')"
            cn.close()
            response.write("Record updated")
response.write("
Home Page")
%>

Table Creation with Structure in SQL

Create table emp(code int, name varchar(15),salary int)
Select * from emp
 

 
 













File Name 8(a):        Elist.html

Particular Employee Details to View


Enter code                          :
                       
                       

File Name 8(b):        Elist.asp

<%  @language="vbscript"%>
<%
a1=request.form("c")
            set cn=Server.CreateObject("ADODB.connection")
            cn.open "dsn=jeb","student","csc"

            set rs=Server.CreateObject("ADODB.recordset")
            rs.open "emp",cn

            cn.execute "select * from emp"

            response.write("Employee list
")

            while not rs.eof
                        if cint(a1)=rs.fields(0) then
                                    response.write(rs.fields(0) & rs.fields(1) & rs.fields(2) & "
")
            end if
                        rs.movenext
            wend
            cn.close()
response.write("
Home Page")
            %>

File Name 9(a):        Elist1.html

All Employee Details to View



File Name 9(b):        Elist1.asp

<%  @language="vbscript"%>
<%
            a1=request.form("c")
            set cn=Server.CreateObject("ADODB.connection")
            cn.open "dsn=jeb","student","csc"

            set rs=Server.CreateObject("ADODB.recordset")
            rs.open "emp",cn

            cn.execute "select * from emp"
            response.write("

Employee list


")

            while not rs.eof
                        response.write(rs.fields(0) & rs.fields(1) & rs.fields(2) & "
")
                        rs.movenext
wend
            cn.close()
response.write("
Home")
            response.write("
")
%>

FSO (File System Object)
Fill system object will create, modify and view the text files through asp file.

Objects of File

1.            Text file
2.            Folder
3.            Text Stream

FSO File Declaration

Syntax:
Set =Server.CreateObject(“Scripting.FileSystemObject”)
To create the FSO file and store it in a variable.
Eg:      Set f=Server.CreateObject(Scripting.FileSystemObject”)

Methods for FSO


Text File Creation Declaration

Syntax:
Set =Server.CreateTextFile(“path”,[overwrite])
[Overwrite] à It will return true or false. True for file exists. False for file not exists.
Eg:      Set ft=Server.CreateTextFile(“d:\test.txt”,true)

Text File Opening Declaration

Syntax:
Set =.OpenTextFile(“path”)
To open a text file and store it in a variable.
Eg:      Set x=ft.openTextFile(“d:\test.txt”)

Open Text File

Syntax:
.OpenTextFile(“Filename”,iomode,create,format)
To open the text file.
File name à String expression that identifies the filename to open.
io mode  à    It is optional which indicates input / output mode. There are 3 constants.
            1.         For Reading              à        1 (only for reading purpose not for writing)
            2.         For Writing                à        2 (only for writing purpose not for reading)
            3.         For appending          à        8 (only to add contents at the end of the file)
create à        It is a Boolean. True for new file creation, False for not created. Default is false.
format à        It is optional. If omitted the file is opened as ASCII mode.

Eg:      x.OpenTextFile(“d:\test.txt”1)

Close Text File

Syntax:
.Close()

Text Stream Object Methods

1.            Write Method
Syntax:
.Write(“String”)
To write a string in a text file.

2.            Writeline Method
Syntax:
.Writeline(“String”)
To write a sentence in a text file.

3.            Writeblanklines Method
Syntax:
.Writeblanklines(number)
To include blank lines in a text file.

4.            Read Method
Syntax:
.Read(Number of characters)
To read number of characters from a text file.

5.            Readline Method
Syntax:
.Readline()
To read a single line from the text file.

6.            Readall Method
Syntax:
.Readall()
To read all the contents from the text file.

7.            Skip Method
Syntax:
.Skip(Number of characters)
To skip the specified number of characters in an opened text file.

8.            Skipline Method
Syntax:
.Skipline()
To skip a single line in an opened text file.

FSO Progams


File Name 10:           Fso1.asp

To create a text file and read the contents.

<%
            dim i
            set fo=server.createobject("Scripting.FileSystemObject")
            set ft=fo.createtextfile("d:\text.txt",true)

            for i=0 to 5
                        ft.writeline("good morning")
            next
            ft.close()
            set txtfile=fo.opentextfile("d:\text.txt")

            for i=0 to 5
                        response.write txtfile.readline()
                        response.write("
            next
            txtfile.close()
%>

File Name 11:           Fso2.asp

To create file and write contents using

writeline and writeblankline methods.
<%
            set fso=server.createobject("Scripting.FileSystemObject")
            set file=fso.createtextfile("d:\text2.txt")
            file.writeline("Rohan")
            file.writeblanklines(1)
            file.writeline("lakshmi is studying in school")
            file.close()
            response.write("File is created")
            set tf=fso.opentextfile("d:\text2.txt")
            response.write "
"
            response.write ""
            response.write tf.skip(0)
            response.write tf.readall()
            tf.close()
%>

File Name 12:           Fso3.asp

To append the text in an existing text file

<%
            set fso=server.createobject("Scripting.FileSystemObject")
            set file=fso.opentextfile("d:\text2.txt",8)
            file.writeline("csc")
            file.writeline("computer")
            file.close()
            response.write("File is Append")
%>

File Name 13:           Fso4.asp

To display the contents of a file.

<%
            set fso=Server.CreateObject("Scripting.FileSystemObject")
            set file=fso.OpenTextFile("d:\text2.txt",1)
            for i = 1 to 8
                        response.write(file.readLine() & "
")
            next
            file.close()
%>

File Name 14:           Fso5.asp

To create a new file.

<%
            set fso=server.createobject("Scripting.FileSystemObject")
            set file=fso.createtextfile("d:\text3.txt",8,true)
            file.writeline("rose")
            file.writeline("jasmine")
            file.close()
            response.write("file is create")
%>

File Name 15:           Fso6.asp

To create, get, copy, move and delete a file.

<%
            dim i
            set fso=server.createobject("scripting.FileSystemObject")
            set file=fso.createtextfile("d:\text4.txt")
            file.writeline("Rose")
            file.writeline("lily")
            file.writeline("jasmine")
            file.writeline("sunflower")
            file.close()
            set afile=fso.getfile("d:\text4.txt")
            afile.copy("d:\sam.txt")
            afile.move("d:\sam1.txt")
            afile.delete
            response.write"File copied to sam move to sam1 and deleted sam1"
%>

File Name 16:           Fso7.asp

To display contents of the file by using endoffile option.

<%
            set fso=server.createobject("Scripting.FileSystemObject")
            set file=fso.opentextfile("d:\text2.txt")
            while not file.AtEndOfline
                        response.write(file.readline)& "
"
            wend
            file.close()
%>
        


* * *

No comments:

Post a Comment

CakePHP Date Picker

Folder Structure: Controller:   File location - cakephp/src/controller/HomepageController.php namespace App\Controller; use...