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

JavaScript Practical

The document contains 13 programming problems related to Java, JavaScript, and JDBC. Program 1 demonstrates how to get the IP address and hostname of the local machine using Java. Program 2 shows how to get the IP address from a hostname. Program 3 gets the hostname from an IP address. The remaining programs cover additional topics like TCP client-server programming in Java, parsing URL information, performing CRUD operations on a student database using JDBC, implementing a simple RMI program in Java, and using JavaScript to display alert boxes and get input from users.

Uploaded by

Devesh Khandare
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)
470 views

JavaScript Practical

The document contains 13 programming problems related to Java, JavaScript, and JDBC. Program 1 demonstrates how to get the IP address and hostname of the local machine using Java. Program 2 shows how to get the IP address from a hostname. Program 3 gets the hostname from an IP address. The remaining programs cover additional topics like TCP client-server programming in Java, parsing URL information, performing CRUD operations on a student database using JDBC, implementing a simple RMI program in Java, and using JavaScript to display alert boxes and get input from users.

Uploaded by

Devesh Khandare
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/ 53

Program No.

Aim: Write a program in java to get IP address and hostname of


local machine.

import java.net.*;
public class myIP
{
public static void main(String args[])throws Exception
{
System.out.println(InetAddress.getLocalHost());
System.out.println(InetAddress.getByName(""));
}
}

Output:
Program No. 2

Aim: Write a program in JAVA to get IP address from host name.

import java.net.*;
import java.io.*;
public class ip
{
public static void main(String args[]) throws IOException
{
String hostname=args[0];
try
{
InetAddress ipaddress=InetAddress.getByName(hostname);
System.out.println("IP address:"+ipaddress.getHostAddress());
}
catch(UnknownHostException e)
{
System.out.println("Could not find ip address for:"+hostname);
}
}
}

Output:
Program No. 3

Aim: Write a program in JAVA to get host name from IP


address.

import java.net.*;
import java.io.*;
public class IP
{
public static void main(String args[]) throws IOException
{
String hostname = args[0];
try
{
InetAddress ipaddress = InetAddress.getByName(hostname);
System.out.println("IP address : " +ipaddress.getHostAddress());
}
catch(UnknownHostException e)
{
System.out.println("Could not find IP address for:"+hostname);
}
}
}

Output:
Program No. 4

Aim: Write a program in java to get Host name.


import java.io.*;
import java.net.*;
class GFG
{
public static void main(String args[])
{
try
{
InetAddress addr=InetAddress.getByName("23.229.203.68");
System.out.println("Host name is:"+addr.getHostName());
}
catch(UnknownHostException e)
{
System.out.println(e);
}
}
}

Output: -
Program No: 5

Aim: Write a program in java to implement TCP/IP Client


Server.

Server Side
import java.net.*;
import java.io.*;
public class SimpleServer{
public static void main( String args[])throws IOException{
ServerSocket s= new ServerSocket(1234);
Socket s1=s.accept();
OutputStream s1out = s1.getOutputStream();
DataOutputStream dos = new DataOutputStream(s1out);
dos.writeUTF("hi there");
dos.close();
s1out.close();
s1.close();
}
}

Output:
Client Side:
import java.net.*;
import java.io.*;
public class SimpleClient{
public static void main( String args[])throws IOException{
Socket s1= new Socket("localhost",1234);
InputStream s1In = s1.getInputStream();
DataInputStream dis = new DataInputStream(s1In);
String st = new String(dis.readUTF());
System.out.println(st);
dis.close();
s1In.close();
s1.close()}}

Output:
Program No: 6

Aim: Write a program in JAVA to display URL information.

