0% found this document useful (0 votes)
53 views

Web Technology Lab Assignment Solution

Uploaded by

reyansh1908
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Web Technology Lab Assignment Solution

Uploaded by

reyansh1908
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

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<args.length;i++)
6. System.out.println(args[i]);
7.
8. }
9. }

• DataInputStream Class

• Scanner Class

10. import java.util.Scanner; // Import the Scanner class


11.
12. class Main {
13. public static void main(String[] args) {
14. Scanner myObj = new Scanner(System.in); // Create a Scanner object
15. System.out.println("Enter username");
16.
17. String userName = myObj.nextLine(); // Read user input
18. System.out.println("Username is: " + userName); // Output user input
Page 1 of 23
19. }
20. }

• Console Class
import java.io.Console;
class ReadStringTest{
public static void main(String args[]){
Console c=System.console();
System.out.println("Enter your name: ");
String n=c.readLine();
System.out.println("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 < arr.length; i++)
arr[i] = new int[i + 1];

// Initializing array
int count = 1;
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[i].length; j++)
arr[i][j] = count++;

// Displaying the values of 2D Jagged array


System.out.println("Contents of 2D Jagged Array");
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
}
}
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(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){System.out.println("barking...");}
6. }
7. class TestInheritance{
8. public static void main(String args[]){
9. Dog d=new Dog();
10. d.bark();
11. d.eat();
12. }}

Page 3 of 23
1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){System.out.println("barking...");}
6. }
7. class BabyDog extends Dog{
8. void weep(){System.out.println("weeping...");}
9. }
10. class TestInheritance2{
11. public static void main(String args[]){
12. BabyDog d=new BabyDog();
13. d.weep();
14. d.bark();
15. d.eat();
16. }}

1. class Animal{
2. void eat(){System.out.println("eating...");}
3. }
4. class Dog extends Animal{
5. void bark(){System.out.println("barking...");}
6. }
7. class Cat extends Animal{
8. void meow(){System.out.println("meowing...");}
9. }
10. class TestInheritance3{
11. public static void main(String args[]){
12. Cat c=new Cat();
13. c.meow();
14. c.eat();
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[a.length][a[0].length];
for(int i=0;i<a.length;i++)
{
for (int j=0;j<a.length;j++)
{
c[i][j]=a[i][j]+b[i][j];
System.out.print(c[i][j]);
}
System.out.println("");
}
}

public static void Sub(int a[][],int b[][])


{
int c[][]=new int[a.length][a[0].length];
for(int i=0;i<a.length;i++)
{
for (int j=0;j<a.length;j++)
{
c[i][j]=a[i][j]-b[i][j];
System.out.print(c[i][j]);
}
System.out.println("");
}
}
}

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()
{
System.out.println("default");

Page 5 of 23
}

// parameterized constructor 2
Temp(int x)
{
// invokes default constructor
this();
System.out.println(x);
}

