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

Advanced Java Lab

Java...

Uploaded by

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

Advanced Java Lab

Java...

Uploaded by

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

MUTHURANGAM GOVERNMENT

ARTS COLLEGE(AUTONOMOUS)
VELLORE-2
PG & Research Department of Computer Science
M.Sc., COMPUTER SCIENCE

PRACTICAL RECORD
2023-2024

REG.NO : ……………………………. NAME :…………………………………………

SUB.CODE : …………………………………………………………………………………

SUBJECT : ………………………………………………………………………………….

CLASS :………………………………………………………………………………….
MUTHURANGAM GOVERNMENT
ARTS COLLEGE (AUTONOMOUS)
VELLORE-2

PG & Research Department of Computer Science


M.Sc., COMPUTER SCIENCE

BONAFIDE CERTIFICATE

Certified to be a bonafide record of work done by ....................................................

(Reg.No……………………………….) in the Laboratory of this College, submitted

for...……………. Semester Practical Examination in ………………………………… during the

Academic Year …………………......

Staff In-charge Head of the Department

Submitted for the Practical Examination held on ………………………

Internal Examiner External Examiner


INDEX

SI. PAGE
DATE TITLE SIGN
NO. NO.

1. Display the Welcome message using Servlet 1

2. Invoice of Purchase detail using Html and Servlet 5

3. Calculate the Percentage using JSP 10

4. Customer and Product details using Html and JSP 14

5. Employee pay slip using JSP 19

Insert and Delete the record in the table using


6. 25
JDBC

7. Display the Form data using Servlet 29

8. Table creation for all the headers using Servlet 34

9. Session Object Program using JSP 38

10. Client Server application using RMI 43

11. Calculator Application using Applet 48

12. Message passing system using Socket 54


Ex. No: 01
DISPLAY THE WELCOME MESSAGE USING SERVLET
Date:

1
1. DISPLAY THE WELCOME MESSAGE USING SERVLET

Welcome.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<CENTER>
<br><br>
<h1>Welcome Message using Servlet</h1>
<br>
<form action="MyServlet" method="post">
<label for="name"><b>Name : </b></label>
<input type="text" id="name" name="name"><br><br>
<input type="submit" value="Go"><br/>
</form>
</CENTER>
</body>
</html>

2
MyServlet.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;

@WebServlet(name = "MyServlet", urlPatterns = {"/MyServlet"})


public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public MyServlet() {
super();
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.print("<html><body><br><br><center><h1>My First Servlet</h1><br>");
out.println("<h1>Hello "+ name +"</h1>");
out.print("<h1>Welcome to HTTP Servlets</h1>");
out.print("<p>doGet() Method is executing...</p>");
out.print("</center></body></html>");
}
}

3
OUTPUT:

4
Ex. No: 02
INVOICE OF PURCHASE DETAIL USING HTML AND
Date: SERVLET

5
2. INVOICE OF PURCHASE DETAIL USING HTML AND SERVLET

PurchaseOrder.html
<!DOCTYPE html>
<html>
<head>
<title>Purchase Order</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Purchase Order Form</h1>
<form action="PurchaseOrder" method="post">
<label for="name">Name : </label>
<input type="text" id="name" name="name"><br><br>
<label for="item">Item : </label>
<input type="text" id="item" name="item"><br><br>
<label for="price">Price : </label>
<input type="number" id="price" name="price"><br><br>
<label for="quantity">Quantity : </label>
<input type="number" id="quantity" name="quantity"><br><br>
<label for="phone">Phone : </label>
<input type="number" id="phone" name="phone"><br><br>
<label for="email">E-Mail : </label>
<input type="email" id="email" name="email"><br><br>
<label for="location">Location : </label>
<input type="text" id="location" name="location"><br><br>
<input type="submit" value="Place Order">
</form>
</body>
</html>

6
PurchaseOrder.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;

@WebServlet(name = "PurchaseOrder", urlPatterns = {"/PurchaseOrder"})


public class PurchaseOrder extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String item = request.getParameter("item");
String price = request.getParameter("price");
int quantity = Integer.parseInt(request.getParameter("quantity"));
String phone = request.getParameter("phone");
String email = request.getParameter("email");
String location = request.getParameter("location");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Purchase Order</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Purchase Order Form</h1>");
out.println("<p>Name : "+ name +"</p>");
out.println("<p>Item : "+ item +"</p>");
out.println("<p>Price : "+ price +"</p>");

7
out.println("<p>Quantity : "+ quantity +"</p>");
out.println("<p>Phone : "+ phone +"</p>");
out.println("<p>Email : "+ email +"</p>");
out.println("<p>Loaction : "+ location +"</p>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
}

8
OUTPUT:

9
Ex. No: 03
CALCULATE THE PERCENTAGE USING JSP
Date:

10
3. CALCULATE THE PERCENTAGE USING JSP

Index.html
<!DOCTYPE html>
<html>
<head>
<title>Student Marks</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Enter The Marks</h1>
<form action="Result.jsp" method="get">
Enter Makrs in Advanced Java : <input type="text" name="java"> <br><br>
Enter NMA Marks : <input type="text" name="NMA"><br><br>
Enter MCAD Marks : <input type="text" name="MCAD"><br><br>
Enter PPUD Marks : <input type="text" name="PPUD"><br><br>
Enter Project Marks : <input type="text" name="pro"><br><br>
<input type="submit" value="Evaluate">
</form>
</body>
</html>

11
Result.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Result Page</title>
</head>
<body>
<%
int java=Integer.parseInt(request.getParameter("java"));
int NMA=Integer.parseInt(request.getParameter("NMA"));
int MCAD=Integer.parseInt(request.getParameter("MCAD"));
int PPUD=Integer.parseInt(request.getParameter("PPUD"));
int Project=Integer.parseInt(request.getParameter("pro"));
int c=java+NMA+MCAD+PPUD+Project;
double avg=c/5;
out.print("Your grade is ");
if(avg > 90 ){
out.print("A");
}else if (avg >= 80) {
out.print("B");
} else if (avg >= 70) {
out.print("C");
} else if (avg >= 60) {
out.print("D");
} else {
out.print("E");
}
%>
</body>
</html>

12
OUTPUT:

13
Ex. No: 04
CUSTOMER AND PRODUCT DETAILS USING
Date: HTML AND JSP

14
4. CUSTOMER AND PRODUCT DETAILS USING HTML AND JSP

PurchaseOrder.html
<!DOCTYPE html>
<html>
<head>
<title>Purchase order Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="PurchaseDetails.jsp" method="post">
<h2>Purchase Order Form</h2>
<table>
<tr>
<td>Product Name : </td>
<td><input type="text" name="productName" required></td>
</tr>
<tr>
<td>Product Quantity : </td>
<td><input type="number" name="productQuantity" required></td>
</tr>
<tr>
<td>Product Price : </td>
<td><input type="number" name="productPrice" required></td>
</tr>
<tr>
<td>Customer Name : </td>
<td><input type="text" name="customerName" required></td>
</tr>
<tr>
<td>Customer Contact Number : </td>

15
<td><input type="text" name="customerContactNumber" required></td>
</tr>
<tr>
<td>Customer Email : </td>
<td><input type="email" name="customerEmail" required></td>
</tr>
<tr>
<td>Shipping Address : </td>
<td><textarea rows="5" name="shippingAddress" required></textarea></td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>

PurchaseOrder.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Purchase details Page</title>
</head>
<body>
<h2>Purchase Order Details</h2>
<table>
<tr>
<td>Product Name:</td>

16
<td>${param.productName}</td>
</tr>
<tr>
<td>Product Quantity:</td>
<td>${param.productQuantity}</td>
</tr>
<tr>
<td>Product Price:</td>
<td>${param.productPrice}</td>
</tr>
<tr>
<td>Customer Name:</td>
<td>${param.customerName}</td>
</tr>
<tr>
<td>Customer Contact Number:</td>
<td>${param.customerContactNumber}</td>
</tr>
<tr>
<td>Customer Email:</td>
<td>${param.customerEmail}</td>
</tr>
<tr>
<td>Shipping Address:</td>
<td>${param.shippingAddress}</td>
</tr>
</table>
<br>
<a href="PurchaseOrder.html">Back</a>
</body>
</html>