import java.net.*;
public class ParseURL
{
public static void main(String args[]) throws MalformedURLException
{
URL url = new
URL("http://java.sun.com:80/docs/"+"books/tutorial/intro.html#DOWNLOADI
NG");
System.out.println("protocol= "+url.getProtocol());
System.out.println("Host= "+url.getHost());
System.out.println("Filename= "+url.getFile());
System.out.println("Port= "+url.getPort());
System.out.println("Referance= "+url.getRef());
}
}

Output:
Program No: 07
Aim: Write a program in JDBC for displaying information about
students from student database (student number, student name,
student age, student percentage)

import java.io.*;
import java.sql.*;
class student
{
public static void main(String args[])throws
IOException,ClassNotFoundException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Register Jdbc Type1 Driver");
Connection con=DriverManager.getConnection("Jdbc:odbc:db");
System.out.println("Connection establish");
Statement stmt=con.createStatement();
String sql="SELECT * FROM rsw";
ResultSet rs=stmt.executeQuery(sql);
System.out.println("studentno\tstudentname\tstudentage\tstudentper");
System.out.println("------------------------------------------------------");
while(rs.next())
{
System.out.println(rs.getString(1)+" \t "+rs.getString(2)+" \t "+rs.getString(3)+"
\t "+rs.getString(4));
}
}catch(SQLException e)
{
}
}
}
Output:
Program No: 08
Aim: Java program (JDBC) for inserting two records of student
table(Student no, student name, student age, student percentage )
and also display data on screen.
import java.io.*;
import java.sql.*;
class student
{
public static void main(String args[])throws
IOException,ClassNotFoundException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Register Jdbc Type1 Driver");
Connection con=DriverManager.getConnection("Jdbc:odbc:db");
System.out.println("Connection establish");
Statement stmt=con.createStatement();
String Query="INSERT INTO rsw values(6,'rakesh',23,86)";
stmt.executeUpdate(Query);
String Query1="INSERT INTO rsw values(7,'prashant',22,87)";
stmt.executeUpdate(Query1);
System.out.println("Records are successfully inserted");
String sql="select * from rsw";
ResultSet rs=stmt.executeQuery(sql);
System.out.println("studentno\tstudentname\tstudentage\tstudentper");
System.out.println("------------------------------------------------------");
while(rs.next())
{
System.out.println(rs.getString(1)+" \t "+rs.getString(2)+" \t "+rs.getString(3)+"
\t "+rs.getString(4));
}
System.out.println("This is the record from Student database");
}catch(SQLException e)
{
}
}
}
Output:
Program No. 09

Aim: Write a JDBC program for updating the records of student


table(Student number, student name, student age, student
percentage) & also display data on screen

import java.io.*;
import java.sql.*;
class student
{
public static void main(String args[])throws
IOException,ClassNotFoundException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Register Jdbc Type1 Driver");
Connection con=DriverManager.getConnection("Jdbc:odbc:db");
System.out.println("Connection establish");
Statement stmt=con.createStatement();
String Query="update rsw set studentname='shubhangi' where studentno=1";
stmt.executeUpdate(Query);
System.out.println("Records are succesfully inserted");
String sql="SELECT * FROM rsw";
ResultSet rs=stmt.executeQuery(sql);
System.out.println("studentno\tstudentname\tstudentage\tstudentper");
System.out.println("------------------------------------------------------");
while(rs.next())
{
System.out.println(rs.getString(1)+" \t "+rs.getString(2)+" \t "+rs.getString(3)+"
\t "+rs.getString(4));
}
}catch(SQLException e)
{
}
}
}
Output:
Program No:-10
Aim: Java program (JDBC) for deleting record of student
table(Student no, student name, student age, student percentage )
and also display data on screen.

import java.io.*;
import java.sql.*;
class student
{
public static void main(String args[])throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver is loaded");
Connection con=DriverManager.getConnection("Jdbc:odbc:db");
System.out.println("Connection establish");
Statement stmt=con.createStatement();
String sql="DELETE FROM rsw WHERE studentno=6";
stmt.executeUpdate(sql);
System.out.println("Records are deleted");
System.out.println("Recrods after delete statement");
String sql1="SELECT * FROM rsw";
ResultSet rs=stmt.executeQuery(sql1);
System.out.println("studentno\tstudentname\tstudentage\tstudentper");
System.out.println("------------------------------------------------------");
while(rs.next())
{
System.out.println(rs.getString(1)+" \t "+rs.getString(2)+" \t "+rs.getString(3)+"
\t "+rs.getString(4));
}
con.close();
}
}
Output:
Program No. 11