// parameterized constructor 3
Temp(int x, int y)
{
// invokes parameterized constructor 2
this(5);
System.out.println(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. System.out.println("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. System.out.println("\nDefault Constructor values: \n");
19. System.out.println("Student Id : "+s.id + "\nStudent Name : "+s.name);
20.
21. System.out.println("\nParameterized Constructor values: \n");
22. Student student = new Student(10, "David");
23. System.out.println("Student Id : "+student.id + "\nStudent Name : "+student.name);
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(){System.out.println("Hello");}
9. public void show(){System.out.println("Welcome");}
10.
11. public static void main(String args[]){
12. A7 obj = new A7();
13. obj.print();
14. obj.show();
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. System.out.println(data);
7. }
8. //catch won't be executed
9. catch(NullPointerException e){
10. System.out.println(e);
11. }
12. //executed regardless of exception occurred or not
13. finally {
14. System.out.println("finally block is always executed");
15. }
16.
17. System.out.println("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)
{
Thread.sleep(10000);
System.out.println(i);
}}
catch(InterruptedException e)
{
System.out.println(e.getMessage());
}}
}

class Odd extends Thread


{
public void run()

Page 8 of 23
{
try
{
for(int i=1;i<100;i=i+2)
{
Thread.sleep(2000);
System.out.println(i);
}}
catch(InterruptedException e)
{
System.out.println(e.getMessage());
}}
}

class Mymain
{
public static void main(String args[])
{
Even e=new Even();
Odd o=new Odd();
e.start();
o.start();
}
}
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<s.length();i++)
{
if(s.charAt(i)==' ')
sp++;
}
System.out.println(s.length()-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";
System.out.print(s.charAt(0)+".");
for(int i=0;i<s.length();i++)
{
char c=s.charAt(i);
if(c==' ')
System.out.print(s.charAt(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<s.length();i++)
{
char c=s.charAt(i);
if(Character.isUpperCase(c))
System.out.print(Character.toLowerCase(c));
else if(Character.isLowerCase(c))
System.out.print(Character.toUpperCase(c));
else
System.out.print(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="mystyle.css">
</head>
<body>

<h1>This is a heading</h1>
<p>This is a paragraph.</p>

</body>
</html>

“mystyle.css"
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 “united.ac.in” (For eg.
faculty@united.ac.in)
• 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 java.net.*;
import java.io.*;
class Client
{
public static void main(String []args)
{
Socket c;
BufferedReader brc,brs;
PrintWriter out;
String msg;
try
{
c=new Socket("127.0.0.1",2000);
System.out.println("Connection Established");
out=new PrintWriter(c.getOutputStream(),true);
brc=new BufferedReader(new InputStreamReader(c.getInputStream()));
brs=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Connection Stream fetched");
System.out.print("Enter Any Number ");
msg=brs.readLine();
out.println(msg);
msg=brc.readLine();
System.out.println("Message Received :"+msg);
Page 14 of 23
c.close();
}catch(Exception e){}
}
}

import java.net.*;
import java.io.*;
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=s.accept();
System.out.println("Connection Received");
brc=new BufferedReader(new InputStreamReader(c.getInputStream()));
out=new PrintWriter(c.getOutputStream(),true);
System.out.println("Stream Fetched for R/W");
msg=brc.readLine();
System.out.println("Client Info Received");
a=Integer.parseInt(msg);
b=a*a*a;
msg=String.valueOf(b);
out.println(msg);
System.out.println("Cube of "+a +" has been sent to client");
s.close();
}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 java.util.Scanner;
import java.sql.*;
public class JDBCappITG1
{
public static void main(String[] args)
{
String un,pd,mb;
PreparedStatement pst;
Scanner sc=new Scanner(System.in);
int x;
try
{
System.out.println("enter username,password & Mobile No.");
un=sc.next();
pd=sc.next();
mb=sc.next();
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/united","root","");
pst=con.prepareStatement("insert into emp(uname,pass,mob) values(?,?,?)");
pst.setString(1,un);
pst.setString(2,pd);
pst.setString(3,mb);
x=pst.executeUpdate();
if(x==1)
System.out.println("Record has been saved");
else
System.out.println("Record Not saved");

}
catch(Exception e)
{
System.out.println("please check the data "+e.getMessage());
}
}

package jdbcappitg1;
import java.util.Scanner;
import java.sql.*;
public class JDBCappITG1
Page 16 of 23
{
public static void main(String[] args)
{
String oun,pd,opd;
PreparedStatement pst;
Scanner sc=new Scanner(System.in);
int x;
try
{
System.out.println("enter New Password,username and old password");
pd=sc.next();
oun=sc.next();
opd=sc.next();
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/united","root","");
pst=con.prepareStatement("update emp set pass=? where uname=? and pass=?");
pst.setString(1,pd);
pst.setString(2,oun);
pst.setString(3,opd);

x=pst.executeUpdate();
if(x==1)
{
System.out.println("Record has been updated");
}

}
catch(Exception e)
{
System.out.println("please check the data "+e.getMessage());
}
}

package jdbcappitg1;
import java.util.Scanner;
import java.sql.*;
public class JDBCappITG1 {

public static void main(String[] args)


{
try{
Page 17 of 23
Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/united","root","");

Statement stmt=con.createStatement();

ResultSet rs=stmt.executeQuery("select * from emp");

while(rs.next())
System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3));

con.close();

}catch(Exception e){ System.out.println(e);}

package jdbcappitg1;
import java.util.Scanner;
import java.sql.*;
public class JDBCappITG1
{
public static void main(String[] args)
{
String un,pd;
PreparedStatement pst;
Scanner sc=new Scanner(System.in);
int x;
try
{
System.out.println("enter username");
un=sc.next();

Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/united","root","");
pst=con.prepareStatement("delete from emp where uname=?");
pst.setString(1,un);
x=pst.executeUpdate();

Page 18 of 23
if(x==1)
{
System.out.println("Record has been updated");
}

}
catch(Exception e)
{
System.out.println("please check the data "+e.getMessage());
}
}

}
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="java.sql.*" errorPage="error.jsp"%>
<%
String un,pd,mb;
un=request.getParameter("t1");
pd=request.getParameter("t2");
mb=request.getParameter("t3");
Page 22 of 23
try
{
PreparedStatement pst;
Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/united","root","");
pst=con.prepareStatement("insert into emp(uname,pass,mob) values(?,?,?)");
pst.setString(1,un);
pst.setString(2,pd);
pst.setString(3,mb);
int x=pst.executeUpdate();
if(x==1)
out.println("Record has been saved");
else
out.println("Record Not saved");

}
catch(Exception e)
{
out.println("please check the data "+e.getMessage());
}

%>

Page 23 of 23

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy