Sie sind auf Seite 1von 13

NOTE TO WEB PROGRAMMING LAB

ALL THE PROGRAMS MAKES USE OF XHTML TECHNOLOGY


PLATFORM USED:WINDOWS XP,RED HAT LINUX 9.0
ALL THE PROGRAMS CAN BE EXECUTED IN STANDALONE LINUX MACHINE OR IN WINDOWS USING
TELNET PROTOCOL TO CONNECT TO LINUX SERVER.
IE6 AND ABOVE IS COMPATIBLE WITH ALL THE PROGRAMS. MOZILLA BROWSER INCOMPATIBLE WITH
SOME JAVASCRIPT FUNCTIONS GNOME BROWSER AND OTHER LINUX BROWSERS CAN BE USED AS
ALTERNATE BROWSER.
APACHE TOMCAT, MYSQL, PERL, PHP USED FOR EXECUTING PERL AND
PHP PROGRAMS

NOTE:
METADATA usage is very important as it is strictly XHTML program.
External CSS should be used to give style specifications.
Linux platform both the files should be kept in html folder.
path: /var/www/html
Path of the html should be given as the URL in the browser.
JAVASCRIPT is used with XHTML documents to generate Fibonacci series
The prompt() is a method of the window object, just like alert() or confirm().
prompt("Message", "default value in the text field");
In addition to the "OK" and "Cancel" buttons, a prompt box also has a text field that is employed for
gathering visitor input. JavaScript lets you specify a default text for this text field.
This is optional, that is you can construct a prompt() without specifing the default text. In such cases,
JavaScript displays an ugly "undefined" value in the text field.
The information submitted by the visitor from prompt() can be stored in a variable, just as we had
stored the value returned by confirm().
var name = prompt("What is your name", "Type you name here");
Once we have the value, we can write a customized greeting using document.write() .
var name = prompt("What is your name", "Type you name here");
alert("Hi " + name + "\nHope you are enjoying JavaScript!");
The indexOf() method returns the position of the first occurrence of a specified value in a string.This
method returns -1 if the value to search for never occurs.
stringObject.indexOf(searchvalue,fromindex)

Parameter Description
o Searchvalue Required. Specifies a value to search for
o fromindex Optional. Specifies where to start the search

The indexOf() method is case sensitive!

The charAt() method returns the character at the specified position in a string.
stringObject.charAt(index)
Parameter Description
o index Required. An integer between 0 and the length of the string-1.
The index of the first character is 0, and the index of the last character in a string
called "txt" is txt.length-1.
RegExp Object
A regular expression is an object that describes a pattern of characters.
Regular expressions are used to perform powerful pattern-matching and "search-and-replace"
functions on text.
var txt=new RegExp(pattern,modifiers); or more simply:
var txt=/pattern/modifiers;
pattern specifies the pattern of an expression
modifiers specify if a search should be global, case-sensitive, etc.

Modifiers
F: Firefox, IE: Internet Explorer
Modifiers are used to perform case-insensitive and global searches:

Modifier Description F IE
i Perform case-insensitive matching 1 4
g Perform a global match (find all
matches rather than stopping after
the first match) 1 4
m Perform multiline matching 1 4
Brackets Brackets are used to find a range of characters:

Expression Description F IE
[abc] Find any character between the brackets 1 4
[^abc] Find any character not between the
brackets 1 4
[0-9] Find any digit from 0 to 9 1 4
[a-z] Find any character from lowercase a to
lowercase z 1 4
[A-Z] Find any character from uppercase A to
uppercase Z 1 4
[a-Z] Find any character from lowercase a to
uppercase Z 1 4
[adgk] Find any character in the given set 1 4
[^adgk] Find any character outside the given set 1 4
[red|blue|green] Find any of the alternatives specified 1 4

Metacharacters
Metacharacters are characters with a special meaning:

Metacharacter Description F IE
. Find a single character, except newline or
line terminator 1 4
\w Find a word character 1 4
\W Find a non-word character 1 4
\d Find a digit 1 4
\D Find a non-digit character 1 4
\s Find a whitespace character 1 4
\S Find a non-whitespace character 1 4
\b Find a match at the beginning/end of
a word 1 4
\B Find a match not at the beginning/end of
a word 1 4
\0 Find a NUL character 1 4
\n Find a new line character 1 4
\f Find a form feed character 1 4
\r Find a carriage return character 1 4
\t Find a tab character 1 4
\v Find a vertical tab character 1 4
\xxx Find the character specified by an octal
number xxx 1 4
\xdd Find the character specified by a
hexadecimal number dd 1 4
\uxxxx Find the Unicode character specified by a
hexadecimal number xxxx

NOTE:
The match() method returns the matches when matching a string against a regular
expression.This method returns an array of information, or null if no match is found.
o stringObject.match(regexp)

Parameter Description
o regexp Required. A regular expression object.

This method is used to retrieve the matches when matching a string against a regular
expression.
string.match(regexpression)
The match() method searches the string for the regular expression passed to the method.
The regular expression is made up of a pattern and flags.
The method returns an array containing the matches found in the string.
The floor() method returns the value of a number rounded DOWNWARDS to the nearest
Integer Math.floor(x)

Parameter Description
o x Required. A number

JavaScript can be used to validate data in HTML forms before sending off the content to a server.

Form data that typically are checked by a JavaScript could be:


o has the user left required fields empty?
o has the user entered a valid e-mail address?
o has the user entered a valid date?
o has the user entered text in a numeric field?
NOTE:
Build the pattern in proper way with /^ $/ symbols.
Check for te validation of the USN eg. “1HK06CS001”. Give the I/P in uppercase .
For B part give I/P USN and sem b/n 1-8

PERL PROGRAMS
NOTE:
Linux platform perl files should be kept in cgi-bin folder.
Path:/var/www/cgi-bin.
Perl file extension should be filename.pl
Do the change mode like chmod 777 filename.pl and run like perl filename.pl
Linux platform html files should be kept in html folder.
path: /var/www/html.
Html file extension should be filename.html
Path of the html should be given as the URL in the browser.
http://localhost/foldername/filename.html
If only perl program is there URL should be
http://localhost/cgi-bin/foldername/filename.pl

Line 1 of this program is a special line that tells the system that this is a Perl program:
#!/usr/local/bin/perl

Let's break this line down, one part at a time:


The first character in the line, the # character, is the Perl comment character. It tells the
system that this line is not an executable instruction.

The ! character is a special character; it indicates what type of program this is. (You don't
need to worry about the details of what the ! character does. All you have to do is
remember to include it.)

The path /usr/local/bin/perl is the location of the Perl executable on your system.
This executable interprets your program; in other words, it figures out what you want to
do and then does it. Because the Perl executable has the job of interpreting Perl
instructions, it usually is called the Perl interpreter.

Line 2 of this program is


use CGI':standard';

The use CGI ':standard'; statement tells Perl that you want to use the CGI.pm module in your
program. This will load the module and make a set of CGI functions available for your code.

Line 3 of this program is


print "content type: text/html \n\n";

The new line tells the webserver that the script's output is of the type "text/html". So, the webserver
knows what value the Content-type header should carry this time and thus happily performs its duty:
Much of the most crucial information needed by CGI applications is made available via
UNIX environment variables. Programs can access this information as they would any
environment variable (e.g., via the %ENV associative array in Perl).

print "Server name :",$ENV{'SERVER_NAME'},"<br>";

$c=param('com');

The most important CGI function is param(). Call it with the name of a form item, and a
list of all the values of that form item will be returned. (If you ask for a scalar, you'll only
get the first value, no matter how many there are in the list.)

If you call param() without giving it the name of a form item, it will return a list of all the
form items that are available. This form of param() is the core of our backatcha script:

system() function execute a system shell command.

carp - warn of errors (from perspective of caller)

After creating the HTTP header, most CGI scripts will start writing out an HTML document. The
start_html() routine creates the top of the page, along with a lot of optional information that controls the
page's appearance and behavior

localtime(time); This module's default exports override the core localtime() function,
replacing it with a version that returns "Time::tm" objects. namely sec, min, hour, mday, mon,
year, wday, yday, and isdst.

MySQL Database
What is MySQL?
MySQL is a database. The data in MySQL is stored in database objects called tables.
A table is a collections of related data entries and it consists of columns and rows.
Databases are useful when storing information categorically. A company may have a
database with the following tables: "Employees", "Products", "Customers" and "Orders".