Aim: Write a simple RMI program in JAVA to display message.

Client:
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class Client
{
private Client() {}
public static void main(String args[])
{
try
{
// Getting the registry
Registry registry = LocateRegistry.getRegistry(null);
//Looking up the registry for the remote object
Hello stub = (Hello)registry.lookup("Hello");
//Calling the remote method using the obtained object
stub.printMsg();
}
catch(Exception e)
{
System.err.println("Client exception: " +e.toString());
e.printStackTrace();
}
}
}

Server:
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Server extends ImplExample
{
public Server() {}
public static void main(String args[])
{
try
{
//Instantiating the implemenation class
ImplExample obj = new ImplExample();
//Exporting the object of implementation class
//(here we are exporting the remote object to the stub)
Hello stub=(Hello)UnicastRemoteObject.exportObject(obj, 0);
//Binding the remote object (stub) in the registry
Registry registry = LocateRegistry.getRegistry();
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e)
{
System.err.println("Server exception:" +e.toString());
e.printStackTrace();
}
}
}

HELLO:
import java.rmi.Remote;
import java.rmi.RemoteException;
// Creating remote interface for our application
public interface Hello extends Remote
{
void printMsg() throws RemoteException;
}

Implementation:
//Implementing the remote interface
public class ImplExample implements Hello
{
//Implementing the interface mehtod
public void printMsg()
{
System.out.println("This is an example RMI program");
}
}
OUTPUT:-
Program No. 12

Aim: Write a JavaScript program to display “My first JavaScript


program” using alert message box.

<html>
<head>
<script type="text/javascript">
<!--
function sayHello()
{
alert("My First java script Program")
}
//-->
</script>
</head>
<body>
click here for the result
<input type ="button" onclick="sayHello()" value="Hello"/>
</body>
</html>

Output:-
Program No. 13
Aim: Write a JavaScript program to display largest of two
numbers using input box.

<html>
<head>
<title> 13 </title>
<script type="text/javascript">
x=parseInt(prompt("Enter First Number:"));
y=parseInt(prompt("Enter Second Number:"));
if(x>y)
{
document.write(x+" is Greater");
}
else if(y>x)
{
document.write(y+" is Greater");
}
else
{
document.write("Both are equal");
}
</script>
</head>
<body>
</body>
</html>
Output:
Program No:14
Aim: Write a JavaScript program to display reverse of the input
number and also display its sum of digits.

<html>
<head>
<title> 14 </title>
</head>
<body>
<script language="javascript">
function rev()
{var a,b,no,temp=0;
no=parseInt(frm.txt1.value);
b=no;
while(no>0){
a=no%10;
no=parseInt(no/10);
temp=temp*10+a;
}
alert("reverse of the number is:"+temp);
}
function find()
{
var sum=0;
var no=parseInt(frm.txt1.value);
while(no>0)
{
sum=sum+no%10;
no=Math.floor(no/10);
}
alert("sum of digits:"+sum);
}
</script>
<form name="frm">
Enter a Number:<input name="txt1"type="text"/>
<br>
<br>
<input name="b2" onclick="rev();find();" type="button" value="result"/>

</form>
</body>
</html>
Output:
Program No.15

Aim: Write JavaScript program to display to Concate string


using string operations.

<html>
<head>
<title> 14 </title>
<script language="javascript">
function readText(form)
{
t1=form.inputbox1.value;
t2=form.inputbox1.value;
s3=t1.concat(t2);
alert("The result is:"+s3);
}
</script>
</head>
<body>
<form name="frm"action=""method="get">
enter a String 1:<input name="inputbox1"type="text" value=""><br><br>
enter a String 2:<input name="inputbox2"type="text" value=""><br><br>
<input name="button" onclick="readText(this.form)"type="button"
value="concat"/>
</form>
</body>
</html>
Output:
Program no:16

