You are on page 1of 19

Shri Vaishnav Vidyapeeth Vishwavidyalaya

Shri Vaishnav Institute of Information Technology

Lab Assignment

SUBJECT NAME: web development lab -2


SUBJECT NAME: btit-407

SUBMITTED By: - SUBMITTED To: -


Avdhesh pratap singh ranawat Mahesh arse
Enrollment No.: 20100btit07697
B Tech IT | 2nd Year | 3rd Semester
Section: A
1. Write a PHP script to display Welcome message. <?php

echo "Welcome to PHP Programming !!!!"."<br>";

echo "This", " string", " was", " made", " with multiple parameters.";

echo ("Welcome to PHP Programming !!!!"."<br>");

print("Welcome to PHP"."<br>");

print "Hello World";

?>

OUTPUT:

Welcome to PHP Programming!!!!

This string was made with multiple parameters. Welcome to PHP Programming!!!!

Welcome to PHP

Hello World

2. Write a PHP script to demonstrate arithmetic operators, comparison operator, and logical operator.

<?php

$x=100;

$y=60;

echo "The sum of x and y is : ". ($x+$y) ."<br />";

echo "The difference between x and y is: ". ($x-$y)."<br />";

echo "Multiplication of x and y: ". ($x*$y)."<br />";

echo "Division of x and y: ". ($x/$y)."<br />";

echo "Modulus of x and y: “. ($x%$y)."<br />";

?>

Output:

The sum of x and y is: 160

The difference between x and y is: 40

Multiplication of x and y: 6000

Division of x and y: 1.6666666666667

Modulus of x and y:40

<?php

$a = 80;

$b = 50;

$c = "80";

var_dump($a == $c) + "\n";

var_dump($a != $b) + "\n";

var_dump($a <> $b) + "\n";


var_dump($a === $c) + "\n";

var_dump($a !== $c) + "\n";

var_dump($a < $b) + "\n";

var_dump($a > $b) + "\n";

var_dump($a <= $b) + "\n";

var_dump($a >= $b);

?>

Output:

bool(true)

bool(true)

bool(true)

bool(false)

bool(true)

bool(false)

bool(true)

bool(false)

bool(true)

<?php

$x = 50;

$y = 30;

if ($x == 50 and $y == 30)

echo "and Success \n";

if ($x == 50 or $y == 20)

echo "or Success \n";

if ($x == 50 xor $y == 20)

echo "xor Success \n";

if ($x == 50 && $y == 30)

echo "&& Success \n";

if ($x == 50 || $y == 20)

echo "|| Success \n";


if (!$z)

echo "! Success \n";

?>

Output:

and Success

or Success

xor Success

&& Success

|| Success

! Success

3. Write PHP Script to print Fibonacci series.

<?php

$num = 0;

$n1 = 0;

$n2 = 1;

echo "<h3>Fibonacci series for first 12 numbers: </h3>";

echo "\n";

echo $n1.' '.$n2.' ';

while ($num < 10 )

$n3 = $n2 + $n1;

echo $n3.' ';

$n1 = $n2;

$n2 = $n3;

$num = $num + 1;

?>

OUTPUT:

0 1 1 2 3 5 8 13 21 34 55 89

4. Write PHP script to demonstrate Variable function

<?php

function add($x, $y){

echo $x+$y;

$var="add";
$var(10,20);

?>

Output:

30

5. Write PHP script to demonstrate string function.

<?php

$str1="PHP";

$str2="Programming";

echo "Origina : $str1 and $str2<br>";

$l1=strlen($str1);

$l2=strlen($str2);

echo "Length : $l1 and $l2<br>";

$add=$str1+$str2;

echo "Addition : $add <br>";

$ord=ord($add);

echo "Ord of $add : $ord <br>";

$chr=chr($ord);

echo "chr of $ord : $chr <br>";

ec4seho "strlen of $chr : ".strlen($chr)."<br>";

?>

6. Write PHP script to demonstrate Array functions.

<?php

$season=array("summer","winter","spring","autumn");

echo "Season are: $season[0], $season[1], $season[2] and $season[3]";

echo count($season);

sort($season);

foreach($season as $s)

echo "$s<br />";

}
reverseseason=array_reverse($season);