1. MySQL
Mysql -An open source, fast, easy to use. -ANSI std. -can easily be integrated
into perl programs by using perl DBI-Data Base Interface module. DBI is an API that allows perl to
connect to & query number of SQL databases. Ex. MySQL, mSQL,PostgreSQL,Oracle, Sybase, &
Informix.

2. MySQL
o Make a connection to MySQL server as the root MySQL user.
o $ mysql –u root
o * MySQL root is used to administer to
o MySQL server only.
o Check out whether server is running in background.
o We can start the server manually.
3. MySQL
o /etc/init.d/mysqld start
o Now you can able to connect as
o $ mysql –u root
o You will get mysql prompt
o Mysql> show databases;
o Mysql> CREATE DATABASES;
o Case insensitive
o Usually we use uppercase for key words.
o Database is container for tables
o Table is collection of rows
o Row is collection of fields

4. Mysql
o Mysql> USE database-name;
o Mysql> CREATE TABLE studinfo(
o lastname CHAR(20),
o firstname CHAR(20),
o age INT);
o DROP table name;

Ls –l /var/lib/mysql
o We get all database directories
o Ls –l /var/lib/mysql/people
o We get table files in that directories

5. Mysql Data types


1.TINYINT -128 to 127(signed) o or 0- 255(unsigned)
2. SMALLINT -32768 to 32767(signed) or o o to 65535(unsigned)
3.MEDIUMINT -8388608 to 8388607 or o 0 to 16777215
4. INT or INTEGER -2147483648 to 214783647 or 0 to 4294967295
5. BIGINT = 0 to 18446744073709551615 or -9223372036854775808 to 9223372036854775807

6. MySQL data types


o FLOAT
o Double
o REAL(same as double)
o DECIMAL
o NUMERIC(same as decimal)

7. MySQL data types for DATE


o Date YYYY-MM-DD
o DATETIME YYYY-MM-DD HH:MM;SS
o TIMESTAMP YYYYMMDDHHMMSS, YYMMDDHHMMSS
o TIME HH:MM:SS
o YEAR YYYY or YY
8. Other character data types
o Have BLOB– Binary Large Object that could hold a variable amount of data.
o BLOBs are case sensitive.
o VARCHAR variable length string upto 255 chars
o TINYBLOB – Maximum length 255 chars
o TEXT
o BLOB max. length 65535 characters
o MEDIUMBLOB OR MEDIUMTEXT –max. 16777215 characters
o LONGBLOB OR LONGTEXT – 4294967295 characters

9. Commands
o Describe command gives the information about the table.
o Describe book;
o Insert inserts the values into table.
o Insert into book values(‘ ‘,’ ‘);
o Insert into book(name, usn) values(‘hhh’,’777’);
o Select selects the records.
o Select * from book order by name;
o Select * from book order by name desc;

10. Commands
o Update –to change the value of an existing record.
o Update book set usn=‘777’ where name=“ccc”;
o Update book set sal=sal+10777 where name=“ccc”;

Delete command is used to delete the record from table.

11. Some administrative details


o Root is MySQL user.
o Change user name to something
o This can be done changing the databases and granting the previleges to new user.
o Use mysql(existing databases)
o Grant select, delete, update, insert on people.* to apache@localhost identified
by ‘password’
o Here apache is user
o Password is password .
o People is new database .

12. Setting password is necessary for the first time


o Now use mysql server as follows
o $mysql –u apache –p
o Enter the password next.
o Change the database using USE people.
o Show tables;
13. DATABASE INDEPENDENT INTERFACE
o DBI enables one to write programs to automate database maintenance & to
write other scripts to interface with MySQL
o DBI is perl module that provides methods to manipulate SQL databases.
o With DBI one can connect to a database within a perl script & issue all kinds
of queries, including SELECT, INSERT, DELETE.
o PERL program can be to written to connect to database.
o CGI script can be written to conect and display database on the browser.

14. Simple perl program


o #!/usr/bin/perl –w
o use strict;
o use DBI;
o my $dbh=DBI->
o connect(‘DBI:mysql:people’,’apache’,’password’) or die “cannot connect “ .
DBI->errstr() ;
o print “success connected”;
o $dbh->disconnect();

15. Use DBI method tells perl to use the DBI module. This allows to use all
methods for sql queries in this class.
o Connect - causes perl to connect to the MySQL database using the perl DBI
class.
o The first argument to connect method is the database to which you want to
connect.

16. Ex. DBI:mysql:people


o DBI module server name
o database
o ‘ apache’ – user
o ‘ password’ – password.
o If sucsessful returns database handler.
o -disconnect to shut down the connection.

17.
o #! /usr/bin/perl –w
o use DBI;
o use strict;
o my $d;
o $d=DBI->connect(‘DBI:mysql:people’,
o ’ apache’,’password’) or die “cannot connect”.DBI->errstr();
o my $sh=$d->prepare(‘SELECT * from book’) or die “can’t prepare”.
o $d->errstr();
o $sh->execute() or die “can’t execute”.
o $sh->errstr();
18.
o my($x,$y,$z);
o While(($x,$y,$z)=$sh->fetchrow())
o{
o Print “$x $y $z ”;
o}
o $sh->finish();
o $d->disconnect();

19.
o Prepare method prepares the statement & returns a statement handle object
that can be used to execute the query by calling execute method.
o One more perl example …

20.
o #!/usr/bin/perl –w
o use strict; use DBI;
o my
o $dbh=DBI->connect(‘DBI:mysql:people’,
o ’ apache’,’password’) or die “cannot connect “ . DBI->errstr() ;
o print “success connected”;
o Print “ ”,”-”x40;
o Print “enter name=“; Chomp($x=<STDIN>);
o Print “enter address=“;Chomp($y=<STDIN>);
o Print “enter author=“; Chomp($z=<STDIN>);

21.
o my $sh=$d->prepare(‘INSERT INTO book(fname,lname,auth) values(?,?,?)’)
or die “can’t prepare”.
o $d->errstr();
o $sh->execute($x,$y,$z) or die “can’t execute”. $sh->errstr();
o Print “ ”,”-”x40;
o Print “record inserted”;
o $sh->finish();
o $d->disconnect();

22. Table Joins


o Two or more tables can be joined to complete data.
o Ex. Tables 1. book 2. address.
o SELECT address.city FROM address, book where book.auth =“LAMP” AND
address.name=book.name;

23. Select address.name, address.zip form address, book where address.age<=50


and address.name=book.auth and address.lanme=book.lname order by book.auth;

24. Loading and dumping databases


o Can load a database or otherwise execute SQL commands from a file.
o Simply put the commands or databases into a file ex. Mystuff.sql and load it
in with this command.
o $ mysql people < mystuff.sql
o We can also dump out a database into a file with this command.
o $mysqldump people < entiredb.sql;

Setting a cookie
Transfer of Web pages follows the HyperText Transfer Protocol (HTTP). Regardless of cookies, browsers
request a page from web servers by sending them a usually short text called HTTP request. For example, to
access the page http://www.example.org/index.html, browsers connect to the server www.example.org
sending it a request that looks like the following one

GET /index.html HTTP/1.1


Host: www.example.org

browser → server

The server replies by sending the requested page preceded by a similar packet of text, called
'HTTP response'. This packet may contain lines requesting the browser to store cookies:

HTTP/1.1 200 OK
Content-type: text/html
Set-Cookie: name=value
(content of page)

browser ← server

The server sends the line Set-Cookie only if the server wishes the browser to store a cookie.
Set-Cookie is a request for the browser to store the string name=value and send it back in all
future requests to the server. If the browser supports cookies and cookies are enabled, every
subsequent page request to the same server will include the cookie. For example, the browser
requests the page http://www.example.org/spec.html by sending the server www.example.org a
request like the following:

GET /spec.html HTTP/1.1


Host: www.example.org
Cookie: name=value
Accept: */*

browser → server

This is a request for another page from the same server, and differs from the first one above because it
contains the string that the server has previously sent to the browser. This way, the server knows that this
request is related to the previous one. The server answers by sending the requested page, possibly adding
other cookies as well.

The value of a cookie can be modified by the server by sending a new Set-Cookie:
name=newvalue line in response of a page request. The browser then replaces the old value with
the new one.
The term "cookie crumb" is sometimes used to refer to the name-value pair.[11] This is not the same as
breadcrumb web navigation, which is the technique of showing in each page the list of pages the user has
previously visited; this technique may however be implemented using cookies.

The Set-Cookie line is typically not created by the base HTTP server but by a CGI program. The basic HTTP
server facility (e.g. Apache) just sends the result of the program (a document preceded by the header
containing the cookies) to the browser. Cookies can also be set by JavaScript or similar scripts running within
the browser. In JavaScript, the object document.cookie is used for this purpose. For example, the instruction
document.cookie = "temperature=20" creates a cookie of name temperature and value 20.

Cookie attributes
Beside the name/value pair, a cookie may also contain an expiration date, a path, a domain name, and
whether the cookie is intended only for encrypted connections. RFC 2965 also specifies that cookies must have
a mandatory version number, but this is usually omitted. These pieces of data follow the name=newvalue pair
and are separated by semicolons. For example, a cookie can be created by the server by sending a line Set-
Cookie: name=newvalue; expires=date; path=/; domain=.example.org.

Example of an HTTP response from google.com, which sets a cookie with attributes. The domain and path tell
the browser that the cookie has to be sent back to the server when requesting URLs of a given domain and
path. If not specified, they default to the domain and path of the object that was requested. As a result, the
domain and path strings may tell the browser to send the cookie when it normally would not. For security
reasons, the cookie is accepted only if the server is a member of the domain specified by the domain string.

Cookies are actually identified by the combination of their name, domain, and path, as opposed to only their
name (the original Netscape specification considers only their name and path). In other words, same name but
different domains or paths identify different cookies with possibly different values. As a result, cookie values
are changed only if a new value is given for the same name, domain, and path.

The expiration date tells the browser when to delete the cookie. If no expiration date is provided, the cookie is
deleted at the end of the user session, that is, when the user quits the browser. As a result, specifying an
expiration date is a means for making cookies survive across browser sessions. For this reason, cookies that
have an expiration date are called persistent.

About PHP
What is PHP?
PHP stands for PHP: Hypertext Preprocessor
PHP is a server-side scripting language, like ASP
PHP scripts are executed on the server
PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL,
Generic ODBC, etc.)
PHP is an open source software
PHP is free to download and use
What is a PHP File?
PHP files can contain text, HTML tags and scripts
PHP files are returned to the browser as plain HTML
PHP files have a file extension of ".php", ".php3", or ".phtml"

What is MySQL?
MySQL is a database server
MySQL is ideal for both small and large applications
MySQL supports standard SQL
MySQL compiles on a number of platforms
MySQL is free to download and use

PHP + MySQL
PHP combined with MySQL are cross-platform (you can develop in Windows and serve on a
Unix platform)
Why PHP?
PHP runs on different platforms (Windows, Linux, Unix, etc.)
PHP is compatible with almost all servers used today (Apache, IIS, etc.)
PHP is FREE to download from the official PHP resource: www.php.net
PHP is easy to learn and runs efficiently on the server side

Basic PHP Syntax


A PHP scripting block always starts with <?php and ends with ?>. A PHP scripting
block can be placed anywhere in the document.
On servers with shorthand support enabled you can start a scripting block with <? and
end with ?>.
For maximum compatibility, we recommend that you use the standard form (<?php)
rather than the shorthand form.

<?php


?>

A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting
code. Below, we have an example of a simple PHP script which sends the text "Hello World" to the
browser:

<html>
<body>
<?php
echo "Hello World";
?>
</body>
</html>
Each code line in PHP must end with a semicolon. The semicolon is a separator and is
used to distinguish one set of instructions from another.
There are two basic statements to output text with PHP: echo and print. In the example
above we have used the echo statement to output the text "Hello World".
Note: The file must have a .php extension. If the file has a .html extension, the PHP
code will not be executed.
Comments in PHP
In PHP, we use // to make a single-line comment or /* and */ to make a large comment
block.
<html>
<body>
<?php
//This is a comment
/*
This is
a comment
block
*/
?>
</body>
</html>

Das könnte Ihnen auch gefallen