Aim: Write a JavaScript program to display month.

<html>
<head>
<title> 16 </title>
<script language="javascript">
var n=prompt("Enter no. between 1 to 12");
switch(n)
{
case(n="1"):
document.write("January");
break;

case(n="2"):
document.write("Fabuary");
break;

case(n="3"):
document.write("March");
break;

case(n="4"):
document.write("April");
break;

case(n="5"):
document.write("May");
break;

case(n="6"):
document.write("Jun");
break;

case(n="7"):
document.write("July");
break;

case(n="8"):
document.write("August");
break;
case(n="9"):
document.write("September");
break;

case(n="10"):
document.write("Octomber");
break;

case(n="11"):
document.write("November");
break;

case(n="12"):
document.write("December");
break;
}
</script>
</head>
<body>
</body>
</html>

Output:
Program No:17

Aim: Write JavaScript program to display power of the number


using math function.

<html>
<head>
<title> 17 </title>
<script language="javascript" type="text/javascript">
a=prompt("enter a number in base:");
b=prompt("enter a number in power:");
var x=Math.pow(a,b);
alert("Result:"+x);
</script>
</body>
</html>

Output:
Program No:18

Aim: Write JavaScript program to display length, lower case,


uppercase and Concat string.

<html>
<head>
<title> 18</title>
</head>
<body>
<script language="javascript">
var text1= prompt("enter first string");
alert("length of the string1 "+text1.length);
alert("string1 converted into lower case "+text1.toLowerCase());
alert("string1 converted into upper case "+text1.toUpperCase());
var text2= prompt("enter second string");
alert("concatenate of two string "+text1.concat(text2));
</script>
</body>
</html>
Output:
Program No:19

Aim: Write a JavaScript program to display current date in


dd/mm/yyyy format.

<html>
<head>
<title> Program No. 19 </title>
</head>
<body>
<script language="javascript">
var currentDate=new Date();
var day=currentDate.getDate();
var month=currentDate.getMonth()+1;
var year=currentDate.getFullYear();
document.write("Current date is ");
document.write(day+"/"+month+"/"+year+"<br>");
</script>
</body>
</html>

Output:
Program No. 20

Aim: Write a JAVASCRIPT program to display time in format


HH:MM:SS.

<html>
<head>
<title> Program No. 19 </title>
</head>
<body>
<script language="javascript">
var currentDate=new Date();
var hours=currentDate.getHours();
var minutes=currentDate.getMinutes();
var seconds=currentDate.getSeconds();
document.write("Current time is ");
document.write(hours+":"+minutes+":"+seconds);
</script>
</body>
</html>

Output:-
Program no. 21

Aim: Write JavaScript program using button to refresh page.

<html>
<head>
<title> refresh page button </title>
<script type="text/javascript">
function refreshPage()
{
if(confirm("want to refresh page?"))
{
location.reload();
}
}
</script>
</head>
<body>
<style"text-align:center;">
<br><br><br><br>
<h1>
java script:using function to refresh page by clicking button
</h1>
<p>
<input type="button" value="refresh" onclick='refreshPage()'/>
</p>
</body>
</html>

Output:
Program No. 22

Aim: Write a JavaScript program to get length of Array.

<head>
<title> 22</title>
</head>
<body>
<p> The length property returns the length of an array </p>
<p id="demo"></p>
<script>
var x;
var txt=" ";
var city=[" Mumbai "," Pune "," Nashik "," Akola "," Amaravati "];
for(x in city)
{txt=txt+city[x];
}
document.write("City Name:-"+txt);
document.write("<br>");
document.write(" Length of an array is="+city.length);
</script>
</body>

Output:-
Program No. 23

Aim: JSP program to display student profile.