17
OUTPUT:

18
Ex. No: 05
EMPLOYEE PAY SLIP USING JSP
Date:

19
5. EMPLOYEE PAY SLIP USING JSP

EmployeeSalarySatement.html
<!DOCTYPE html>
<html>
<head>
<title>Employee Salary Statement</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h3>Enter Employee Details:</h3>
<form action="EmployeePaySlip.jsp" method="POST">
<table>
<tr>
<td>Employee Name:</td>
<td><input type="text" name="ename" autofocus></td>
</tr>
<tr>
<td>Department:</td>
<td><input type="text" name="dept"></td>
</tr>
<tr>
<td>Designation:</td>
<td><input type="text" name="des"></td>
</tr>
<tr>
<td>Basic Salary:</td>
<td><input type="text" name="bsal"></td>
</tr>
<tr>
<td>TA(%):</td>

20
<td><input type="text" name="ta"></td>
</tr>
<tr>
<td>DA(%):</td>
<td><input type="text" name="da"></td>
</tr>
<tr>
<td>HRA(%):</td>
<td><input type="text" name="hra"></td>
</tr>
<tr>
<td>PF(%):</td>
<td><input type="text" name="pf"></td>
</tr>
<tr>
<td>LIC(%):</td>
<td><input type="text" name="lic"></td>
</tr>
<tr>
<td><input type="submit" value="Evaluate"></td>
<td><input type="reset" value="Reset"></td>
</tr>
</table>
</form>
</body>
</html>

21
EmployeePaySlip.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<%@ page import="java.sql.*" %>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Employee Pay Slip</title>
</head>
<body>
<%
String ename=request.getParameter("ename");
String dept=request.getParameter("dept");
String des=request.getParameter("des");
double bsal=Double.valueOf(request.getParameter("bsal"));
double ta=Double.valueOf(request.getParameter("ta"));
double ta2=bsal*ta/100;
double da=Double.valueOf(request.getParameter("da"));
double da2=bsal*da/100;
double hra=Double.valueOf(request.getParameter("hra"));
double hra2=bsal*hra/100;
double pf=Double.valueOf(request.getParameter("pf"));
double pf2=bsal*pf/100;
double lic=Double.valueOf(request.getParameter("lic"));
double lic2=bsal*lic/100;
double allowance, deduction, gsal, netsal;

allowance = (bsal*ta)/100 + (bsal*da)/100 + (bsal*hra)/100;


deduction = (bsal*pf)/100 + (bsal*lic)/100;
gsal = bsal + allowance;
netsal = gsal - deduction;
%>

22
<CENTER>
<table border=5 bgcolor="biege" height=600 width=400 >
<caption>
<h2><font color="blue" >XYZ Company Ltd.,<br>
SALARY STATEMENT</font></h2>
</caption>

<tr><td>Employee Name</td><td colspan=2><%=ename%></td></tr>


<tr><td>Department</td><td colspan=2><%=dept%></td></tr>
<tr><td>Designation</td><td colspan=2><%=des%></td></tr>
<tr><td>Basic Salary</td><td colspan=2><%=bsal%></td></tr>
<tr><th>Allowances</th><th>Percentage</th><th>Amount</th></tr>
<tr align=center><td>TA</td><td><%=ta%></td><td><%=ta2%></td></tr>
<tr align=center><td>DA</td><td><%=da%></td><td><%=da2%></td></tr>
<tr align=center><td>HRA</td><td><%=hra%></td><td><%=hra2%></td></tr>
<tr><th>Total Allowance:</th><td colspan=2><%=allowance%></td><tr>
<tr><th>Deductions</th><th>Percentage</th><th>Amount</th></tr>
<tr align=center><td>PF</td><td><%=pf%></td><td><%=pf2%></td></tr>
<tr align=center><td>LIC</td><td><%=lic%></td><td><%=lic2%></td></tr>
<tr><th>Total Deduction:</th><td colspan=2><%=deduction%></td><tr>
<tr><td>Gross Salary</td><td colspan=2><%=gsal%></td></tr>
<tr><td>Net Salary</td><td colspan=2><%=netsal%></td></tr>
</table>
</CENTER>
</body>
</html>

23
OUTPUT:

24
Ex. No: 06
INSERT AND DELETE THE RECORD IN THE TABLE
Date:
USING JDBC

25
6. INSERT AND DELETE THE RECORD IN THE TABLE USING JDBC

JDBCSample.java
import java.sql.*;
public class JDBC
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c = DriverManager.getConnection ("jdbc:odbc:SampleDB","system","123");
Statement s = c.createStatement();
s.executeUpdate("create table product(prodno number,prodname varchar(10),"+"qty
number,price number(7,2),amount number(7,2))");
s.executeUpdate("insert into product values(100,'pears',5,34.50,0)");
s.executeUpdate("insert into product values(101,'Cinthol',4,24.75,0)");
s.executeUpdate("insert into product values(102,'Rin',7,37.50,0)");
System.out.println("Before Updation:");
ResultSet r = s.executeQuery("select * from product");
while(r.next())

System.out.println(r.getString(1)+"\t"+r.getString(2)+"\t"+r.getString(3)+"\t"+r.getString(4)+"\t"+r.
getString(5));
s.executeUpdate("update product set amount=qty*price");
System.out.println("After Updation:");
r=s.executeQuery("select * from product");
while(r.next())

System.out.println(r.getString(1)+"\t"+r.getString(2)+"\t"+r.getString(3)+"\t"+r.getString(4)+"\t"+r.
getString(5));
c.close();

26
}
catch(Exception e)
{
System.out.println(e);
}
}
}

27
OUTPUT:

28
Ex. No: 07
DISPLAY THE FORM DATA USING SERVLET
Date:

29
7. DISPLAY THE FORM DATA USING SERVLET

Details.html
<!DOCTYPE html>
<html>
<head>
<title>Form</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h3>Fill in the Form</h3>
<form action="FormDataHandle" method="post">
<table>
<tr>
<td>Full Name :</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Phone Number :</td>
<td><input type="text" name="phone" /></td>
</tr>
<tr>
<td>Gender :</td>
<td><input type="radio" name="gender" value="Male" />Male
<input type="radio" name="gender" value="Female" />Female</td>
</tr>
<tr>
<td>Select Programming Languages to learn:</td>
<td><input type="checkbox" name="language" value="Java" />Java
<input type="checkbox" name="language" value="Python" />Python
<input type="checkbox" name="language" value="SQL" />SQL

30
<input type="checkbox" name="language" value="PHP" />PHP</td>
</tr>
<tr>
<td>Select Course duration:</td>
<td><select name="duration">
<option value="3 months">3 Months</option>
<option value="6 months">6 Months</option>
<option value="9 months">9 Months</option></select></td>
</tr>
<tr>
<td>Anything else you want to share:</td>
<td><textarea rows="5" cols="40" name="comment"></textarea></td>
</tr>
</table>
<input type="submit" value="Submit Details">
</form>
</body>
</html>

FormdataHandle.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;

@WebServlet(name = "FormDataHandle", urlPatterns = {"/FormDataHandle"})


public class FormDataHandle extends HttpServlet {
private static final long serialVersionUID = 1L;

31
public FormDataHandle() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String phNum = request.getParameter("phone");
String gender = request.getParameter("gender");
String progLang[] = request.getParameterValues("language");
String langSelect = " ";
if(progLang!=null){
for(int i=0;i<progLang.length;i++){
langSelect= langSelect + progLang[i]+ ", ";
}
}
String courseDur = request.getParameter("duration");
String comment = request.getParameter("comment");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.print("<html><body>");
out.print("<h3>Details Entered</h3><br/>");
out.print("Full Name : "+ name + "<br/>");
out.print("Phone Number : "+ phNum +"<br/>");
out.print("Gender : "+ gender +"<br/>");
out.print("Programming languages selected : "+ langSelect +"<br/>");
out.print("Duration of course : "+ courseDur +"<br/>");
out.print("Comments : "+ comment);
out.print("</body></html>");
}
}