foreach($reverseseason as $s)

echo "$s<br />";

$key=array_search("spring”, $season);

echo $key;

?>

7. Create student registration form using text box, check box, radio button, select, submit button. And
display user inserted value in new PHP page.

Registration Page Script

<html>

<head>

<title>Registration</title>

<style>

.main

width:30%;

margin:2em auto;

border:2px solid black;

padding:1em;

.main input[type="submit"]

width:95%;

border:1px solid black;

padding:.5em;

margin:.5em;

.main input[type="password"]

width:95%;

border:1px solid black;

padding:.5em;

margin:.5em;

.main input[type="text"],.main input[type="email"]


{

width:45%;

border:1px solid black;

padding:.5em;

margin:.5em;

</style>

</head>

<body>

<div class="main">

<form method="post" action="welcome.php">

<h2>Student Registration</h2>

<input type="text" name="fname" placeholder="First Name">

<input type="text" name="lname" placeholder="Last Name">

<input type="email" name="email" placeholder="Email">

<input type="text" name="mobile" placeholder="Mobile">

<input type="text" name="city" placeholder="City"><br>

<hr>

Gender<br>

<input type="radio" name="gender" value="Male">Male<br>

<input type="radio" name="gender" value="female">Female<br>

<hr>

Hobbies<br>

<input type="checkbox" name="hobby[]" value="Cricket">Cricket<br>

<input type="checkbox" name="hobby[]" value="Football">Football<br>

<input type="checkbox" name="hobby[ ]" value="Chess">Chess<br>

<hr>

<input type="Password" name="pass" placeholder="Password"><br>

<input type="submit" name="submit" value="Register">

</form>

</div>

</body>

</html>
Welcome Page Script:

<?php

if(isset($_POST['submit']))

$fn=$_POST['fname'];

$ln=$_POST['lname'];

$em=$_POST['email'];

$mob=$_POST['mobile'];

$city=$_POST['city'];

$gender=$_POST['gender'];

$hobbies=$_POST['hobby'];

$pass=$_POST['pass'];

echo "First name : $fn <br><br>";

echo "Last name : $ln <br><br>";

echo "Email : $em <br><br>";

echo "Mobile : $mob <br><br>";

echo "City : $city <br><br>";

echo "Gender : $gender <br><br>";

echo "<h3>Hobbies</h3>";

$i=0;

while($i<sizeof($hobbies))

echo $hobbies[$i]."<br>";

$i++;

?>

8. Write two different PHP script to demonstrate passing variables through a URL.

Script For Index.Php File

<?php

$fname="Sarjil";

$lname="Shaikh";

?>
<a href="retrive.php?First_name=<?php echo $fname;?>&last_name=<?php echo $lname;?>">

Click here to Pass Variable through URL

</a>

Script For Retrive.Php File

<?php

echo "First Name : ".$_GET['First_name']."<br>";

echo "Last Name : ".$_GET['last_name'];

?>

9. Write PHP script to demonstrate passing variables with cookies.

<html>

<head>

<title>Cookie Demo</title>

</head>

<body>

<form method="post">

Enter Your Name:

<input type="text" name="fname" value="<?php echo $_COOKIE['myname'];?>">

<input type="submit" value="Create Cookie" name="submit">

</form>

</body>

</html>

<?php

if(isset($_POST['submit']))

$name=$_POST['fname'];

setcookie('myname',$name,time()+100,"/","",0);

echo "Your name is : ".$_COOKIE['myname'];

if(!isset($_COOKIE['myname']))

echo "failed to create cookie";

?>

10. Write an example of Error-handling using exceptions.


<?php

function checkNumber($num) {

if($num>=1) {

throw new Exception("Value must be less than 1");

return true;

try {

checkNumber(5);

echo 'If you see this text, the passed value is less than 1';

catch (Exception $e) {

echo 'Exception Message: ' .$e->getMessage();

?>

Output:

Exception Message: Value must be less than 1

11. Write a PHP script to connect MySQL server from your website.

<?php

$servername = "localhost";

$username = "yourusername";

$password = "yourpassword “;

$conn = new mysqli($servername, $username, $password);

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

echo "Connected successfully";

?>

12. Write a program to read customer information like cust_no, cust_name, Item_purchase, and mob_no,
from customer table and display all these information in table format on output screen.

13. Write a program to read employee information like emp_no, emp_name, designation and salary from
EMP table and display all this information using table format.

14. Create a dynamic web site using PHP and MySQL.

Index.Php

<?php

$myFileName = "filename.html"; // or .php file or .css file genrated


$fh = fopen($myFileName, 'w'); // or die("error");

$stringData = "

<?php include('config.php');?>

<?php include('header.php');?>

<?php include('sidebarmenu.php');?>

<p>

<ul>

<li>how to make a dynamic web pages using php & mysql</li>

<li>php generate html page</li>

<li>how to create dynamic pages in php</li>

<li>how to generate a html page dynamically using php</li>

<li>php dynamic page content</li>

<li>php dynamic website source code</li>

<li>using php to generate html</li>

<li>how to create a dynamic website in php step by step pdf</li>

</ul>

</p>

<?php include('footer.php');?>

";

fwrite($fh, $stringData);

?>

Output : Filename.Html

Automatically This index.php file run to create a filename.html or filename.php genrated dynamically.

<?php include('config.php');?>

<?php include('header.php');?>

<?php include('sidebarmenu.php');?>

<p>

<ul>

<li>how to make a dynamic web pages using php & mysql</li>

<li>php generate html page</li>

<li>how to create dynamic pages in php</li>

<li>how to generate a html page dynamically using php</li>

<li>php dynamic page content</li>

<li>php dynamic website source code</li>

<li>using php to generate html</li>

<li>how to create a dynamic website in php step by step pdf</li>


</ul>

</p>

<?php include('footer.php');?>

Dynamically Generate Simple Links

PHP script to dynamically generate simple links to the files present in current directory

<?php

$dir_open = opendir('.');

while(false !== ($fname = readdir($dir_open))){

if($fname != "." && $fname != ".."){

$link = "<a href='./$fname'> $fname </a><br />";

echo $link;

closedir($dir_open);

?>

<? //Generate simple text file on the fly using php

header("Content-type: text/plain");

header("Content-Disposition: attachment; filename=myfile.txt");

print "how to make a dynamic web pages using php & mysql\n";

print $result;

?>

15. Write a program for JSP scriptlet tag that prints the user name

File: index.html

<html>

<body>

<form action="welcome.jsp">

<input type="text" name="uname">

<input type="submit" value="go"><br/>

</form>

</body>

</html>
File: welcome.jsp

<html>

<body>

<%

String name=request.getParameter("uname");

out.print("welcome "+name);

%>

</form>

</body>

</html>

16. Write a program for JSP expression tag that prints current time

To display the current time, we have used the getTime() method of Calendar class. The getTime() is an instance
method of Calendar class, so we have called it after getting the instance of Calendar class by the getInstance()
method.

index.jsp

<html>

<body>

Current Time: <%= java.util.Calendar.getInstance().getTime() %>

</body>

</html>

17. Write a program for JSP declaration tag that declares method

<html>

<body>

<%!

int cube(int n){

return n*n*n*;

%>

<%= "Cube of 3 is:"+cube(3) %>

</body>

</html>

18. Write a program for JSP for request and response implicit object

Example of JSP request implicit object

index.html

<form action="welcome.jsp">

<input type="text" name="uname">

<input type="submit" value="go"><br/>


</form>

welcome.jsp

<%

String name=request.getParameter("uname");

out.print("welcome "+name);

%>

Example of response implicit object

index.html

<form action="welcome.jsp">

<input type="text" name="uname">

<input type="submit" value="go"><br/>

</form>

welcome.jsp

<%

response.sendRedirect("http://www.google.com");

%>

19. Write a program for JSP for session implicit object

index.html

<html>

<body>

<form action="welcome.jsp">

<input type="text" name="uname">

<input type="submit" value="go"><br/>

</form>

</body>

</html>

welcome.jsp

<html>

<body>

<%

String name=request.getParameter("uname");

out.print("Welcome "+name);

session.setAttribute("user",name);

<a href="second.jsp">second jsp page</a>


%>

</body>

</html>

second.jsp

<html>

<body>

<%

String name=(String)session.getAttribute("user");

out.print("Hello "+name);

%>

</body>

</html>

20. Write a program for JSP for exception implicit object

index.jsp

<form action="process.jsp">

No1:<input type="text" name="n1" /><br/><br/>

No1:<input type="text" name="n2" /><br/><br/>

<input type="submit" value="divide"/>

</form>

process.jsp

<%@ page errorPage="error.jsp" %>

<%

String num1=request.getParameter("n1");

String num2=request.getParameter("n2");

int a=Integer.parseInt(num1);

int b=Integer.parseInt(num2);

int c=a/b;

out.print("division of numbers is: "+c);

%>

error.jsp

<%@ page isErrorPage="true" %>

<h3>Sorry an exception occured!</h3>


Exception is: <%= exception %>

21. Write a program for JSP for Simple example of java bean class.

package mypack;

public class Employee implements java.io.Serializable{

private int id;

private String name;

public Employee(){}

public void setId(int id){this.id=id;}

public int getId(){return id;}

public void setName(String name){this.name=name;}

public String getName(){return name;}

Accessing the java bean class

package mypack;

public class Test{

public static void main(String args[]){

Employee e=new Employee();//object is created

e.setName("Arjun");//setting value to the object

System.out.println(e.getName());

}}

22. Write a program for JSP for JSP Action Tag

1. <jsp:include> Action

<html>

<head>

<title>Demo of JSP include Action Tag</title>

</head>

<body>

<h3>JSP page: Demo Include</h3>

<jsp:include page="sample.jsp" flush="false" />

</body>

</html>

2. <jsp:forward> Action

<html>

<head>

<title>Demo of JSP Forward Action Tag</title>


</head>

<body>

<h3>JSP page: Demo forward</h3>

<jsp:forward page="second.jsp" />

</body>

</html>

3. <jsp: param> Action

html>

<head>

<title>Demo of JSP Param Action Tag</title>

</head>

<body>

<h3>JSP page: Demo Param along with forward</h3>

<jsp:forward page="second.jsp">

<jsp:param name ="date" value="20-05-2012" />

<jsp: param name ="time" value="10:15AM" />

<jsp: param name ="data" value="ABC" />

</jsp:forward>

</body>

</html>

4. <jsp:useBean>Action

square.java

Package demotest;

public class square

public int square(int n)

{return n*n;} }

example.jsp

<html>

<title>usebean</title>

<body>

<jsp:useBean id="obj" class="demotest.square"/>

<%

int m=obj.square(3);

out.print("square of 3 is "+m);

%>
</body>

</html>

5. <jsp:getProperty> and <jsp:setProperty>

TestBean.java

package demotest;

import java.io.Serializable;

public class TestBean implements Serializable

private String message = "null";

public String getMessage() {

return msg;

public void setMessage(String message) {

this.message = message;

GetSet.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<html>

<head>

<title>Get Set Property</title>

</head>

<body>

<jsp:useBean id="myBean" class="demotest.TestBean" />

<jsp:setProperty name="myBean" property="message" value="GetSetActions" />

<jsp:getProperty name="myBean" property="message" />

</body>

</html>

6. example of element, attribute, body

<html>

<head><title>ele attr body</title>

</head>

<body>
<jsp:element name="XMLelement">

<jsp:attribute name="XMLattribute">

</jsp:attribute>

<jsp:body> My XML</jsp:body>

</jsp:element>

</body>

</html>

7. <jsp:text>

<html>

<head>

<title>Template</title>

</head>

<body>

<jsp:text>Here goes the template text.</jsp:text>

</body>

</html>

8. <jsp: output>

<html>

<head>

<title>Output Actions</title>

</head>

<body>

<jsp: output doctype-root-element="XML" doctype-system="http://www.myxml.com/loose.dtd"/>

</body>

</html>

You might also like