United College of Engineering & Research, Prayagraj
Department of Computer Science and Engineering
Web Technology Lab Assignment (KCS-652)
B. Tech Computer Science and Engineering (VI-Semester)
N Program
o.
1 Write a program in java to take input from user by using all the following methods:
• Command Line Arguments
• DataInputStream Class
• BufferedReader Class
• Scanner Class
• Console Class
Solution:
• Command Line Arguments
1. class A
2. {
3. public static void main(String args[]){
4.
5. for(int i=0;i<[Link];i++)
6. [Link](args[i]);
7.
8. }
9. }
• DataInputStream Class
• Scanner Class
10. import [Link]; // Import the Scanner class
11.
12. class Main {
13. public static void main(String[] args) {
14. Scanner myObj = new Scanner([Link]); // Create a Scanner object
15. [Link]("Enter username");
16.
17. String userName = [Link](); // Read user input
18. [Link]("Username is: " + userName); // Output user input
Page 1 of 23
19. }
20. }
• Console Class
import [Link];
class ReadStringTest{
public static void main(String args[]){
Console c=[Link]();
[Link]("Enter your name: ");
String n=[Link]();
[Link]("Welcome "+n);
}
}
21.
2 Write a program in java which creates the variable size array (Jagged Array) and print
all the values using loop statement.
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Solution:
class Main {
public static void main(String[] args)
{
int r = 5;
// Declaring 2-D array with 5 rows
int arr[][] = new int[r][];
// Creating a 2D array such that first row
// has 1 element, second row has two
// elements and so on.
Page 2 of 23
for (int i = 0; i < [Link]; i++)
arr[i] = new int[i + 1];
// Initializing array
int count = 1;
for (int i = 0; i < [Link]; i++)
for (int j = 0; j < arr[i].length; j++)
arr[i][j] = count++;
// Displaying the values of 2D Jagged array
[Link]("Contents of 2D Jagged Array");
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < arr[i].length; j++)
[Link](arr[i][j] + " ");
[Link]();
}
}
}
3 Write a program in java to implement the following types of inheritance:
• Single Inheritance
• Multilevel Inheritance
• Hierarchical Inheritance
• Hybrid Inheritance
Solution:
Single Inheritance
1. class Animal{
2. void eat(){[Link]("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){[Link]("barking...");}
6. }
7. class TestInheritance{
8. public static void main(String args[]){
9. Dog d=new Dog();
10. [Link]();
11. [Link]();
12. }}
Page 3 of 23
1. class Animal{
2. void eat(){[Link]("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){[Link]("barking...");}
6. }
7. class BabyDog extends Dog{
8. void weep(){[Link]("weeping...");}
9. }
10. class TestInheritance2{
11. public static void main(String args[]){
12. BabyDog d=new BabyDog();
13. [Link]();
14. [Link]();
15. [Link]();
16. }}
1. class Animal{
2. void eat(){[Link]("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){[Link]("barking...");}
6. }
7. class Cat extends Animal{
8. void meow(){[Link]("meowing...");}
9. }
10. class TestInheritance3{
11. public static void main(String args[]){
12. Cat c=new Cat();
13. [Link]();
14. [Link]();
15. }}
Page 4 of 23
4 Create a package named “Mathematics” and add a class “Matrix” with methods to add
and subtract matrices (2x2). Write a Java program importing the Mathematics package
and use the classes defined in it.
Solution:
package Mathematics;
public class Matrix
{
public static void Add(int a[][],int b[][])
{
int c[][]=new int[[Link]][a[0].length];
for(int i=0;i<[Link];i++)
{
for (int j=0;j<[Link];j++)
{
c[i][j]=a[i][j]+b[i][j];
[Link](c[i][j]);
}
[Link]("");
}
}
public static void Sub(int a[][],int b[][])
{
int c[][]=new int[[Link]][a[0].length];
for(int i=0;i<[Link];i++)
{
for (int j=0;j<[Link];j++)
{
c[i][j]=a[i][j]-b[i][j];
[Link](c[i][j]);
}
[Link]("");
}
}
}
5 Write a program in java to implement constructor chaining and constructor overloading.
Solution:
// Java program to illustrate Constructor Chaining
// within same class Using this() keyword
// and changing order of constructors
class Temp
{
// default constructor 1
Temp()
{
[Link]("default");
Page 5 of 23
}
// parameterized constructor 2
Temp(int x)
{
// invokes default constructor
this();
[Link](x);
}
// parameterized constructor 3
Temp(int x, int y)
{
// invokes parameterized constructor 2
this(5);
[Link](x * y);
}
public static void main(String args[])
{
// invokes parameterized constructor 3
new Temp(8, 10);
}
}
1. public class Student {
2. //instance variables of the class
3. int id;
4. String name;
5.
6. Student(){
7. [Link]("this a default constructor");
8. }
9.
10. Student(int i, String n){
11. id = i;
12. name = n;
Page 6 of 23
13. }
14.
15. public static void main(String[] args) {
16. //object creation
17. Student s = new Student();
18. [Link]("\nDefault Constructor values: \n");
19. [Link]("Student Id : "+[Link] + "\nStudent Name : "+[Link]);
20.
21. [Link]("\nParameterized Constructor values: \n");
22. Student student = new Student(10, "David");
23. [Link]("Student Id : "+[Link] + "\nStudent Name : "+[Link]);
24. }
25. }
6 “Java does not support multiple inheritance but we can achieve it by interface”. Write
a program to justify the above statement.
Solution:
1. interface Printable{
2. void print();
3. }
4. interface Showable{
5. void show();
6. }
7. class A7 implements Printable,Showable{
8. public void print(){[Link]("Hello");}
9. public void show(){[Link]("Welcome");}
10.
11. public static void main(String args[]){
12. A7 obj = new A7();
13. [Link]();
14. [Link]();
15. }
16. }
7 Write a program in java to demonstrate that finally block is always executed whether
exception occurred or not.
Solution:
1. class TestFinallyBlock {
2. public static void main(String args[]){
3. try{
4. //below code do not throw any exception
Page 7 of 23
5. int data=25/5;
6. [Link](data);
7. }
8. //catch won't be executed
9. catch(NullPointerException e){
10. [Link](e);
11. }
12. //executed regardless of exception occurred or not
13. finally {
14. [Link]("finally block is always executed");
15. }
16.
17. [Link]("rest of phe code...");
18. }
19. }
8 Write a program in java which creates two threads, “Even” thread and “Odd” thread
and print the even no using Even Thread and odd no using Odd Thread.
Solution:
class Even extends Thread
{
public void run()
{
try
{
for(int i=0;i<100;i=i+2)
{
[Link](10000);
[Link](i);
}}
catch(InterruptedException e)
{
[Link]([Link]());
}}
}
class Odd extends Thread
{
public void run()
Page 8 of 23
{
try
{
for(int i=1;i<100;i=i+2)
{
[Link](2000);
[Link](i);
}}
catch(InterruptedException e)
{
[Link]([Link]());
}}
}
class Mymain
{
public static void main(String args[])
{
Even e=new Even();
Odd o=new Odd();
[Link]();
[Link]();
}
}
9 Write a program in java which takes a name of a person as an input from the user
implement the followings:
• Find the length of the string (excluding the whitespaces).
public class StringApp {
public static void main(String[] args)
{
String s="Arun Kumar Maurya";
int sp=0;
for(int i=0;i<[Link]();i++)
{
if([Link](i)==' ')
sp++;
}
[Link]([Link]()-sp);
Page 9 of 23
}
}
• Create the abbreviation of the name (For eg. “Arun Kumar Maurya” will be
displayed as “A.K.M.”).
public class StringApp {
public static void main(String[] args)
{
String s="Arun Kumar Maurya";
[Link]([Link](0)+".");
for(int i=0;i<[Link]();i++)
{
char c=[Link](i);
if(c==' ')
[Link]([Link](i+1)+".");
}
• Swap the case of all input characters (For eg. “AruN KuMar MauRya” will be
displayed as “aRUn kUmAR mAUrYA”)
public class StringApp {
public static void main(String[] args)
{
String s="Arun Kumar Maurya 123";
for(int i=0;i<[Link]();i++)
{
char c=[Link](i);
if([Link](c))
[Link]([Link](c));
else if([Link](c))
[Link]([Link](c));
else
[Link](c);
Page 10 of 23
}
10 Write a HTML program to display your complete class time table.
Solution:
<html>
<head>
</head>
<body>
<table border="1px" width="100%" height="100px">
<tr><th width="10%">DAY/<br>TIME</th><th
width="10%">2</th><th>2</th><th>2</th><th>2</th><th
rowspan="7">R<br>E<br>C<br>E<br>S<br>S<br></th><th width="10%">1:10-
1:55</th><th width="10%">1:55-2:40</th><th width="10%">2</th><th>2</th></tr>
<tr><td>Mon</td><td>1</td><td>2</td><td>3</td><td>4</td><td colspan="2"
align="center">OS Lab </td><td>2</td><td>2</td></tr>
<tr><td>Tue</td><td>2</td><td>2</td><td>2</td><td>2</td><td>2</td><td>2</td
><td>2</td><td>2</td></tr>
<tr><td>Wed</td><td>2</td><td>2</td><td>2</td><td>2</td><td>2</td><td>2</t
d><td>2</td><td>2</td></tr>
<tr><td>Thurs</td><td>2</td><td>2</td><td>2</td><td>2</td><td>2</td><td>2</
td><td>2</td><td>2</td></tr>
<tr><td>Fri</td><td>2</td><td>2</td><td>2</td><td>2</td><td>2</td><td>2</td
><td>2</td><td>2</td></tr>
<tr><td>Sat</td><td>2</td><td>2</td><td>2</td><td>2</td><td>2</td><td>2</td
><td>2</td><td>2</td></tr>
</table>
Page 11 of 23
</body>
</html>
11 Write a HTML program to display the given list.
• Fruit
o Bananas
o Apples
▪ Green
▪ Red
o Pears
• Vegetables
• Meat
12 Write a HTML program to divide your web page using frames
Frame1 Frame3 Frame4
Contents of Frame 1 Contents of Frame 3 Contents of Frame 4
Frame2
Contents of Frame 2
13 Write an HTML code to demonstrate the usage of inline CSS, internal CSS, external
CSS and imported CSS.
Solution:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="[Link]">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
“[Link]"
Page 12 of 23
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1 style="color:blue;text-align:center;">This is a heading</h1>
<p style="color:red;">This is a paragraph.</p>
</body>
</html>
14 Write programs using Java script for Web Page to display browsers information.
Page 13 of 23
Solution:
15 Write a Java script to validate Name, Mobile Number, Email Id and Password.
• Name must contain alphabets and whitespace only
• Mobile number must be 10 digits only.
• Email Id must have one “@”, and domain name is “[Link]” (For eg.
faculty@[Link])
• Password must have atleast one aplhabet, one digit and one special character
(!@#$%&*)
16 Writing program in XML for creation of DTD, which specifies set of rules. Create a
style sheet in CSS/ XSL & display the document in internet explorer.
17 Write a program to implement Math server using TCP socket and also write a client
program to send user input to Math server and display response as the square of the
given number.
Solution:
import [Link].*;
import [Link].*;
class Client
{
public static void main(String []args)
{
Socket c;
BufferedReader brc,brs;
PrintWriter out;
String msg;
try
{
c=new Socket("[Link]",2000);
[Link]("Connection Established");
out=new PrintWriter([Link](),true);
brc=new BufferedReader(new InputStreamReader([Link]()));
brs=new BufferedReader(new InputStreamReader([Link]));
[Link]("Connection Stream fetched");
[Link]("Enter Any Number ");
msg=[Link]();
[Link](msg);
msg=[Link]();
[Link]("Message Received :"+msg);
Page 14 of 23
[Link]();
}catch(Exception e){}
}
}
import [Link].*;
import [Link].*;
class Server
{
public static void main(String []args)
{
ServerSocket s;
PrintWriter out;
BufferedReader brc;
Socket c;
String msg;
int a,b;
try
{
s=new ServerSocket(2000);
c=[Link]();
[Link]("Connection Received");
brc=new BufferedReader(new InputStreamReader([Link]()));
out=new PrintWriter([Link](),true);
[Link]("Stream Fetched for R/W");
msg=[Link]();
[Link]("Client Info Received");
a=[Link](msg);
b=a*a*a;
msg=[Link](b);
[Link](msg);
[Link]("Cube of "+a +" has been sent to client");
[Link]();
}catch(Exception e){}
}
}
18 Write a program to illustrate CURD operations using JDBC connectivity with
MySQL database.
Page 15 of 23
Solution:
package jdbcappitg1;
import [Link];
import [Link].*;
public class JDBCappITG1
{
public static void main(String[] args)
{
String un,pd,mb;
PreparedStatement pst;
Scanner sc=new Scanner([Link]);
int x;
try
{
[Link]("enter username,password & Mobile No.");
un=[Link]();
pd=[Link]();
mb=[Link]();
[Link]("[Link]");
Connection con=[Link](
"jdbc:mysql://localhost:3306/united","root","");
pst=[Link]("insert into emp(uname,pass,mob) values(?,?,?)");
[Link](1,un);
[Link](2,pd);
[Link](3,mb);
x=[Link]();
if(x==1)
[Link]("Record has been saved");
else
[Link]("Record Not saved");
}
catch(Exception e)
{
[Link]("please check the data "+[Link]());
}
}
package jdbcappitg1;
import [Link];
import [Link].*;
public class JDBCappITG1
Page 16 of 23
{
public static void main(String[] args)
{
String oun,pd,opd;
PreparedStatement pst;
Scanner sc=new Scanner([Link]);
int x;
try
{
[Link]("enter New Password,username and old password");
pd=[Link]();
oun=[Link]();
opd=[Link]();
[Link]("[Link]");
Connection con=[Link](
"jdbc:mysql://localhost:3306/united","root","");
pst=[Link]("update emp set pass=? where uname=? and pass=?");
[Link](1,pd);
[Link](2,oun);
[Link](3,opd);
x=[Link]();
if(x==1)
{
[Link]("Record has been updated");
}
}
catch(Exception e)
{
[Link]("please check the data "+[Link]());
}
}
package jdbcappitg1;
import [Link];
import [Link].*;
public class JDBCappITG1 {
public static void main(String[] args)
{
try{
Page 17 of 23
[Link]("[Link]");
Connection con=[Link](
"jdbc:mysql://localhost:3306/united","root","");
Statement stmt=[Link]();
ResultSet rs=[Link]("select * from emp");
while([Link]())
[Link]([Link](1)+" "+[Link](2)+" "+[Link](3));
[Link]();
}catch(Exception e){ [Link](e);}
package jdbcappitg1;
import [Link];
import [Link].*;
public class JDBCappITG1
{
public static void main(String[] args)
{
String un,pd;
PreparedStatement pst;
Scanner sc=new Scanner([Link]);
int x;
try
{
[Link]("enter username");
un=[Link]();
[Link]("[Link]");
Connection con=[Link](
"jdbc:mysql://localhost:3306/united","root","");
pst=[Link]("delete from emp where uname=?");
[Link](1,un);
x=[Link]();
Page 18 of 23
if(x==1)
{
[Link]("Record has been updated");
}
}
catch(Exception e)
{
[Link]("please check the data "+[Link]());
}
}
}
19 Write a SQL query to create and call the stored procedures with following
specifications:
• Procedure without parameter and no return type
• Procedure with parameter and no return type
• Procedure with parameter and with return type
Solution:
CREATE PROCEDURE empdetails()
BEGIN
SELECT * FROM emp;
END
//
--->change the delimeter to ; again
DELIMITER ;
--->call the procedure
CALL empdetails;
Page 19 of 23
--->drop the procedure
DROP PROCEDURE empdetails;
mysql> use school;
Database changed
mysql> DELIMITER //
mysql>
mysql> CREATE PROCEDURE getempdetails()
-> BEGIN
-> SELECT * FROM employee;
-> END //
Query OK, 0 rows affected (0.00 sec)
_______________________________
Changing Delimeter to ;
mysql> DELIMITER ;
_________________________________
Calling Stored Procedure
mysql> call getempdetails;
+------+-----+--------+---------+
| name | id | salary | Dept_id |
+------+-----+--------+---------+
Page 20 of 23
| John | 110 | 25000 | 201 |
| John | 111 | 25000 | 201 |
+------+-----+--------+---------+
2 rows in set (0.00 sec)
Query OK, 0 rows affected (0.01 sec)
______________________________________
Removing Stored Procedure
mysql> drop procedure getempdetails;
mysql> drop procedure if exists getempdetails;
DELIMITER &&
CREATE PROCEDURE get_tot_salary ()
BEGIN
SELECT sum(salary) AS Total_Salary FROM employee;
END &&
DELIMITER ;
call get_tot_salary;
Page 21 of 23
DELIMITER &&
CREATE PROCEDURE get_student (IN var1 INT)
BEGIN
SELECT * FROM student_info LIMIT var1;
END &&
DELIMITER ;
CALL get_student(2);
+--------+------+----------+
| fname | id | address |
+--------+------+----------+
| fname2 | 2 | Lucknow |
| fname3 | 3 | Varanasi |
+--------+------+----------+
2 rows in set (0.00 sec)
20 Write a program to illustrate CURD operations using JSP connectivity with MySQL
database on Apache Tomcat web server.
Solution:
<%@page import="[Link].*" errorPage="[Link]"%>
<%
String un,pd,mb;
un=[Link]("t1");
pd=[Link]("t2");
mb=[Link]("t3");
Page 22 of 23
try
{
PreparedStatement pst;
[Link]("[Link]");
Connection con=[Link](
"jdbc:mysql://localhost:3306/united","root","");
pst=[Link]("insert into emp(uname,pass,mob) values(?,?,?)");
[Link](1,un);
[Link](2,pd);
[Link](3,mb);
int x=[Link]();
if(x==1)
[Link]("Record has been saved");
else
[Link]("Record Not saved");
}
catch(Exception e)
{
[Link]("please check the data "+[Link]());
}
%>
Page 23 of 23