<html>
<title>Welcome</title>
<h1><% out.print("Student Profile"); %></h1><br>
<body>
Name:<% out.print("Swapnil Anil Tayade"); %><br>
DOB:<% out.print("16-11-1999"); %><br>
Address:<% out.print("New Bhim Nagar,MIDC phase 3,shivani"); %><br>
Taluka:<% out.print("Akola"); %><br>
District:<% out.print("Akola"); %><br>
Pincode:<% out.print("444104"); %><br>
Aadhar No:<% out.print("403380148221"); %><br>
Qualification:<% out.print("Graduation"); %><br>
Stream:<% out.print("Bsc in Information Technology"); %><br>
Batch:<% out.print("2021"); %><br>
</body>
</html>

Output:-
Program No. 23

Aim: JSP program to display student profile.

<html>
<title>Welcome</title>
<h1><% out.print("Student Profile"); %></h1><br>
<body>
Name:<% out.print("Devesh Santosh Khandare"); %><br>
DOB:<% out.print("01-01-2001"); %><br>
Address:<% out.print("Samyak colony khanapur road, patur."); %><br>
Taluka:<% out.print("Patur"); %><br>
District:<% out.print("Akola"); %><br>
Pincode:<% out.print("44501"); %><br>
Aadhar No:<% out.print("518645170178"); %><br>
Qualification:<% out.print("Graduation"); %><br>
Stream:<% out.print("Bsc in Information Technology"); %><br>
Batch:<% out.print("2021"); %><br>
</body>
</html>

Output:-
Program No. 23

Aim: JSP program to display student profile.

<html>
<title>Welcome</title>
<h1><% out.print("Student Profile"); %></h1><br>
<body>
Name:<% out.print("Aetesam Husain"); %><br>
DOB:<% out.print("02-10-2000"); %><br>
Address:<% out.print("Firdous colony, akola"); %><br>
Taluka:<% out.print("Akola"); %><br>
District:<% out.print("Akola"); %><br>
Pincode:<% out.print("444002"); %><br>
Aadhar No:<% out.print("756854231789"); %><br>
Qualification:<% out.print("Graduation"); %><br>
Stream:<% out.print("Bsc in Information Technology"); %><br>
Batch:<% out.print("2021"); %><br>
</body>
</html>

Output:
Program No.24

Aim: - JSP program to display day using switch case.

<html>
<head>
<title> Switch Case Example</title></head>
<body>
<form method ="get" action="day.jsp">
enter your choice:<input type ="text" name="t"><br>
<br>
<input type="submit" value="click here to get result">
</form>
<%if(request.getParameter("t")!=null)
{int day;
day=Integer.parseInt(request.getParameter("t"));
switch (day)
{case 0:
out.print("It/'s Sunday");
break;
case 1:
out.print(" It/'s Monday");
break;
case 2:
out.print(" It/'s Tuesday");
break;
case 3:
out.print(" It/'s Wednesday");
break;
case 4:
out.print(" It/'s Thurday");
break;
case 5:
out.print(" It/'s Friday");
break;
case 6:
out.print(" It/'s Saturday");
break;}
}
%>
</body>
</html>
Output:-
Program No.25
Aim: JSP program to display (current instance) time.

<%@ page import = "java.io.*,java.util.*" %>


<%@ page import = "javax.servlet.*,java.text.*" %>
<html>
<head>
<title>Display Current Date & Time</title>
</head>
<body>
<center>
<h1>Display Current Date & Time</h1>
</center>
<%
Date dNow = new Date( );
SimpleDateFormat ft =
new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
out.print( "<h2 align=\"center\">" + ft.format(dNow) + "</h2>");
%>
</body>
</html>

Output:-
Program No.26

Aim: JSP program to display student name on different font size


using for loop.

<%! int fontsize; %>


<html>
<head>
<title> for loop </title>
</head>
<body>
<% for(fontsize=1;fontsize<=6;fontsize++){%>
<font color="green" size="<%= fontsize%>">
Devesh
</font><br>
<br>
<%}%>
</body>
</html>