32
OUTPUT:

33
Ex. No: 08
TABLE CREATION FOR ALL THE HEADERS
Date: USING SERVLET

34
8. TABLE CREATION FOR ALL THE HEADERS USING SERVLET

HeaderInformation.java
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;

@WebServlet(name = "HeaderInformation", urlPatterns = {"/HeaderInformation"})


public class HeaderInformation extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)


throws IOException, ServletException {
printHeader(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)


throws IOException, ServletException {
printHeader(request, response);
}

public void printHeader(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
String header = null;
String head = "<html><head><title>Header Information</title></head><body>";
String foot = "</body></html>";
response.setContentType("text/html;charset=UTF-8");

35
PrintWriter out = response.getWriter();
Enumeration enumeration = request.getHeaderNames();
out.println(head);
out.println("<h2> Header Information </h2>");
out.println("<B>Request Method : </B>" + request.getMethod() + "<BR>\n" +
"<B>Request URI : </B>" + request.getRequestURI() + "<BR>\n" +
"<B>Request Protocol : </B>" + request.getProtocol() + "<BR><BR>\n");
out.println("<table>");
while (enumeration.hasMoreElements()) {
header = (String) enumeration.nextElement();
if (header != null) {
out.println("<tr><td><b>" + header + "</td>");
out.println("<td >" + request.getHeader(header) + "</td></tr>");
}
}
out.println("</table><br>");
out.println(foot);
}
}

36
OUTPUT:

37
Ex. No: 09
JSP SESSION OBJECT PROGRAMMING
Date:

38
9. JSP SESSION OBJECT PROGRAMMING

Index.html
<!DOCTYPE html>
<html>
<head>
<title>Session Object</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<CENTER>
<br><br>
<h1>JSP Using Session Object</h1>
<br>
<form action="Welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="Go"><br/>
</form>
</CENTER>
</body>
</html>

Welcome.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome Page</title>
</head>
<body>
39
<CENTER>
<br><br>
<%
String name=request.getParameter("uname");
out.print("Welcome "+name);
session.setAttribute("user",name);
%>
<br><br>
<a href="Second.jsp">Display the value</a>
</CENTER>
</body>
</html>

Second.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Second Page</title>
</head>
<body>
<CENTER>
<br><br>
<h1>Display the session value on this page</h1>
<%
String name=(String)session.getAttribute("user");
out.print("Hello "+name);
%>
</CENTER>
</body>
</html>

40
OUTPUT:

41
42
Ex. No: 10
CLIENT SERVER APPLICATION USING RMI
Date:

43
10. CLIENT SERVER APPLICATION USING RMI

Factorial.java
import java.math.BigInteger;
import java.rmi.*;

// Creating an Interface
public interface Factorial extends Remote {
// Declaring the method
public BigInteger fact(int num) throws RemoteException;
}

FactorialImpl.java
import java.math.BigInteger;

// Extends and Implement the class


// and interface respectively
public class FactorialImpl extends java.rmi.server.UnicastRemoteObject implements Factorial {

// Constructor Declaration
public FactorialImpl() throws java.rmi.RemoteException
{
super();
}
// Calculation for the problem statement
// Implementing the method fact()
// to find factorial of a number
public BigInteger fact(int num) throws java.rmi.RemoteException
{
BigInteger factorial = BigInteger.ONE;
for (int i = 1; i <= num; ++i) {
44
factorial = factorial .multiply( BigInteger .valueOf(i));
}
return factorial;
}
}

FactorialClient.java
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

public class FactorialClient {


public static void main(String[] args)
{
try {
// Create a remote object with the same name
// Cast the lookup result to the interface
factorial c = (factorial);
Naming.lookup("rmi:// localhost/FactorialService");
// Call the method for the results
System.out.println(c.fact(30));
}
// If any error occurs
catch (MalformedURLException murle) {
System.out.println("\nMalformedURLException: " + murle);
}
catch (RemoteException re) {
System.out.println("\nRemoteException: " + re);
}
catch (NotBoundException nbe) {
System.out.println("\nNotBoundException: " + nbe);

45
}
catch (java.lang.ArithmeticException ae) {
System.out.println("\nArithmeticException: " + ae);
}
}
}

