Download as pdf or txt
Download as pdf or txt
You are on page 1of 72

PAGE STAFF

S.NO DATE CONTENT


NO SIGN

Multi Threading
&
1
Exception Handling
Program to read, write and copy a file
2
using Byte streams
Program to read, write and copy a file
3
using Character streams
Program using AWT to display
4
Employee personal details

5 Develop banking system using Swing

Program to handle Mouse and Key


6
events

TCP/IP Protocol for Message


7
Communication
UDP Protocol for Message
8
Communication

9 Program Using JDBC

Client/Server communication using


10
servlets

11 Web page using JSP

12 RMI
1. MULTITHREADING & EXCEPTION HANDLING

AIM:
To develop a java program using multithreading and multithreading table.

ALGORITHM:
Step 1:
Start the program.
Step 2:
Import the package java.io.
Step 3:
create a class mtable.
Step 4:
Declare the variable n,no.
Step 5:
calculate the mtable by using try and catch block.
Step 6:
Assign the value using for loop statement .
Step 7:
Print the exceptionbyapproptiate try and catch block correactly.
Step 8:
Stop the program.
//MULTI THREADING & EXCEPTION HANDLING
packagemt;
import java.io.*;
public class mtable
{
public static void main(String[ ] args)
{
System.out.println("Multiplication table using Multithread");
table t1 = new table("two",2);
table t2 = new table("three",3);
}
}
class table extends Thread
{
String t_name;
intt_number;
Thread t;
table (String n,int no)
{
t_name=n;
t_number=no;
t=new Thread(this);
t.start();
}
@Override
public void run()
{
int i;
try
{
for(i=1;i<=10;i++)
{
System.out.println(t_number + "*" + i + "=" + (t_number * i ));
Thread.sleep(500);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUTPUT:

init:
deps-jar:
compile-single:
run-single:
Multiplication table using Multithread
2*1=2
3*1=3
2*2=4
3*2=6
2*3=6
3*3=9
2*4=8
3*4=12
2*5=10
3*5=15
2*6=12
3*6=18
2*7=14
3*7=21
2*8=16
3*8=24
2*9=18
3*9=27
2*10=20
3*10=30
BUILD SUCCESSFUL (total time: 5 seconds)
RESULT:

Thus the Multithreading program is has been executed successfully.


1. EXCEPTION HANDLING

AIM :
To create a try block that is lightly to generate in exception and using catch block statement.

ALGORITHM:
Step 1:
Start the program.
Step 2:
Create a class ‘exception’.
Step 3:
Calculate the exception by using try and catch block.
Step 4:
Assign the value for calculate the exception.
Step 5:
Calculate the exception of the number formatted by using try and catch block.
Step 6:
Print the exception by appropriate try and catch block correctly.
Step 7:
Stop the program.
//EXCEPTION

package exception;
import java.io.*;
classownexception extends exception
{
ownexception(String str)
{
System.out.println("str");
}
}
class exception
{
public static void main(String args[ ]) throws IOException
{
int choice;
intch;
BufferedReaderbr = new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.println("\n Enter your choice 1 to 5");
choice = Integer.parseInt(br.readLine());
try
{
if (choice == 1)
{
System.out.println("\n Enter two values");
int n1 = Integer.parseInt(br.readLine());
int n2 = Integer.parseInt(br.readLine());
System.out.println("the given first value:" + n1);
System.out.println("the given second value:" + n2);
int n3 = n1 / n2;
System.out.println("n1/n2 value is:" + n3);
}
else if (choice == 2)
{
int n[ ] = new int[10];
System.out.println(n[14]);
}
else if (choice == 3)
{
System.out.println("\n Enter your name");
int n3 = Integer.parseInt(br.readLine());
}
else if (choice == 4)
{
intarr[ ] = new int[2];
System.out.println("first element" + arr[0]);
}
else if (choice == 5)
{
String s = "it is used to exception";
newownexception(s);
}
}
catch (ArithmeticException a)
{
System.out.println("\n divided by zero exception is caught");
}
catch (ArrayIndexOutOfBoundsException n)
{
System.out.println("\n array index out exception is caught");
}
catch (NumberFormatException e)
{
System.out.println("\n Number Format exception is caught");
}
catch (NegativeArraySizeException e)
{
System.out.println("\nNegative Array Size exception isaught");
}
catch (NullPointerException a)
{
System.out.println("\n Null pointer exception is caught");
}
System.out.println("r u want to continue(0)?");
ch = Integer.parseInt(br.readLine());
}
while (ch == 0);
}
}
OUTPUT:

init:
deps-jar:
Compiling 1 source file to C:\exception\build\classes
compile-single:
run-single:
Enter your choice 1 to 5
1
Enter two values
24
12
the given first value:24
the given second value:12
n1/n2 value is:2
r u want to continue(0)?
0
Enter your choice 1 to 5
2
array index out exception is caught
r u want to continue(0)?
0
Enter your choice 1 to 5
3
Enter your name
dhinakaran
Number Format exception is caught
r u want to continue(0)?
0
Enter your choice 1 to 5
4
first element0
r u want to continue(0)?
0
Enter your choice 1 to 5
5
str
r u want to continue(0)
RESULT:

Thus the Exception Handling program is has been executed successfully.


2. READ, WRITE AND COPY A FILE USING BYTE STREAM

AIM
To write a java program using i/o package

ALGORITHM:
Step 1:
Start the program
Step 2:
Import the package Java.IO
Step 3:
Create a class ‘data Stream’
Step 4:
Create the class Buffer Reader Object for input stream Reader.
Step 5 :
Create the Data output Stream object for writing the output Stream.
Step 6:
In main( ) create an object a for the Buffered Reader and Data Output Stream.
Step 7:
Close the data output Stream and Buffered Reader using the Object is Created.
Step 8:
Stop the Program.
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class ReadByteStreams
{
public static void main(String args[])throws IOException
{
String str = args[0];
byte b[] = str.getBytes();
InputStream bais = null;
bais = new ByteArrayInputStream(b);
int r;
while ((r = bais.read()) != -1)
{
System.out.print((char) r + " ");
}
bais.close();
}
}
OUTPUT:

init:
deps-jar:
compile-single:
run-single:
PACKAGE JJ;
HI DHINA HOW ARE YOU
wordfound:package
wordfound:jj
wordfound:HI
wordfound:DHINA
wordfound:HOW
wordfound:ARE
wordfound:YOU
found:7 words in file
BUILD SUCCESSFUL (total time: 0 seconds)
RESULT:

Thus the I/O Byte Stream program has been executed successfully.
3. READ, WRITE AND COPY A FILE USING CHRACTER STREAM

AIM:
To write a java program using io package

ALGORITHM:
Step 1:
Start the program
Step 2 :
Import the package Java.IO
Step 3:
Create a class ‘data Stream’
Step 4:
Create the class BufferReader Object for input stream Reader.
Step5 :
Create the Data output Stream object for writing the output Stream.
Step 6:
In main () create an object a for the Buffered Reader and Data Output Stream.
Step 7:
Close the data output Stream and Buffered Reader using the Object is Created.
Step 8:
Stop the Program.
packagejj;
import java.io.*;
classkar
{
public static void main(String[ ] args) throws IOException
{
BufferedReader d = new BufferedReader(new InputStreamReader(new
FileInputStream("C:/jj/src/jj/main.java")));
DataOutputStream o = new DataOutputStream(new
FileOutputStream("C:/jj/src/jj/t.txt"));
FileReaderfr = new FileReader("C:/jj/src/jj/main.java");
StreamTokenizer input = new StreamTokenizer(fr);
inttot,count=0;
String line,a;
while((line = d.readLine())!=null)
{
a=(line.toUpperCase());
System.out.println(a);
o.writeBytes(a+"\r\n");
}
while((tot=input.nextToken())!=StreamTokenizer.TT_EOF)
if(tot==StreamTokenizer.TT_WORD)
{
System.out.println("word found:"+input.sval);
count++;
}

System.out.println("found:"+count+" words in file");


d.close();
o.close();
}
}
OUTPUT:

init:
deps-jar:
compile-single:
run-single:
PACKAGE JJ;
HI DHINA HOW ARE YOU
wordfound:package
wordfound:jj
wordfound:HI
wordfound:DHINA
wordfound:HOW
wordfound:ARE
wordfound:YOU
found:7 words in file
BUILD SUCCESSFUL (total time: 0 seconds)
RESULT:

Thus the I/O Streams program is has been executed successfully.


4. THE PERSONAL DETAIL OF AN EMPLOYEE USING AWT

AIM:
To write a program which will make ball of various colors to move within a frame.

ALGORITHM:
Step 1:
Start the program
Step 2:
Import the Packages java.awt and java.applet
Step 3:
Declare the variable m1,m2,m3,m4,m5,m6,m7,m8.
Step 4:
Declare the variable n1,n2,n3,n4,n5,n6,n7,n8.
Step 5:
Declare the function name and Print.
Step 6:
Declare the method sleep()
Step 7:
Use the method repaint()
Step 8:
Stop the program.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class employee extends Frame implements ActionListener


{
String msg;
Button b1=new Button("save");
Label l11=new Label("Employee Details",Label.CENTER);
Label l1=new Label("Name:",Label.LEFT);
Label l2=new Label("age:",Label.LEFT);
Label l3=new Label("Sex(M/F):",Label.LEFT);
Label l4=new Label("Address:",Label.LEFT);
Label l5=new Label("Department:",Label.LEFT);
Label l6=new Label("Experience:",Label.LEFT);
Label l7=new Label("",Label.RIGHT);
TextField t1=new TextField();
Choice c1=new Choice();
CheckboxGroup cbg=new CheckboxGroup();
Checkbox ck1=new Checkbox("Male",false,cbg);
Checkbox ck2=new Checkbox("Female",false,cbg);
TextArea t2=new TextArea("",180,90,TextArea.SCROLLBARS_VERTICAL_ONLY);
Choice dept=new Choice();
Choice exp=new Choice();
Choice age=new Choice();
public employee()
{
addWindowListener(new myWindowAdapter());
setBackground(Color.cyan);
setForeground(Color.black);
setLayout(null);
add(l11);
add(l1);
add(l2);
add(l3);
add(l4);
add(l5);
add(l6);
add(l7);
add(t1);
add(t2);
add(ck1);
add(ck2);
add(dept);
add(exp);
add(age);
add(b1);
b1.addActionListener(this);
add(b1);
dept.add("Programmer");
dept.add("Testing");
dept.add("Debugging");
dept.add("Deployment");
exp.add("1");
exp.add("2");
exp.add("3");
exp.add("4");
exp.add("5");
exp.add("6");
age.add("20");
age.add("21");
age.add("22");
age.add("23");
age.add("Greater 23");
l1.setBounds(25,65,90,20);
l2.setBounds(25,90,90,20);
l3.setBounds(25,120,90,20);
l4.setBounds(25,185,90,20);
l5.setBounds(25,260,90,20);
l6.setBounds(25,290,90,20);
l7.setBounds(25,260,90,20);
l11.setBounds(10,40,280,20);
t1.setBounds(120,65,170,20);
t2.setBounds(120,185,170,60);
ck1.setBounds(120,120,50,20);
ck2.setBounds(170,120,60,20);
dept.setBounds(120,260,100,20);
exp.setBounds(120,290,50,20);
age.setBounds(120,90,50,20);
b1.setBounds(120,350,50,30);
}
public void paint(Graphics g)
{
g.drawString(msg,200,450);}
public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("save"))
{
msg="Employee details saved!";
setForeground(Color.red); }
}
public static void main(String g[])
{
employee emp=new employee();
emp.setSize(new Dimension(500,500));
emp.setTitle("Employee Registration");
emp.setVisible(true);
}
}
class myWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
OUTPUT:

init:
deps-jar:
Compiling 1 source file to C:\balls\build\classes
compile-single:
run-applet:
RESULT:

Thus the AWT program is has been executed successfully.


5. BANKING SYSTEM USING SWING

AIM:
To write a java program for Student mark list using Swing.

ALGORITHM:
Step1 :
Start the program.
Step2 :
Import the Packages java.swing,java.awt.event and java.awt
Step3 :
Create the class ‘mark’ that extends JApplet and implements Action Listner.
Step4 :
Create JLabelIButtonJText Field.
Step5 :
Define the method into Perform one ‘ok’ and ‘cancel’
Step 6:
Define the method Actionperformed() In which event is Activated.
Step7 :
Create a html Program with applet code.
Step8 :
Stop the program.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class GuiAccTest extends Frame implements ActionListener
{
Label lab=new Label(" ");
Label lab1=new Label(" ");
TextField t[]=new TextField [4];
Label l[]=new Label [4];
Button but=new Button("Create Account");
Button but1=new Button("Test Account");
BankAccount b;
GuiAccTest()
{
addWindowListener(new NewWindowAdapter());
setLayout(new GridLayout(2,0));
Panel p=new Panel();
Panel p1=new Panel();
but.addActionListener(this);
but1.addActionListener(this);
p.setLayout(new GridLayout(5,2));
p1.add(lab1);
p1.add(lab);
l[0]=new Label("Account Number");
l[1]=new Label("Initial Balance");
l[2]=new Label("Deposit Amount");
l[3]=new Label("Withdraw Amount");
for(int i=0;i<4;i++)
{
t[i]=new TextField(10);
p.add(l[i]);
p.add(t[i]);
}
p.add(but);
p.add(but1);
but1.setVisible(false);
l[2].setVisible(false);
l[3].setVisible(false);
t[2].setVisible(false);
t[3].setVisible(false);
add(p);
add(p1);
}
String testAccount(int d_amt,int w_amt)
{
String msg;
b.deposit(d_amt);
msg="Transaction Succesful";
try
{
b.withdraw(w_amt);
}catch(FundsInsufficientException fe)
{
fe=new FundsInsufficientException(b.amount,w_amt);
msg=String.valueOf(fe);
}
return msg;
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if(str.equals("Create Account"))
{
b=new BankAccount(Integer.parseInt(t[0].getText()),Integer.parseInt(t[1].getText()));
but1.setVisible(true);
l[2].setVisible(true);
l[3].setVisible(true);
t[2].setVisible(true);
t[3].setVisible(true);
but.setVisible(false);
l[0].setVisible(false);
l[1].setVisible(false);
t[0].setVisible(false);
t[1].setVisible(false);
lab1.setText("Account : "+b.accnum+", Current Balance : "+b.amount);
return;
}
else
{

lab.setText(testAccount(Integer.parseInt(t[2].getText()),Integer.parseInt(t[3].getText())));
lab1.setText("Account : "+b.accnum+", Current Balance : "+b.amount);
}
}
public static void main(String arg[])
{
GuiAccTest at=new GuiAccTest();
at.setTitle("Bank Account Tester");
at.setSize(600,200);
at.setVisible(true);
}
}
class NewWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
class BankAccount
{
int accnum;
int amount;
BankAccount(int num,int amt)
{
accnum=num;
amount=amt;
}
public void deposit(int amt)
{
amount=amount+amt;
}
public void withdraw(int amt) throws FundsInsufficientException
{
if(amt>amount)
throw new FundsInsufficientException(amount,amt);
else
amount=amount-amt;
}
}
class FundsInsufficientException extends Exception
{
int balance;
int withdraw_amount;
FundsInsufficientException(int bal,int w_amt)
{
balance=bal;
withdraw_amount=w_amt;
}
public String toString()
{
return "Your withdraw amount ("+withdraw_amount+") is less than the balance
("+balance+"). No withdrawal was recorded.";
}
}
OUTPUT:

init:
deps-jar:
Compiling 1 source file to C:\apple\build\classes
compile-single:
run-applet:
RESULT:

Thus the Swing program has been executed successfully.


6. EVENT HANDLING
AIM:
To write a java program for mouse Event handling.

ALGORITHM:
Step1 :
Start the program
Step2 :
Import the packages java.awt,java.awt.event and java.applet
Step 3:
Create the class ‘mouse’ that extends Applet and implements mouselistener and
mousemotion Listener.
Step 4:
Define the method init to perform that eventAction.
Step 5:
Define the method paint()
Step 6:
Create the HTML program with applet code.
Step 7:
Compile the program using java mouse.java
Step 8:
Execute the program using appletviewer mouse.html.
Step 9:
Stop the program.
//EVENT HANDLING

package event;
import java.awt.*;
importjava.awt.event.*;
importjava.applet.*;
/*<applet code="mouse.class" width=500 height=500>
* </applet>*/
public class mouse extends Applet implements MouseListener ,
MouseMotionListener
{
String msg="";
intmousex=0,mousey=0;
@Override
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent e)
{
setBackground(Color.pink);
msg="Mouse Clicked";
repaint();
}
public void mouseEntered(MouseEvent e)
{
setBackground(Color.white);
msg="Mouse Entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
setBackground(Color.blue);
msg="Mouse Exited";
repaint();
}
public void mousePressed(MouseEvent me)
{
setBackground(Color.red);
msg="Down";
repaint();
}
public void mouseRepleased(MouseEvent me)
{
setBackground(Color.yellow);
msg="Up";
repaint();
}
public void mouseDragged(MouseEvent me)
{
mousex=me.getX();
mousey=me.getY();
msg="**";
showStatus("Dragging mouse at" +mousex+ "," +mousey);
repaint();
}
public void mouseMoved(MouseEvent me)
{
showStatus("Moving mouse at"+me.getX()+","+me.getY());
}
public void paint(Graphics g)
{
g.drawString(msg,mousex,mousey);
}
public void mouseReleased(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
OUTPUT:

init:
deps-jar:
Compiling 1 source file to C:\event\build\classes
compile-single:
run-applet:
Key Event
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code=Keyevents.class width=200 height=200>
</applet>*/
public class Keyevents extends Applet implements KeyListener
{
int X=20,Y=30;
String msg="KeyEvents ===>";
public void init()
{
addKeyListener(this);
requestFocus();
setBackground(Color.green);
setForeground(Color.blue);
}
public void keyPressed(KeyEvent k)
{
showStatus("KeyDown");
int key=k.getKeyCode();
switch(key)
{
case KeyEvent.VK_UP:
showStatus("Move to Up");
break;
case KeyEvent.VK_DOWN:
showStatus("Move to Down");
break;
case KeyEvent.VK_LEFT:
showStatus("Move to Left");
break;
case KeyEvent.VK_RIGHT:
showStatus("Move to Right");
break;
}
repaint();
}
public void keyReleased(KeyEvent k)
{
showStatus("Key Up");
}
public void keyTyped(KeyEvent k)
{

msg+=k.getKeyChar();
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,X,Y);
}
}
RESULT:

Thus the Event Handling program has been executed successfully.


7. MESSAGE COMMUNICATION USING TCP/IP PROTOCOL

AIM:

To write a java program for Message Communication Using Tcp/Ip Protocol.

ALGORITHM:

Step 1:
Start the program.
Step 2:
Import the packages java.net and java.io.
Step 3:
Create the class ‘Server’.
Step 4:
If main(),create an object for BufferedReader and DatagramSocket.,Datagram Packet.
Step 5:
Close the Datagram socket using the Object created.
Step 6:
Compile the program using java server.java.
Step 7:
Create the class ‘Client’.
Step 8:
In main(), Create an Object for Buffered Reader,DatagramSocket and Datagram packet.
Step 9:
Receiving the message from Server.
Step 10:
Sending message to server.
Step 11:
Close the Datagram packet.
Step 12:
Compile the program using javac client.java.
Step 13:
Run the Server program using java server.
Step 14:
Run the client program using java client
Step 15:
Stop the Program.
SERVER.JAVA
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server


{

private static Socket socket;

public static void main(String[] args)


{
try
{

int port = 25000;


ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server Started and listening to the port 25000");

//Server is running always. This is done using this while(true) loop


while(true)
{
//Reading the message from the client
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String number = br.readLine();
System.out.println("Message received from client is "+number);

//Multiplying the number by 2 and forming the return message


String returnMessage;
try
{
int numberInIntFormat = Integer.parseInt(number);
int returnValue = numberInIntFormat*2;
returnMessage = String.valueOf(returnValue) + "\n";
}
catch(NumberFormatException e)
{
//Input was not a number. Sending proper message back to client.
returnMessage = "Please send a proper number\n";
}
//Sending the response back to the client.
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(returnMessage);
System.out.println("Message sent to the client is "+returnMessage);
bw.flush();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(Exception e){}
}
}
}

CLIENT.JAVA
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;

public class Client


{

private static Socket socket;

public static void main(String args[])


{
try
{
String host = "localhost";
int port = 25000;
InetAddress address = InetAddress.getByName(host);
socket = new Socket(address, port);

//Send the message to the server


OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);

String number = "2";


String sendMessage = number + "\n";
bw.write(sendMessage);
bw.flush();
System.out.println("Message sent to the server : "+sendMessage);

//Get the return message from the server


InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
System.out.println("Message received from the server : " +message);
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
//Closing the socket
try
{
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
OUTPUT:

init:
deps-jar:
compile-single:
run-single:
HI ARUL
Message from Server:
HI ARUL
Enter your Message:
HI
message from client:
HI i am client,send me a message
Enter your message:
WAT R U DOING VENKAT
Message from Server:
WAT R U DOING VENKAT
Enter your Message:
NOTHING TO DO K I HAVE WORK BYE
message from client:
NOTHING TO DO K I HAVE WORK BYE
Enter your Message:
K BYE…..
Message from Server:
K BYE.....
Enter your Message:
RESULT:

Thus the Message Communication Using Tcp/Ip Protocol program has been executed
successfully.
8. MESSAGE COMMUNICATION USING UDP PROTOCOL

AIM:

To write a java program for Message Communication Using UDP Protocol

ALGORITHM:

Step 1:
Start the program.
Step 2:
Import the packages java.net and java.io.
Step 3:
Create the class ‘Server’.
Step 4:
If main(),create an object for BufferedReader and DatagramSocket.,Datagram Packet.
Step 5:
Close the Datagram socket using the Object created.
Step 6:
Compile the program using java server.java.
Step 7:
Create the class ‘Client’.
Step 8:
In main(), Create an Object for Buffered Reader,DatagramSocket and Datagram packet.
Step 9:
Receiving the message from Server.
Step 10:
Sending message to server.
Step 11:
Close the Datagram packet.
Step 12:
Compile the program using javac client.java.
Step 13:
Run the Server program using java server.
Step 14:
Run the client program using java client
Step 15:
Stop the Program.
Client Program

import java.io.*;
import java.net.*;
public class client2
{
public static void main(String args[])throws Exception
{
DatagramSocket ds;
DatagramPacket p1,p2;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
byte arr[]=new byte[255];
String Input,Output;
InetAddress i=InetAddress.getLocalHost();
Output="\n Hai, i am client,send a message";
arr=Output.getBytes();
p1=new DatagramPacket(arr,arr.length,i,10000);
ds=new DatagramSocket();
ds.send(p1);
do
{
arr=new byte[255];
p1=new DatagramPacket(arr,255);
ds.receive(p1);
arr=p1.getData();
input=new String(arr);
System.out.println(Input);
if(Input.charAt(0)=='*')
{
break;
}
System.out.println("Enter your message:\n");
Output=br.readLine();
arr=new byte[255];
arr=Output.getBytes();
p2=new DatagramPacket(arr,arr.length,p1.getAddress(),p1.getPort());
ds.send(p2);
arr=p1.getData();
}
while(true);
}
}

Server Program

import java.io.*;
import java.net.*;
public class ser1
{
public static void main(String args[])throws Exception
{
DatagramSocket ds=new DatagramSocket(10000);
DatagramPacket p1,p2;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
byte arr[]=new byte[255];
String Input,Output;
do
{
p1=new DatagramPacket(arr,255);
ds.receive(p1);
arr=p1.getData();
Input=new String(arr);
System.out.println("message from client:\n");
System.out.println(Input);
if(Input.charAt(0)=='*')
{
break;
}
System.out.println("Enter your message:\n");
Output=br.readLine();
arr=new byte[255];
arr=Output.getBytes();
p2=new DatagramPacket(arr,arr.length,p1.getAddress(),p1.getPort());
ds.send(p2);
arr=p1.getData();
}
while(true);
ds.close();
}
}
OUTPUT:

init:
deps-jar:
compile-single:
run-single:
HI ARUL
Message from Server:
HI ARUL
Enter your Message:
HI
message from client:
HI i am client,send me a message
Enter your message:
WAT R U DOING VENKAT
Message from Server:
WAT R U DOING VENKAT
Enter your Message:
NOTHING TO DO K I HAVE WORK BYE
message from client:
NOTHING TO DO K I HAVE WORK BYE
Enter your Message:
K BYE…..
Message from Server:
K BYE.....
Enter your Message:
RESULT:

Thus the Message Communication Using UDP Protocol program has been executed
successfully.
9. STUDENT INFORMATION SYSTEM USING JDBC

AIM:
To write a java program for using jdbc connection.

ALGORITHM:
Step 1:
Start the program.
Step 2:
Import the packages java.sql.
Step 3:
Create the class ‘jdbc’.
Step 4:
In main(), create an object for connection.
Step 5:
Create a database for Microsoft Access.
Step 6:
Connect the database using Direct Manager.
Step 7:
With in Try block specify the instruction.
Step 8:
With in the help of catch the block handle the Error Exception.
Step 9:
Compile the program using java.jdbc.java.
Step 10:
Run the Program using java.jdbc.
Step 11:
Stop the program.
//JDBC
import java.sql.*;
public class jdbc
{
public static void main(String args[ ])
{
Connection conn=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:md");
String s="select *from kd";
Statement stmt=conn.createStatement();
ResultSetrset=stmt.executeQuery(s);
System.out.println("S.NO \t Name \t Job \t Salary");
while(rset.next())
{
System.out.println(rset.getString(1)+"\t"+rset.getString(2)+"\t"+rset.getSt
ring(3)+"\t"+rset.getString(4));
}
stmt.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output:

Init:
Deps-jar;
Compile-single:
Run-single:

S.no Name Job Salary

1 Venkat IAS 25000


2 Arul CEO 20000
3 Dhina Si 15000

BUILD SUCCESSFUL (total time:0 Seconds)


RESULT:

Thus the jdbc connection. Program has been executed successfully.


10. CLIENT/SERVER COMMUNICATION USING SERVLET

AIM:

To write a java program for Networking.

ALGORITHM:
Step 1:
Start the program.
Step 2:
Import the packages java.net and java.io.
Step 3:
Create the class ‘Server’.
Step 4:
If main(),create an object for BufferedReader and DatagramSocket.,Datagram Packet.
Step 5:
Close the Datagram socket using the Object created.
Step 6:
Compile the program using java server.java.
Step 7:
Create the class ‘Client’.
Step 8:
In main(), Create an Object for Buffered Reader,DatagramSocket and Datagram packet.
Step 9:
Receiving the message from Server.
Step 10:
Sending message to server.
Step 11:
Close the Datagram packet.
Step 12:
Compile the program using javac client.java.
Step 13:
Run the Server program using java server.
Step 14:
Run the client program using java client
Step 15:
Stop the Program.
//NETWORK PROGRAMMING
//SERVER

packagenps;
import java.net.*;
import java.io.*;
public class server
{
public static void main(String args[])throws Exception
{
DatagramSocket ds=new DatagramSocket(10000);
DatagramPacket p1,p2;
BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));
bytearr[]=new byte[255];
String input,output;
do
{
p1=new DatagramPacket(arr,255);
ds.receive(p1);
arr=p1.getData();
input=new String(arr);
System.out.println("message from client:\t");
System.out.println(input);
if(input.charAt(0)=='*')
{
break;
}
System.out.println("Enter your message:\t");
output=br.readLine();
arr=output.getBytes();
p2=new
DatagramPacket(arr,arr.length,p1.getAddress(),p1.getPort());
ds.send(p2);
arr=p1.getData();
}
while(true);
ds.close();
}
}
//CLIENT
packagenpc;
import java.io.*;
import java.net.*;
public class client
{
public static void main(String args[])throws Exception
{
DatagramSocket ds;
DatagramPacket p1,p2;
BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));
bytearr[]=new byte[255];
String input,output;
InetAddress i=InetAddress.getLocalHost();
output="hi,i am client,send me a message";
arr=output.getBytes();
p1=new DatagramPacket(arr,arr.length,i,10000);
ds=new DatagramSocket();
ds.send(p1);
do
{
arr=new byte[255];
p1=new DatagramPacket(arr,255);
ds.receive(p1);
arr=p1.getData();
input=new String(arr);
System.out.println("Message from Server:");
System.out.println(input);
if(input.charAt(0)=='*')
{
break;
}
System.out.println("Enter your Message:");
output=br.readLine();
arr=new byte[255];
arr=output.getBytes();
p2=new DatagramPacket(arr,arr.length,i,10000);
ds.send(p2);
arr=p1.getData();
}
while(true);
}}

OUTPUT:

init:
deps-jar:
compile-single:
run-single:
HI ARUL
Message from Server:
HI ARUL
Enter your Message:
HI
message from client:
HI i am client,send me a message
Enter your message:
WAT R U DOING VENKAT
Message from Server:
WAT R U DOING VENKAT
Enter your Message:
NOTHING TO DO K I HAVE WORK BYE
message from client:
NOTHING TO DO K I HAVE WORK BYE
Enter your Message:
K BYE…..
Message from Server:
K BYE.....
Enter your Message:
RESULT:

Thus client/server communication using servlet program is has been executed


successfully.
11. Webpage Using JSP

AIM:
To Develop a java program for using servelet in client and server of a program.

ALGORITHM:

Step 1:
Start the program.
Step 2:
To create a “HTML” code of the content in format only.
Step 3:
To Create a import “java.servlet http” HTTP servlet response and request.
Step 4:
To import the servlet class and extends “HTTP servlet”
Step 5:
It’s used to the try block statement and then print the statements.
Step 6:
The finally used to close statement.
Step 7:
The servelet is used to request and response the sevlet exception.
Step 8:
Stop the program.
//SERVLET
<%-----
Document:index
Created on : feb-26-013 ,3:52:47 pm
Author:Administrator
-----%>
<%@page content type=”text/html”pageEncoding=”UTF-8”%>
<!DOCTYPEHTML>
<html>
<head>
<meta http-equiv=”content-type”content=”text/html;charset=UTF-8”>
<title>Jsp page</title>
</head>
<body bgcolor=”pink”>
<h1>Govt Arts College,Krishnagiri.</h1>
<form action=”servletdesign” method=”post”>
FirstName:<inputtype=”text”name=”firstname” size=”20”>
Surname:<input type=”text”name=”surname”size=”20”>
<br/><br/>
<input type=”submit”value=”submit”>
</form>
</body>
</html>
import java.io.IOException;
import java.io.printwrite;
import java.servlet.servletExecption;
import java.servlet.http.httpservlet;
import java.servlet.httpservlet Request;
import java.servlet.httpservlet Response;

public class serveldesign extends httpservlet


{
Protected void processrequest(HTTPservlet,request,HTTPservletResponse
respose)throws servletException
{
Response.setcontenttype(“text/html;charset=utf-8”);
Printwriter out=response.getwriter();
String firstname=request.getparameter(“firstname”)tostring();
String surname=request.getparameter(“surname”)tostring();
Try
{
Out.println(“<html>”);
Out.println(“<head>”);
Out.println(“<title>servlet Geretingservlet</title>”);
Out.println(“</head>”);
Out.println(“<body>”);
Out.println(“<h1>Govt Arts College”+request.getcontentpath()+</h1>);
Out.println(“<p>Wellcome”+firstname+” ”+surname+”</h1>”);
Out.println(“</body>”);
Out.println(“</html>”);
}
Finaly
{
Out.close();
}
}
/**
*
*@param request
*@param response
*@throws sservletexception
*@throws IOException
*/
@override
Protected void dopost(HttpservletRequest request,httpservletResponse
response)throws servletException,IOException
{
String first name=request.get parameter(“firstname”)tostring();
System.out.println(“firstname=”+firstname);
Prosessrequest(requestresponse);
}
}
Output:
RESULT:

Thus the Servlet program is has been executed successfully.


12. Implementation of RMI

AIM:
To write a java program for using rmi with client and server method.

ALGORITHM:
Step 1:
Start the program.
Step 2:
Import the package rmi.
Step 3:
Create a interface message.
Step 4:
Create a class messageimpl.
Step 5:
Using the concept of override.
Step 6:
Declear the two variable try but does’t return the any value.
Step 7:
Create a class server in server side.
Step 8:
Print the execution by appropriate try and catch block correctly.
Step 9:
Client side interface name message.
Step 10:
Create a class client.
Step 11:
Using the dottest method.
Step 12:
Calling in the server method.
Step 13:
Add the number (or) variables.
//RMI
package test;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class Msg extends UnicastRemoteObject Implements NewInterface
{
public Msg()throws RemoteException
{

}
public void sayHello(String name)throws RemoteException
{
System.out.println("hello"+name);
}
public void add(int x,int y) throws RemoteException
{
System.out.println("the value is"+(x+y));
}
}
package test;
import newpackage.*;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.RemoteException;
public class client
{
private void doTest()
{
try
{
Registry myRegistry=LocateRegistry.getRegistry("127.0.0.1",1099);
Msg impl=(Msg)myRegistry.lookup("myMessage");
impl.sayHello("dhinakaran");
impl.add(10,20);
System.out.println("message sent");
}
catch(RemoteException|NotBoundException e)
{

public static void main(String[] args)


{
client c=new client();
c.dotest();
}

private void dotest() {


throw new UnsupportedOperationException("Not yet implemented");
}
}
Package test;

import newpackage.*;
import java.rmi.Remote;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class server
{
private void startServer()
{
try
{
Registry registry=LocateRegistry.createRegistry(1099);
registry.rebind("myMessage",(Remote)new Msg());
}
catch(Exception e)
{

}
System.out.println("system is ready");
}
public static void main(String[] args)
{
server s=new server();
s.startserver();

private void startserver() {


throw new UnsupportedOperationException("Not yet implemented");
}
OUTPUT:
run:
system is ready

run:
Message Sent
BUILD SUCCESSFUL(total time:0seconds)

Run
System is ready
Hello Tendulker
The value is:30
RESULT:

Thus the RMI program is has been executed successfully.


GOVERNMENT ARTS COLLEGE (MEN)
KRISHNAGIRI.

DEPARTMENT OF COMPUTER SCIENCE

PROGRAMMING IN……………………………………………………………………………..

This is to certify that this is a bonafide RECORD OF WORK done by ………………………….

student of the M.Sc., COMPUTER SCIENCE, during the year 2021-2022 in the Computer Lab of Govt.

Arts College (Men), Krishnagiri.

Head Department of computer Science Staff in-Charge

UNIVERSITY REGISTER NUMBER:…………………………………………………….

Submitted for the university practical examination held on……………….........................at Govt. Arts

College (MEN), Krishnagiri.

Internal & External Examiners


1.
2.

You might also like