Output:-
Program No.26

Aim: JSP program to display student name on different font size


using for loop.

<%! int fontsize; %>


<html>
<head>
<title> for loop </title>
</head>
<body>
<% for(fontsize=1;fontsize<=6;fontsize++){%>
<font color="green" size="<%= fontsize%>">
Swapnil
</font><br>
<br>
<%}%>
</body>
</html>

Output:-
Program No.26

Aim: JSP program to display student name on different font size


using for loop.

<%! int fontsize; %>


<html>
<head>
<title> for loop </title>
</head>
<body>
<% for(fontsize=1;fontsize<=6;fontsize++){%>
<font color="green" size="<%= fontsize%>">
Aetesam
</font><br>
<br>
<%}%>
</body>
</html>

Output:-
Program No. 27

Aim: - JSP program to demonstrate get method.

<html>
<body>
<form action ="input.jsp" method="POST">
firstname:<input type="text" name="firstname">
<br>
lastname:<input type="text" name="lastname">
<br>
<div align="center">
<input type="submit" value="Submit"/>
</form>
</body>
</html>

<html>
<head> <title> Get Method </title>
</head>
<body>
<h1> Get method</h1>
<ul> <li><p><b> firstname:</b></p></li>
<%= request.getParameter ("firstname")%>
<li><p><b> lastname:</b></p></li>
<%= request.getParameter ("lastname")%><ul>
</body>
</html>
Output:-
Program No. 27

Aim: - JSP program to demonstrate get method.

<html>
<body>
<form action ="input.jsp" method="POST">
firstname:<input type="text" name="firstname">
<br>
lastname:<input type="text" name="lastname">
<br>
<div align="center">
<input type="submit" value="Submit"/>
</form>
</body>
</html>

<html>
<head> <title> Get Method </title>
</head>
<body>
<h1> Get method</h1>
<ul> <li><p><b> firstname:</b></p></li>
<%= request.getParameter ("firstname")%>
<li><p><b> lastname:</b></p></li>
<%= request.getParameter ("lastname")%><ul>
</body>
</html>
Output:-
Program No. 27

Aim: - JSP program to demonstrate get method.

<html>
<body>
<form action ="input.jsp" method="POST">
firstname:<input type="text" name="firstname">
<br>
lastname:<input type="text" name="lastname">
<br>
<div align="center">
<input type="submit" value="Submit"/>
</form>
</body>
</html>

<html>
<head> <title> Get Method </title>
</head>
<body>
<h1> Get method</h1>
<ul> <li><p><b> firstname:</b></p></li>
<%= request.getParameter ("firstname")%>
<li><p><b> lastname:</b></p></li>
<%= request.getParameter ("lastname")%><ul>
</body>
</html>
Output:-
Program No. 28

Aim:- JSP program using checkbox to display the element


in list.

<html>
<body>
<form action = "main.jsp" method = "POST" target = "_blank">
<input type = "checkbox" name = "DM" checked = "checked" /> DM
<input type = "checkbox" name = "CG" /> CG
<input type = "checkbox" name = "CSC" checked = "checked" /> CSC
<input type = "checkbox" name = "DOS" checked = "checked" /> DOS
<input type = "submit" value = "Select Subject" />
</form>
</body>
</html>

<html>
<head>
<title>Reading Checkbox Data</title>
</head>
<body>
<h1>Reading Checkbox Data</h1>
<ul>
<li><p><b>DM Flag:</b>
<%= request.getParameter("DM")%>
</p></li>
<li><p><b>CG Flag:</b>
<%= request.getParameter("CG")%>
</p></li>
<li><p><b>CSC Flag:</b>
<%= request.getParameter("CSC")%>
</p></li>

<li><p><b>DOS Flag:</b>
<%= request.getParameter("DOS")%>
</p></li>
</ul>
</body>
</html>
Output:-

You might also like