FactorialServer.java
import java.rmi.Naming;

public class FactorialServer {


// Implement the constructor of the class
public FactorialServer()
{
try {
// Create an object reference for the interface
Factorial c = new FactorialImpl();
// Bind the localhost with the service
Naming.rebind("rmi:// localhost/FactorialService", c);
}
catch (Exception e) {
// If any error occurs
System.out.println("ERR: " + e);
}
}
public static void main(String[] args)
{
// Create an object
new FactorialServer();
}
}

46
OUTPUT:

47
Ex. No: 11
CALCULATOR APPLICATION USING APPLET
Date:

48
11. CALCULATOR APPLICATION USING APPLET

Calculator.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Calculator extends Applet implements ActionListener{


String msg=" ";
int v1,v2,result;
TextField t1;
Button b[]=new Button[10];
Button add,sub,mul,div,clear,mod,EQ;
char OP;
public void init() {
Color k=new Color(120,89,90);
setBackground(k);
t1=new TextField(10);
GridLayout gl=new GridLayout(4,5);
setLayout(gl);
for(int i=0;i<10;i++)
{
b[i]=new Button(""+i);
}
add=new Button("add");
sub=new Button("sub");
mul=new Button("mul");
div=new Button("div");
mod=new Button("mod");
clear=new Button("clear");
EQ=new Button("EQ");

49
t1.addActionListener(this);
add(t1);
for(int i=0;i<10;i++)
{
add(b[i]);
}
add(add);
add(sub);
add(mul);
add(div);
add(mod);
add(clear);
add(EQ);
for(int i=0;i<10;i++)
{
b[i].addActionListener(this);
}
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
mod.addActionListener(this);
clear.addActionListener(this);
EQ.addActionListener(this);
}

public void actionPerformed(ActionEvent ae)


{
String str=ae.getActionCommand();
char ch=str.charAt(0);
if(Character.isDigit(ch))
t1.setText(t1.getText()+str);

50
else
if(str.equals("add"))
{
v1=Integer.parseInt(t1.getText());
OP='+';
t1.setText("");
}
else if(str.equals("sub"))
{
v1=Integer.parseInt(t1.getText());
OP='-';
t1.setText("");
}
else if(str.equals("mul"))
{
v1=Integer.parseInt(t1.getText());
OP='*';
t1.setText("");
}
else if(str.equals("div"))
{
v1=Integer.parseInt(t1.getText());
OP='/';
t1.setText("");
}
else if(str.equals("mod"))
{
v1=Integer.parseInt(t1.getText());
OP='%';
t1.setText("");
}
if(str.equals("EQ"))

51
{
v2=Integer.parseInt(t1.getText());
if(OP=='+')
result=v1+v2;
else if(OP=='-')
result=v1-v2;
else if(OP=='*')
result=v1*v2;
else if(OP=='/')
result=v1/v2;
else if(OP=='%')
result=v1%v2;
t1.setText(""+result);
}
if(str.equals("clear"))
{
t1.setText("");
}
}
}

52
OUTPUT:

53
Ex. No: 12
MESSAGE PASSING SYSTEM USING SOCKET
Date:

54
12. MESSAGE PASSING SYSTEM USING SOCKET

MyClient.java

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

class MyClient
{
public static void main(String args[])throws Exception
{
Socket s=new Socket("localhost",3333);
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="",str2="";
while(!str.equals("stop"))
{
str=br.readLine();
dout.writeUTF(str);
dout.flush();
str2=din.readUTF();
System.out.println("Server says: "+str2);
}
dout.close();
s.close();
}
}

55
MyServer.java

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

class MyServer
{
public static void main(String args[])throws Exception
{
ServerSocket ss=new ServerSocket(3333);
Socket s=ss.accept();
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="",str2="";
while(!str.equals("stop"))
{
str=din.readUTF();
System.out.println("client says: "+str);
str2=br.readLine();
dout.writeUTF(str2);
dout.flush();
}
din.close();
s.close();
ss.close();
}
}

56
OUTPUT:

57

You might also like