Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views

Final Advance Java2019

The document describes a Java program assignment to demonstrate networking using sockets. It involves implementing programs for a server and client with a single server and single client, and also a single server with multiple clients. Code snippets are provided for a basic server program that establishes a server socket and accepts a connection from a single client, receiving and sending data over the socket connection.

Uploaded by

Rohan Rathod
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Final Advance Java2019

The document describes a Java program assignment to demonstrate networking using sockets. It involves implementing programs for a server and client with a single server and single client, and also a single server with multiple clients. Code snippets are provided for a basic server program that establishes a server socket and accepts a connection from a single client, receiving and sending data over the socket connection.

Uploaded by

Rohan Rathod
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Assignment1.

Write a Java program that connects to a database using JDBC


and does add, delete, modify and retrieve operations. Create Appropriate GUI
using awt for user interaction.

Index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<center>
<h1> JDBC Program</h1>
<form action="jspdb.jsp" method="post">
Roll No: <input type="text" name="t1"><br><br>
Student Name: <input type="text" name="t2"><br><br>
<input type="submit" name="b" value="insert">
<input type="submit" name="b" value="update">
<input type="submit" name="b" value="delete">
<input type="submit" name="b" value="view">
</form>

</center>

</body>
</html>

Jspdb.jsp

<%@page import="java.sql.*" %>


<%@page import="java.sql.Connection" %>
<%@page import="java.sql.DriverManager" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String rn=request.getParameter("t1");
int a=Integer.parseInt(rn);

1
String name=request.getParameter("t2");
int i;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection("jdbc:odbc:dsn3"," "," ");
Statement st=c.createStatement();;
String btn=request.getParameter("b");
if(btn.equals("update"))
{

i=st.executeUpdate("update stud set name='"+name+"'whererno="+a+"");


out.println("record update");
}
if(btn.equals("delete"))
{
i=st.executeUpdate("delete * from stud where rno="+a+"");
out.println("record delete");
}
if(btn.equals("insert"))
{
i=st.executeUpdate("insert into stud values("+a+",'"+name+"')");
out.println("record insert");
}
if(btn.equals("view"))
{
ResultSet rs=st.executeQuery("select * from stud");
out.println("student detail<br>");
while(rs.next())
{
out.println(rs.getInt("rno")+" ");
out.println(rs.getString("name"));
out.println("<br>");
}
out.println("record view");
}
}
catch(Exception ee)
{
out.println(ee);
}
%>
</body>
</html>

2
Output:-

3
Assignment2. Write a Java program(s) that demonstrates the use of Collection
Classes.
package collectionclass1;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Enumeration;

import java.util.HashMap;

import java.util.HashSet;

import java.util.Hashtable;

import java.util.Iterator;

import java.util.LinkedHashSet;

import java.util.LinkedList;

import java.util.Map;

import java.util.Set;

import java.util.Stack;

import java.util.Vector;

public class Collectionclass1

public static void main(String[] args)

System.out.println("****Arrays Collection Class****"); //1

int ar[]={10,20,30};

Arrays.sort(ar);

System.out.println("Array Element are:-"+Arrays.toString(ar));

int index=Arrays.binarySearch(ar,20);

4
System.out.println("Index Position of Element 20 is:-"+index);

int[] cp=Arrays.copyOf(ar,3);

System.out.println("Creating Arrays Copy"+Arrays.toString(cp));

System.out.println("****ArrayList collection class****"); //2

ArrayList a=new ArrayList();

a.add("IMCA");

a.add("MCA");

a.add("MBA");

a.add("BCA");

a.add("BBM");

a.add("BBA");

int n= a.size();

System.out.println("Size of Array:->"+n);

System.out.println("Element in Array:-");

Iterator it1=a.iterator();

while(it1.hasNext())

System.out.println(it1.next());

a.remove("MCA");

System.out.println("Remove Element MCA");

System.out.println("Element in Array:-");

Iterator it=a.iterator();

while(it.hasNext())

5
{

System.out.println(it.next());

System.out.println("Searching Contain IMCA:"+a.contains("IMCA"));

System.out.println( "Is ArrayList Empty:->"+a.isEmpty());

System.out.println("****HashMap collection class****"); //3

HashMap hm=new HashMap();

hm.put(100,"Amul");

hm.put(101,"Pihu");

hm.put(102,"Rahul");

int s=hm.size();

System.out.println("Size of Array:->"+s);

Set s1=hm.entrySet();

System.out.println("Elements in HashMap are:-");

Iterator i=s1.iterator();

while(i.hasNext())

Map.Entry m=(Map.Entry)i.next();

System.out.print(m.getKey());

System.out.println("-"+m.getValue());

boolean ck=hm.containsKey(101);

6
System.out.println("Present Contain Key 101:-"+ck);

boolean cv=hm.containsValue("Priya");

System.out.println("Present Contain Value Priya:-"+cv);

System.out.println("Is HashMap is empty:-"+hm.isEmpty());

hm.clear();

int sizehm=hm.size();

System.out.println("HashMap Size After Clearing Elements-:"+sizehm);

System.out.print("****Linked List Collection Class****\n"); //4

LinkedList l=new LinkedList();

l.addAll(a);

System.out.println("Size of Linked List:-"+l.size());

System.out.println("Linked List Elements:"+l);

System.out.println("Searching Contain IMRD:"+l.contains("IMRD"));

l.clear();

System.out.println("After Clearing Linked List Elements are:-"+l.size());

System.out.println("Is Linked List is empty:-"+l.isEmpty());

System.out.print("****Linked List HashSet Collection Class****\n");//5

LinkedHashSet lh=new LinkedHashSet();

lh.addAll(a);

System.out.println("Elements:"+lh);

System.out.println("Searching Contains MCA:"+ lh.contains("MCA"));

lh.clear();

System.out.println("Clearing LinkedList Hashset"+l.size());

7
System.out.println("****Stack Collection Class****"); //6

Stack st=new Stack();

st.push("A");

st.push("B");

st.push("C");

System.out.println("Stack Elements are:-");

Enumeration e=st.elements();

while(e.hasMoreElements())

System.out.println(e.nextElement());

System.out.println("Last Element of Array is:-"+st.peek());

System.out.println("Deleting Element :-"+st.pop());

int sizes=st.size();

System.out.println("After Deleting Size of Stack:-"+sizes);

st.clear();

System.out.println("Clearing the Stack:-"+st.size());

System.out.println("****Vector Collection Class****"); //7

Vector v=new Vector();

v.add("Raj");

v.add("Anand");

v.add("Rahul");

8
v.add("Hemant");

System.out.println("Element in Vector:-");

Iterator v1=v.iterator();

while(v1.hasNext())

System.out.println(v1.next());

v.remove("Rahul");

System.out.println("After Removing Element 'Rahul'");

System.out.println("Element in Vector:-");

Iterator v2=v.iterator();

while(v2.hasNext())

System.out.println(v2.next());

int sizev=v.size();

System.out.println("Size of Elements in Vector :-"+sizev);

System.out.println( "Is Vector Empty:->"+v.isEmpty());

v.clear();

int sizev1=v.size();

System.out.println("Vector Size After Clearing Elements:- "+sizev1);

System.out.println("****HashTable Collection Class"); //8

Hashtable ht=new Hashtable();

ht.put(1,"Kajal");

9
ht.put(2,"Vaishnavi");

ht.put(3,"Ruchita");

ht.put(4,"Monisa");

Set sht=ht.entrySet();

System.out.println("Elements in HashTable are:-");

Iterator hti=sht.iterator();

while(hti.hasNext())

Map.Entry m=(Map.Entry)hti.next();

System.out.print(m.getKey());

System.out.println("-"+m.getValue());

ht.remove(3);

System.out.println("After Removing Element 'Ruchita'");

Iterator htj=sht.iterator();

while(htj.hasNext())

Map.Entry m=(Map.Entry)htj.next();

System.out.print(m.getKey());

System.out.println("-"+m.getValue());

boolean b1=ht.containsKey(4);

System.out.println("Present Contain Key 4:-"+b1);

boolean b2=ht.containsValue("priya");

10
System.out.println("Present Contain Value priya:-"+b2);

ht.clear();

int sizeht=ht.size();

System.out.println("HashTable Size After Clearing Elements-:"+sizeht);

System.out.println("****HashSet Collection Class****"); //9

HashSet hs=new HashSet();

hs.addAll(a);

System.out.println("Element in HashSet Are:-");

Iterator hs1=hs.iterator();

while(hs1.hasNext())

System.out.println(hs1.next());

hs.remove("BBA");

System.out.println("After Removing Element 'BBA'");

System.out.println("Element in HashSet:-");

Iterator hs2=hs.iterator();

while(hs2.hasNext())

System.out.println(hs2.next());

int sizehs=hs.size();

System.out.println("Size of Elements in HashSet :-"+sizehs);

System.out.println( "Is HashSet Empty:->"+hs.isEmpty());

11
hs.clear();

System.out.println("HashSet Size After Clearing Elements:- "+hs.size());

Output:-

12
13
14
15
Assignment3. Implement the Java program(s) for server and client to
demonstrate networking in Java using Sockets. (Single server and single
client, Single server and multiple clients).
• Single server and single client

Server

package socket1;
import java.util.*;
import java.io.*;
import java.net.*;
public class serverside
{
public static void main(String s[]) throws Exception
{
String str="abc ",str1="xyz ";
ServerSocket ss=new ServerSocket(2553);
Socket s1=ss.accept();
DataInputStream dis=newDataInputStream(s1.getInputStream());
DataOutputStream dos=new DataOutputStream(s1.getOutputStream());
DataInputStream br=new DataInputStream(System.in);
while(!str.equals("stop"))
{
str=dis.readUTF();
System.out.println("client says "+str);
str1=br.readLine();
dos.writeUTF(str1);
dos.flush();
}
dis.close();
dos.close();
s1.close();
ss.close();
}
}

Client

package socket1;
import java.util.*;
import java.io.*;
import java.net.*;

16
public class clientside {
public static void main(String s[]) throws Exception
{
String str="hi ",str1="hey ";
Socket s1=new Socket("localhost",2553);
DataInputStream dis=new DataInputStream(s1.getInputStream());
DataOutputStream dos=new DataOutputStream(s1.getOutputStream());
DataInputStream br=new DataInputStream(System.in);
while(!str.equals("stop"))
{
str=br.readLine();
dos.writeUTF(str);
dos.flush();
str1=dis.readUTF();
System.out.println(str1);
}
dis.close();
dos.close();
s1.close();
}
}

Output:-

Client: Server:

17
Single server and multiple clients

Server

package multisc;
import java.util.*;
import java.net.*;
import java.io.*;
public class server
{

public static void main(String s[]) throws Exception


{
Socket sa=null;
ServerSocket ss2=null;
System.out.println("server listening ");
try
{
ss2=new ServerSocket(4445);
}
catch(IOException e)
{
System.out.println("server error");
}
while(true)
{
try
{
sa=ss2.accept();
System.out.println("connetion established");
ServerThread st =new ServerThread(sa);
18
st.start();
}
catch (Exception e)
{
System.out.println("connetion error");
}
}
}
}
class ServerThread extends Thread
{
String line=null;
DataInputStream is =null;
PrintWriter od=null;
Socket s1=null;
public ServerThread(Socket s)
{
s1=s;
}
public void run()
{
try
{

is = new DataInputStream(s1.getInputStream());
od = new PrintWriter(s1.getOutputStream());

line=is.readLine();

while(!line.equals("QUIT"))
{
od.println(line);
od.flush();
System.out.println("response to client "+line);
line=is.readLine();

}
is.close();
od.close();

19
s1.close();

}
catch(IOException ie)
{
System.out.println("socket close error");
}
}
}

Client

package multisc;
import java.util.*;
import java.net.*;
import java.io.*;

public class client


{

public static void main(String s[]) throws Exception


{
Socket s1=null;
String line=null;
DataInputStream br=null;
DataInputStream is=null;
PrintWriter os=null;
try
{
s1=new Socket("localhost",4445);
br=new DataInputStream(System.in);
is=new DataInputStream(s1.getInputStream());
os=new PrintWriter(s1.getOutputStream());

}
catch (IOException e)
{
System.err.print("IO Exception");

}
System.out.println("Enter data to echo server (enter QUIT to end) :-> ");
String res=null;
try

20
{
line=br.readLine();
while (line.compareTo("QUIT")!=0)
{
os.println(line);
os.flush();
res=is.readLine();
System.out.println("server response :-> "+res);
line=br.readLine();
}
is.close();
os.close();
br.close();
s1.close();
System.out.println("close connection ");
}
catch (IOException e)
{
System.out.println("socket read error");
}
}
}

Output:-

Server.java

21
Client

Client 1:

Client 2:

Client 3:

22
23
Assignment4. Write a Java program(s) that demonstrates the use of RMI
technology
Adder.java

package rmi;

import java.rmi.Remote;

interface Adder extends Remote


{
public int add(int x,int y) throws Exception;
}

AdderRemote.java

package rmi;

import java.util.*;
import java.io.*;
import java.rmi.server.*;
import java.rmi.*;

public class AdderRemote extends UnicastRemoteObject implements Adder


{
AdderRemote() throws RemoteException
{
super();
}
public int add(int x,int y)
{
return x+y;
}
}
client.java

package rmi;

import java.util.*;
import java.io.*;
import java.rmi.registry.*;
import java.rmi.*;

24
public class client.java

{
public static void main(String z[])
{
try
{
Adder stub=(Adder)Naming.lookup("rmi://localhost:6000/ss");
int i=stub.add(120,10);
System.out.println("addition"+i);
}
catch(Exception e)
{
System.out.println("remi server error");
}
}
}

Server.java

package rmi;

import java.util.*;
import java.io.*;
import java.rmi.registry.*;
import java.rmi.*;
public class Server
{
public static void main(String z[]) throws Exception
{
Adder stub=new AdderRemote();
try

25
{
Registry reg=LocateRegistry.createRegistry(6000);
reg.rebind("ss",stub);
}
catch(Exception e)
{
System.out.println("remi server error");
}
}
}

Output:-

26
Assignment5. Write a Java program that implements a multi-thread
application that has three threads. First thread generates random integer
every 1 second and if the value is even, second thread computes the square of
the number and prints. If the value is odd, the third thread will print the value
of cube of the number.

package javaapplication3;
import java.util.Random;
public class JavaApplication3 {
static int i;
static class ThreadDemo1 extends Thread
{
@Override
public void run()
{
try {

Random random = new Random();


int r=random.nextInt(10);
System.out.println(r);
i=r;

Thread.sleep(500);

} catch (Exception e) {
System.out.println("Thread interupted "+e);
}
}
}
static class ThreadDemo2 extends Thread
{
@Override
public void run()
{
try {

if(i%2==0)
System.out.println(i*i);
Thread.sleep(500);

} catch (Exception e) {

System.out.println("Thread Interupted "+e);


}
}
}

27
static class ThreadDemo3 extends Thread
{
@Override
public void run()
{
try {

if(i%2!=0)
System.out.println(i*i*i);
Thread.sleep(500);

} catch (Exception e) {
System.out.println("Thread interupted "+e);
}
}
}
public static void main(String[] args) {
ThreadDemo1 T1=new ThreadDemo1();
T1.start();
ThreadDemo2 T2=new ThreadDemo2();
T2.start();
ThreadDemo3 T3=new ThreadDemo3();
T3.start();
}
}

28
Assignment6. Create a Simple Java Web Application Using Servlet, JSP and
JDBC.

Index.jsp

<html>

<head>

<title>TODO supply a title</title>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>

<div>TODO write content</div>

<form action="NewServlet" method="get">

<input type="submit" value="click">

</form>

</body>

</html>

Servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

try {

PrintWriter out=response.getWriter();

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection c=DriverManager.getConnection("jdbc:odbc:dsn1","","");

Statement st=c.createStatement();

ResultSetrs=st.executeQuery("select *from student");

29
out.println("Record are as Folllows\n");

while(rs.next())

out.println(rs.getInt("rno")+" "+" "+rs.getString("sname"));

} catch (IOException | ClassNotFoundException | SQLException e) {

System.out.println("Exp= "+e);

Output:

Before

After

30
Assignment7. Create a Java Web Application which show the login page with
Username, password and login And register button.On login button it validate
the user name and password entered by user with database information
stored in login info database table if it is correct then show message login
successful and open home page which will show all the record of login info
table into grid. If information is incorrect then show message invalid
credentials.
On Register Button show the register page, on this page show the username,
password, date ofBirth field to save this information into login info database
table.
On register page show two button save and cancel.
On save button record should be saved to database table login info and login
page should beOpened. Cancel it will returns back to login page.

Index.jsp

<%@page import="java.sql.DriverManager"%>

<%@page import="java.sql.Connection"%>

<%@page import="java.sql.Statement"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html;

charset=UTF-8">

<title>Login</title>

</head>

<body>

<form action="Home.jsp" method="POST">

<br><br><br><br>

<center>

31
<h1>Login User</h1>

User Name<input type="text" name="uname"><br><br>

Password <input type="text" name="pass"><br><br>

<input type="Submit" name="submit"

value="Login">&nbsp;&nbsp; &nbsp;

<a href="Registration.jsp"><input type="Button"

value="Register"></a></center>

</form>

<%

try

String uName,pass,dob;

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection co =

DriverManager.getConnection("jdbc:odbc:dsn3","","");

Statement st = co.createStatement();

uName = request.getParameter("uname");

pass = request.getParameter("pass");

dob = request.getParameter("dob");

st.executeUpdate("insert into

student(username,password,dob)

values('"+uName+"','"+pass+"','"+dob+"')");

catch (Exception e)

32
System.out.println("Error : "+e);

%>

</body>

</html>

Home.jsp

<%@page import="java.sql.DriverManager"%>

<%@page import="java.sql.Statement"%>

<%@page import="java.sql.Connection"%>

<%@page import="java.sql.ResultSet"%>

<%@page import="java.io.PrintWriter"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html;

charset=UTF-8">

<title>Home</title>

</head>

<body>

<%

String uName,pass,dob;

PrintWriter o = response.getWriter();

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

33
Connection co =

DriverManager.getConnection("jdbc:odbc:dsn3","","");

Statement st = co.createStatement();

try

uName = request.getParameter("uname");

pass = request.getParameter("pass");

dob = request.getParameter("dob");

ResultSet rs = st.executeQuery("Select * from student

where username='"+uName+"' and password='"+pass+"'");

if(rs.next())

%><center><h1>Login Success</h1><%

ResultSet rs1 = st.executeQuery("Select *from student");

%><table border='1' cellpadding='5'><%

while(rs1.next())

{%>

<tr>

<td> <%= rs1.getString("username") %> </td>

<td> <%= rs1.getString("password") %> </td>

<td> <%= rs1.getDate("dob") %> </td>

</tr>

<%}%>

</table></center> <%

34
else

%>Plzz Enter Valid Username & Password<%

catch (Exception e)

System.out.println("Error : "+e);

%>

</body>

</html>

Registration.jsp

<html>

<head>

<meta http-equiv="Content-Type" content="text/html;

charset=UTF-8">

<title>Registration</title>

</head>

<body>

<form action="index.jsp" method="POST">

<br><br><br><br>

<center>

<h1>Register User</h1>

35
User name <input type="text" name="uname"><br><br>

Password <input type="text" name="pass" ><br><br>

DOB <input type="date" name="dob"><br><br>

<input type="Submit" name="submit"

value="Save">&nbsp;&nbsp; &nbsp;

<a href="index.jsp"><input type="Button"

value="Cancel"></a></center>

</form>

</body>

</html>

Output:

36
37
Assignment8. Write a Java program(s) that demonstrates Java Bean.

Index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8">
<title>JSP Page</title>
</head>
<body>
<h1> demonstrates Java Bean <h1>
<jsp:useBean id="student" class="mypackage.packg">
<jsp:setProperty name="student" property="rno" value="1" />
<jsp:setProperty name="student" property="sname" value="Monisa" />
</jsp:useBean>
Roll no <jsp:getProperty name="student" property="rno" /><br>
Student Name <jsp:getProperty name="student" property="sname"/>
</body>
</html>

Package .java

package mypackage;
import java.io.Serializable;

public class packg implements Serializable


{
private int rno;
String sname;
public packg()
{

}
public void setrno(int r)
{
rno=r;
}
public int getrno()
{
return rno;
}
public void setsname(String sn)

38
{
sname=sn;
}
public String getsname()
{
return sname;
}
}

Output:-

39
Assignment9. Write a Java program(s) that demonstrates EJB.

calbean.java

package sessionBean;

import javax.ejb.Stateless;

@Stateless

public class calbean implements calbeanLocal

@Override

public Integer addition(int a, int b)

return (a+b);

addijsp.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF8">

<title>JSP Page</title>

</head>

<body>

<h1>Hello World!</h1>

40
<form action="calservelet">

First No<input type="text" name="t1"><br>

Second No<input type="text" name="t2"><br>

<input type="submit" name="add" value="add">

</form>

</body>

</html>

calservelet

package sessionBean;

import java.io.IOException;

import java.io.PrintWriter;

import javax.ejb.EJB;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class calservelet extends HttpServlet {

@EJB

private calbeanLocal calbean;

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

41
try (PrintWriter out = response.getWriter())

out.println("<!DOCTYPE html>");

out.println("<html>");

out.println("<head>");

out.println("<title>Servlet calservelet</title>");

out.println("</head>");

out.println("<body>");

int a=Integer.parseInt(request.getParameter("t1"));

int b=Integer.parseInt(request.getParameter("t2"));

out.println("<h1>Sum = " +calbean.addition(a, b)+ "</h1>");

out.println("</body>");

out.println("</html>");

calbeanLocal.java

package sessionBean;

import javax.ejb.Local;

@Local

public interface calbeanLocal

Integer addition(int a, int b);

42
Output:-

43
44

You might also like