Professional Documents
Culture Documents
Advance Java.
Advance Java.
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
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:
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:
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++;
}
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:
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.*;
init:
deps-jar:
Compiling 1 source file to C:\balls\build\classes
compile-single:
run-applet:
RESULT:
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:
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:
AIM:
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;
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;
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:
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:
AIM:
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:
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;
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)
{
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();
run:
Message Sent
BUILD SUCCESSFUL(total time:0seconds)
Run
System is ready
Hello Tendulker
The value is:30
RESULT:
PROGRAMMING IN……………………………………………………………………………..
student of the M.Sc., COMPUTER SCIENCE, during the year 2021-2022 in the Computer Lab of Govt.
Submitted for the university practical examination held on……………….........................at Govt. Arts