You are on page 1of 23

150160107115

Practical 1
Aim: Create chat application using either TCP or UDP.

Server.java

import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Server {
public static void main(String[] args) throws Exception
{
Scanner sc= new Scanner(System.in);
ServerSocket ss=new ServerSocket(7886);
Socket s=ss.accept();
DataInputStream din=new DataInputStream(s.getInputStream());
String str;
str=din.readUTF();
System.out.println("Client:\t"+str);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
while(true)
{
str=din.readUTF();
System.out.print("Client:\t"+str);
if(str.equals("bye")) {
break;
}
System.out.print("Server:\t");
str=sc.nextLine();
if(str.equals("bye")) {
dout.writeUTF(str);
break;
}
dout.writeUTF(str);
}
dout.close();
din.close();
s.close();
ss.close();
sc.close();
}
}

Client.java

package practical1_tcp;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client
{

1
CE | 6th
150160107115
public static void main(String[] args) throws Exception
{
Scanner sc= new Scanner(System.in);
Socket s=new Socket("localhost",7886);
if(s.isConnected())
{
System.out.println("Connected to server");
}
String str="Start Chat....................................................................................";
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
DataInputStream din=new DataInputStream(s.getInputStream());
dout.writeUTF(str);
System.out.println(str);
while(true)
{
System.out.print("Client:\t");
str=sc.nextLine();
if(str.equals("bye")) {
dout.writeUTF(str);
break;
}
dout.writeUTF(str+"\n");
str=din.readUTF();
System.out.println("Server:\t"+str);
if(str.equals("bye")){
break;
}
}
dout.close();
din.close();
s.close();
sc.close();
}
}

Output:

2
CE | 6th
150160107115

Practical 2
Aim: Implement TCP Server for transferring files using Socket and ServerSocket.

Server.java

import java.io.*;
import java.net.*;
class Server
{
public static void main(String args[]) throws Exception
{
ServerSocket ss=new ServerSocket(7777);
Socket s=ss.accept();
System.out.println("connected..........");
FileInputStream fin=new FileInputStream("/home/zero1ce/eclipse-workspace-
java/practical/prac2/practical2/Send.txt");
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
int r;
while((r=fin.read())!=-1)
{
dout.write(r);
}
System.out.println("\nFile tranfer Completed");
s.close();
fin.close();
ss.close();
}

Client.java

import java.io.*;
import java.net.*;
public class Client {

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


{
Socket s=new Socket("localhost",7777);
if(s.isConnected())
{
System.out.println("Connected to server");
}
FileOutputStream fout= new FileOutputStream("/home/zero1ce/eclipse-workspace-
java/practical/prac2/practical2/received.txt");
DataInputStream din=new DataInputStream(s.getInputStream());
int r;
while((r=din.read())!=-1)
{
fout.write((char)r);
}
s.close();
3
CE | 6th
150160107115
fout.close();
}

Output:

4
CE | 6th
150160107115

Practical 3
Aim: Implement TCP Server for transferring files using Socket and ServerSocket.

Server.java
import java.io.*;
import java.net.*;
class Server
{
public static void main(String args[]) throws Exception
{
ServerSocket ss=new ServerSocket(7778);
Socket s=ss.accept();
System.out.println("connected..........");
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
int i=0;
int n=din.readInt();
int a[]=new int[n];
System.out.println("data:");
System.out.println("Receiving Data....");
for(i=0;i<n;i++)
{
a[i]=din.readInt();
}
System.out.println("Data Received");
System.out.println("Sorting Data........");
for (int j=1;j<n;j++){
int key=a[j];
i = j-1;
while ((i>-1) && (a[i]>key)){
a[i+1]=a[i];
i--;
}
a[i+1]=key;
}
System.out.println("Data Sorted");
System.out.println("Sending Data........");
for(i=0;i<n;i++)
{
dout.writeInt(a[i]);
}
System.out.println("\nData Sent Successfully");
s.close();
ss.close();
}
}

5
CE | 6th
150160107115
Client.java

import java.io.*;
import java.net.*;
class Server
{
public static void main(String args[]) throws Exception
{
ServerSocket ss=new ServerSocket(7778);
Socket s=ss.accept();
System.out.println("connected..........");
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
int i=0;
int n=din.readInt();
int a[]=new int[n];
System.out.println("data:");
System.out.println("Receiving Data....");
for(i=0;i<n;i++)
{
a[i]=din.readInt();
}
System.out.println("Data Received");
System.out.println("Sorting Data........");
for (int j=1;j<n;j++){
int key=a[j];
i = j-1;
while ((i>-1) && (a[i]>key)){
a[i+1]=a[i];
i--;
}
a[i+1]=key;
}
System.out.println("Data Sorted");
System.out.println("Sending Data........");
for(i=0;i<n;i++)
{
dout.writeInt(a[i]);
}
System.out.println("\nData Sent Successfully");
s.close();
ss.close();
}
}

6
CE | 6th
150160107115
Output:

7
CE | 6th
150160107115

Practical 4
Aim: Implement Concurrent TCP Server programming in which more than one
client can connect and communicate with Server for sending the string and server
returns the reverse of string to each of client.

Server.java
import java.net.*;
import java.io.*;

public class Server


{
public static void main(String[] args)throws Exception
{
int count=1;
System.out.println("Server is running...................");
ServerSocket ss=new ServerSocket(7878);
while(true)
{
new RevThread(ss.accept(),count).start();
System.out.println(count+" client connected");
count++;
}
}
}

class RevThread extends Thread


{
Socket s=null;
int n;
public RevThread(Socket socket,int count)
{
s=socket;
n=count;
}
public void run()
{
try
{
while(true)
{
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
System.out.println("receiving from client "+n);
String str=din.readUTF();
System.out.println("processing data of Client "+n);
StringBuffer rev=new StringBuffer();
rev=rev.append(str);
rev=rev.reverse();

8
CE | 6th
150160107115
String revStr=new String(rev);
System.out.println("sending to client "+n);
dout.writeUTF(revStr);
}
}
catch(IOException e)
{
System.out.println(e);
}
}
}

Client.java

import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Client
{
public static void main(String[] args) throws Exception
{
Socket s=new Socket("localhost",7878);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
DataInputStream din=new DataInputStream(s.getInputStream());
Scanner sc=new Scanner(System.in);
if(s.isConnected())
{
System.out.println("Connected to Server....");
}
while(true)
{
System.out.println("Enter String to reverse:");
String str=sc.nextLine();
dout.writeUTF(str);
String rev=din.readUTF();
System.out.println("Reversed String:\t"+rev);
}
}
}

Output:

9
CE | 6th
150160107115

10
CE | 6th
150160107115

Practical 5
Aim: Write a Program to use Statement object to make JDBC connectivity.
import java.sql.*;
public class statement {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection co = DriverManager.getConnection("jdbc:mysql://localhost:3306/Milan" ,
"root" , "naruto");
Statement st = co.createStatement();
if(!st.isClosed()) {
System.out.println("jdbc to mysql connection sucessfull.");
}

}
}
Output:

11
CE | 6th
150160107115

Practical 6
Aim: Write a program to make use the PreparedStatement object to insert student
record to database.

import java.sql.*;
public class PRstatement {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Connection cn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/Milan" , "root" ,
"naruto");
String Query = "insert into student(rollno,name,age) values (?,?,?)";
PreparedStatement ps = cn.prepareStatement(Query);
ps.setString(1, "1");
ps.setString(2, "milan");
ps.setString(3,"20");
ps.executeUpdate();
System.out.println("Value inserted to the database");
}
}
Output:

12
CE | 6th
150160107115

Practical 7
Aim: Write a program to utilize a stored procedure in a java Program using the
Callable statement object.
import java.sql.*;
public class Callable {
public static void main(String[] args) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Milan","root","naruto");
Statement stm=con.createStatement();
stm.execute("CREATE PROCEDURE "+"Milan"+".insert_Student (IN rollno INT,IN stdname
varchar(45),IN age INT) "+
"BEGIN "+
"INSERT "+
"INTO student "+
"VALUES(rollno,stdname,age); "+
"END");
CallableStatement stmt=con.prepareCall("{call Milan.insert_student(?,?,?)}");
stmt.setInt(1,1);
stmt.setString(2,"Milan");
stmt.setInt(3,19);
stmt.execute();
System.out.println("data base updated");
}
}

Output:

13
CE | 6th
150160107115

Practical 8
Aim: Create a simple servlet which will display the information send from an HTML
page.

FirstServleteFirst.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> Call another servlet</title>
</head>
<body>
<form action="/DynamicPractice/FirstServletfirst" method =
"post">
<label>
Enter:<input type="text" name="message">
</label>
<input type = "submit" name = "ok" value = "OK"/>
</form>
</body>
</html>

FirstServleteFirst.java
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class Parameter
*/
@WebServlet("/FirstServletfirst")
public class Fistservletfirst extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String paramName=request.getParameter("message");
out.println(paramName);

14
CE | 6th
150160107115
}
/**
* @see HttpServlet#doPost(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}

Output:

15
CE | 6th
150160107115

Practical 9
Aim: Create a login form and perform state management using cookies and
HttpSession.

16
CE | 6th
150160107115

Practical 10
Aim: Create a servlet that will make use of the request dispatcher.

callAnotherServlet.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> Call another servlet</title>
</head>
<body>
<form action="/DynamicPractice/FirstServlet" method = "post">
<input type = "submit" name = "ok" value = "OK"/>
</form>
</body>
</html>

FirstServlet.java

import java.io.IOException;
//import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class FirstServlet
*/
@WebServlet("/FirstServlet")
public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
System.out.println("FirstServlet");
//RequestDispatcher rd = request.getRequestDispatcher("SecondServlet");
//rd.forward(request, response);
response.sendRedirect("SecondServlet");
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
17
CE | 6th
150160107115
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

SecondServelet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class SecondServlet
*/
@WebServlet("/SecondServlet")
public class SecondServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
PrintWriter out = response.getWriter();
out.println("Hello welcome to Seconf Servlet");

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

18
CE | 6th
150160107115

Output:

19
CE | 6th
150160107115

20
CE | 6th
150160107115

Practical 11
Aim: Create Servlet that will display information of the HTTP Request Header.
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class Header
*/
@WebServlet("/Header")
public class Header extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
PrintWriter out = response.getWriter();
Enumeration<String> e = request.getHeaderNames();
String hname = "";
String hvalue = "";
out.println("<html>"
+ " <head>"
+ " <style>"
+ " table , th , td { border : 2px solid blue; border-collpse : collapse; }"
+ " </style>"
+ " </head>"
+ " <body>"
+ " <table> "
+ "<tr>"
+ " <th> Header name </th>"
+ " <th> Header value </th> </tr>");

while(e.hasMoreElements()) {
hname = (String)e.nextElement();
hvalue = request.getHeader(hname);
out.println( " <td> " +hname+ "</td>"
+ " <td> " +hvalue+ "<td>"
+ "</tr>");
}
out.println(" </table></body></html>");
21
CE | 6th
150160107115

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}
Output:

22
CE | 6th
150160107115

Practical 12
Aim: Implement authentication filter using filter API.
Practical 13
Aim: Create a web service which provides student information.
Practical 14
Aim: Create a login form and enter in the application with username and password if
password is correct then only allow access to the application otherwise direct hack to
the login screen. This with help of JSP.
Practical 15
Aim: Study and implement Hibernate.

23
CE | 6th

You might also like