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

Mscjava

Adv.Java lab

Uploaded by

Dad's girl
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Mscjava

Adv.Java lab

Uploaded by

Dad's girl
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 61

C.S.

I COLLEGE OF ARTS AND SCIENCE FOR WOMEN


KARPAGA NAGAR, K.PUDUR, MADURAI-625007.

CERTIFICATE

RECORD OF PRACTICAL WORK IN: BRANCH:

THIS TO CERTIFY THAT THIS IS THE BONAFIDE RECORD OF

PRACTICAL WORK DONE BY………………………………………….

REG.NO. ……………..…………………………….IN THE YEAR………………….

STAFF IN-CHARGE

SUBMITTED FOR

PRACTICAL EXAMINATION

EXTERNAL EXAMINER
1.

2.
INDEX
S.NO DATE CONTENT PAGE SIGNATURE
NO
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

Ex.No:1 COMPLEX NUMBER USING CLASSES & OBJECTS


Date:

AIM:
To write a Java program to calculate the Complex number.
SOURCE CODE:
import java.io.*;
class calc
{
Float real,img;
calc()
{
}
calc(Float r,Float i)
{
real=r;
img=i;
}
void display()
{
System.out.println(real+"i"+img);
}
calc add(calc c2)
{
calc res=new calc();
res.real=real+c2.real;
res.img=img+c2.img;
return(res);

}
calc sub(calc c2)
{
calc res=new calc();
res.real=real-c2.real;
res.img=img-c2.img;
return(res);
}
}
class complex
{
public static void main(String args[])
{
calc c1=new calc(12.5f,2.5f);
calc c2=new calc(09.5f,0.5f);
System.out.println("c1 is :");
c1.display();
System.out.println("c2 is:");
c2.display();
calc c3=new calc();
System.out.println("Addition of c1 & c2 is:");
c3=c1.add(c2);
c3.display();
System.out.println("Subtraction of c1&c2 is:");
c3=c1.sub(c2);
c3.display();
}
}

OUTPUT:
RESULT:
Thus the program was executed successfully.

Ex.No:2 STRING MANIPULATION USING STRING OPERATION


Date:
AIM:
To write a Java program to String function.
SOURCE CODE:
import java.io.*;
class str
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int ch=1;
while(ch==1)
{
System.out.println("1.String trim");
System.out.println("2.String copy");
System.out.println("3.String concatenate");
System.out.println("4.String compare");
System.out.println("5.String Reverse");
System.out.println("Enter your choice");
int opt=Integer.parseInt(br.readLine());
String s1=new String();
String s2=new String();
StringBuffer s3;
switch(opt)
{
case 1:
System.out.println("1.Enter string to trim");

s1=br.readLine();
s2=s1.trim();
System.out.println(s2);
break;
case 2:
System.out.println("Enter string to copy");
s1=br.readLine();
s2=s1;
System.out.println(s2);
break;
case 3:
System.out.println("Enter string 1");
s1=br.readLine();
System.out.println("Enter string 2");
s2=br.readLine();
s2=s1+s2;
System.out.println(s2);
break;
case 4:
System.out.println("Enter string 1");
s1=br.readLine();
System.out.println("Enter string 2");
s2=br.readLine();
if(s1.compareTo(s2)==0)
{
System.out.println("Both string are equal");
}

else
{
System.out.println("Both are not equal");
}
break;
case 5:
System.out.println("Enter string to reverse");
s1=br.readLine();
s3=new StringBuffer(s1);
s3.reverse();
System.out.println(s3);
break;
default:
return;
}
System.out.println("Enter to countinue");
ch=Integer.parseInt(br.readLine());
}
}
}

OUTPUT:
RESULT:
Thus the program was executed successfully.
Ex.No:3 ARRAY LIST
Date:

AIM:
To write a Java program to Array list.
SOURCE CODE:
import java.util.*;
public class arraylist
{
public static void main(String args[])
{
List<Integer>al=new ArrayList<>();
al.add(10);
al.add(20);
al.add(30);
al.add(1);
al.add(2);
System.out.println("Original Array List :"+al);
al.remove(1);
al.remove(2);
System.out.println("Modified Array List :"+al);
}
}
OUTPUT:

RESULT:
Thus the program was executed successfully.
Ex.No: 4 REVERSE ARRAY LIST
Date:
AIM:
To write a Java program to Reverse the Array list.
SOURCE CODE:
import java.util.*;
public class arrayrev
{
public static void main(String args[])
{
List<String>mylist=new ArrayList<String>();
mylist.add("Practice");
mylist.add("Code");
mylist.add("Quiz");
mylist.add("Greeks for geeks");
System.out.println("Original list:"+mylist);
Collections.reverse(mylist);
System.out.println("Modified list: "+mylist);
}
}
OUTPUT:

RESULT:
Thus the program was executed successfully.
Ex.No: 5 HASH TABLE USING ITERATOR
Date:
AIM:
To write a Java program to Hash table using Iterator.
SOURCE CODE:
import java.io.*;
import java.util.*;
public class iterator
{
public static void main(String args[])
{
Hashtable<String,String>hash_t=new Hashtable<String,String>();
hash_t.put("1","Geeks");
hash_t.put("2","For");
hash_t.put("3","Geeks");
Set hash_set=hash_t.keySet();
System.out.println("Set created from Hashtable key contain:");
Iterator itr=hash_set.iterator();
while(itr.hasNext())
System.out.println(itr.next());
}
}

OUTPUT:
RESULT:
Thus the program was executed successfully.
Ex.No:6 LINKED LIST
Date:

AIM:
To write a Java program to Linked list.

SOURCE CODE:
import java.io.*;
import java.util.LinkedList;
class GFG
{
public static void main(String args[])
{
LinkedList<String>gfg=new LinkedList<String>();
gfg.add("Rani");
gfg.add("Sheela");
gfg.add("Kavi");
System.out.println("Linked List element :");
for(int i=0;i<gfg.size();i++)
{
System.out.println("Element at index "+i+" is "+gfg.get(i));
}
}
}
OUTPUT:

RESULT:
Thus the program was executed successfully.
Ex.No: 7 REVERSE LIST
Date:

AIM:
To write a Java program to name contains any numeric value throw Exception invalid name.
SOURCE CODE:
import java.util.*;
public class reverse
{
public static void main(String args[])
{
List<String> mylist=new ArrayList<String>();
mylist.add("MS.Dhoni");
mylist.add("Matheesh Paththirana");
mylist.add("V.Kholi");
mylist.add("Jaadu");
System.out.println("Original list: "+mylist);
Collections.reverse(mylist);
System.out.println("Modified list: "+mylist);
}
class NameNotValidExceptionextendsreverse
{
public String validname()
{
return("Invalidname please reenter the name");
}
}
}
OUTPUT:

RESULT:
Thus the program was executed successfully.
Ex.No:8 EXCEPTION HANDLING
Date:
AIM:
To write a Java program to build in exception.
SOURCE CODE:
import java.io.*;
class Exception
{
public static void main(String args[])throws IOException
{
try
{
int a=30,b=0;
int c=a/b;
System.out.println("Result: "+c);
}
catch(ArithmeticException e)
{
System.out.println("Division by zero error");
}
}
}

OUTPUT:
RESULT:
Thus the program was executed successfully.
Ex.No:9 CALENDAR
Date:

AIM:
To write a Java program to print a date and time.
SOURCE CODE:
import java.util.*;
public class calendar
{
public static void main(String args[])
{
Calendar calendar=Calendar.getInstance();
System.out.println("The current date is:"+calendar.getTime());
calendar.add(Calendar.DATE,15);
System.out.println("15 days to go:"+calendar.getTime());
calendar.add(Calendar.MONTH,4);
System.out.println("4 month: "+calendar.getTime());
calendar.add(Calendar.YEAR,2);
System.out.println("2 year : "+calendar.getTime());
}
}
OUTPUT:

RESULT:
Thus the program was executed successfully.
Ex.No:10 SINGLE INHERITANCE
Date:

AIM:
To write a Java program to calculate the area and volume using Single Inheritance.

SOURCE CODE:
import java.io.*;
import java.util.*;
class Room
{
int length,breadth;
Room(int x,int y)
{
length=x;
breadth=y;
}
int area()
{
return(length*breadth);
}
}
class BedRoom extends Room
{
int height;
BedRoom(int x,int y, int z)
{
super(x,y);
height=z;
}
int volume()
{
return(length*breadth*height);
}
}
class singleInheritance
{
public static void main(String args[])throws IOException
{
int l,b,h;
Scanner s=new Scanner(System.in);
System.out.println("Enter the length,Breadth,Height");
l=s.nextInt();
b=s.nextInt();
h=s.nextInt();
BedRoom r= new BedRoom(l,b,h);
int area=r.area();
int volume=r.volume();
System.out.println("Area of the Room "+area);
System.out.println("Volume of the Room "+volume);
}
}
OUTPUT:

RESULT:
Thus the program was executed successfully.
Ex.No:11 MULTIPLE INHERITANCE
Date:

AIM:
To write a Java program to Multiple Inheritance.

SOURCE CODE:
import java.util.*;
interface area
{
final static float pi=3.14f;
float compute(float x,float y);
}
class rectangle implements area
{
public float compute(float x, float y)
{
return(x*y);
}
}
class circle implements area
{
public float compute(float x, float y)
{
return(pi * x *x);
}
}
class shape
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the length & breath of a Rectangle");
int l=sc.nextInt();
int b=sc.nextInt();
System.out.println("Enter the Radius of a circle");
int r=sc.nextInt();
rectangle rect=new rectangle();
circle cir=new circle();
area ar;
ar=rect;
System.out.println("Area of rectangle is :"+ar.compute(l,b));
ar=cir;
System.out.println("Area of circle is:" + ar.compute(r,0));
}
}
OUTPUT:

RESULT:
Thus the program was executed successfully.
Ex.No:12 PACKAGE
Date:

AIM:
To write a Java program to Arithmetic calculation using Package.

SOURCE CODE:
PACKAGE:
package arithmetic;
public class Mymath
{
public int add(int x,int y)
{
return x+y;
}
public int sub(int x,int y)
{
return x-y;
}
public int mul(int x,int y)
{
return x*y;
}
public int div(int x,int y)
{
return x/y;
}
public int mod(int x,int y)
{
return x%y;
}
}

MAIN:
Import arithmetic.Mymath;
public class arithop
{
public static void main(String args[])
{
Mymath m=new Mymath();
System.out.println(m.add(8,5));
System.out.println(m.sub(8,5));
System.out.println(m.mul(8,5));
System.out.println(m.div(8,5));
System.out.println(m.mod(8,5));
}
}
OUTPUT:

RESULT:
Thus the program was executed successfully.
Ex.No:13 BANKING OPERATIONS USING RMI
Date

AIM :
To write a java program in banking operations using RMI.

SOURCE CODE :

banking.java
import java.rmi.*;
import java.rmi.server.*;
public interface banking extends Remote
{
public int withdraw(int a,int amt)throws RemoteException;
public int deposit(int b,int amt)throws RemoteException;
public int balance(int amt)throws RemoteException;
}

bankimpl.java

import java.rmi.*;
import java.rmi.server.*;
public class bankimpl extends UnicastRemoteObject implements banking
{
public bankimpl()throws RemoteException
{
}
public int withdraw(int a,int amt)throws RemoteException
{
amt=amt-a;
return(amt);
}
public int deposit(int b,int amt)throws RemoteException
{

amt=amt+b;
return(amt);
}
public int balance(int amt)throws RemoteException
{
return(amt);
}
}
bankserver.java
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class bankserver
{
public static void main(String args[])throws RemoteException
{
try
{
Registry reg=LocateRegistry.createRegistry(9999);
bankimpl b=new bankimpl();
reg.rebind("bankserver",b);
System.out.println("Server is Ready");
}
catch(Exception e)
{
System.out.println("Exception"+e);
}
}
}

bankclient.java

import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class bankclient
{
public static void main(String args[])throws RemoteException
{
int ch;
try
{
Registry reg=LocateRegistry.getRegistry("127.0.0.1",9999);
banking bf=(banking)reg.lookup("bankserver");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Customer name:");
String n=br.readLine();
System.out.println("Enter the Account No:");
int an=Integer.parseInt(br.readLine());
System.out.println("Enter the Initial Amount:");
int amt=Integer.parseInt(br.readLine());
do
{
System.out.println("\n \t 1.Withdraw \n\t 2.Deposit \n\t 3.Balance \n\t 4.Exit");

System.out.println("Enter your choice");


ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("Enter the amount of withdraw");
int wd=Integer.parseInt(br.readLine());
System.out.println("User Name :"+n);
System.out.println("Account No:"+an);
if(wd>amt)
{
System.out.println("Balance is less for withdraw");
}
else
{
amt=bf.withdraw(wd,amt);
System.out.println("Balance after withdraw:"+amt);
}
break;
case 2:
System.out.println("Enter the amount of deposit");
int dp=Integer.parseInt(br.readLine());
System.out.println("User Name :"+n);
System.out.println("Account No:"+an);
amt=bf.deposit(dp,amt);
System.out.println("Balance after deposit :"+amt);
break;
case 3:
System.out.println("User Name :"+n);
System.out.println("Account No:"+an);
amt=bf.balance(amt);
System.out.println("Available balance
"+amt); break;
case 4:
System.out.println("Transaction completed!!!");
}
}
while(ch<4);
}
catch(Exception e)
{
System.out.println("Exception"+e);
}
}
}
OUTPUT:

RESULT :
Thus the above program was successfully executed.

Ex.No:14 STUDENT MARK CALCULATION USING RMI


Date

AIM :
To write a java program in student mark calculation using RMI.

SOURCE CODE :

studinter.java

import java.rmi.*;
import java.rmi.server.*;
public interface studinter extends Remote
{
public int mark(int a,int b,int c,int d,int e)throws RemoteException;
public int avg(int tot)throws RemoteException;
}

studimpl.java

import java.rmi.*;
import java.rmi.server.*;
public class studimpl extends UnicastRemoteObject implements studinter
{
public studimpl()throws RemoteException
{
}
public int mark(int a,int b,int c,int d,int e)throws RemoteException
{
int tol=a+b+c+d+e;
return(tol);
}
public int avg(int tot)throws RemoteException
{
int avg=tot/5;
return(avg);
}
}

studserver.java
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class studserver
{
public static void main(String args[])throws RemoteException
{
try
{
Registry reg=LocateRegistry.createRegistry(9999);
studimpl b=new studimpl();
reg.rebind("studserver",b);
System.out.println("Server is Ready");
}
catch(Exception e)
{
System.out.println("Exception"+e);
}
}
}

studclient.java

import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class studclient
{
public static void main(String args[])throws RemoteException
{
try
{
Registry reg=LocateRegistry.getRegistry("127.0.0.1",9999);
studinter st=(studinter)reg.lookup("studserver");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Student name:");
String name=br.readLine();
System.out.println("Enter the Mark Tamil:");
int m1=Integer.parseInt(br.readLine());
System.out.println("Enter the Mark English:");
int m2=Integer.parseInt(br.readLine());
System.out.println("Enter the Mark Maths:");
int m3=Integer.parseInt(br.readLine());
System.out.println("Enter the Mark Science:");
int m4=Integer.parseInt(br.readLine());
System.out.println("Enter the Mark Social:");
int m5=Integer.parseInt(br.readLine());
int calc=st.mark(m1,m2,m3,m4,m5);
System.out.println("Total:"+calc);
int avg=st.avg(calc);
System.out.println("Average:"+avg);
if(avg>=90)
{
System.out.println("Distinction");
}
else if((avg>=80) && (avg<90))
{
System.out.println("First class");
}
else if((avg>=60) && (avg<80))
{
System.out.println("Second Class");
}
else if((avg>=35) && (avg<60))
{
System.out.println("Third Class");
}
}
catch(Exception e)
{
System.out.println("Exception"+e);
}
}
}
OUTPUT:

RESULT :
Thus the above program was successfully executed.
Ex.No: 15 CHANGING BACKGROUND COLOR USING BUTTON
Date:

AIM :
To write a java program in Changing background color using button.

SOURCE CODE :

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*<applet code="color.class" height=500 width=500>
</applet>*/
public class color extends Applet implements ActionListener
{
Button b1;
Button b2;
Button b3;
public void init()
{
b1=new Button("Red");
b2=new Button("Blue");
b3=new Button("Green");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
add(b1);
add(b2);
add(b3);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
setBackground(Color.red);
}
else if(e.getSource()==b2)
{
setBackground(Color.blue);
}
else
{
setBackground(Color.green);
}
}
}
OUTPUT:

RESULT :
Thus the above program was successfully executed.

Ex.No: 16 DIGITAL CLOCK


Date :

AIM :
To write a java program in digital clock.

SOURCE CODE :

import java.applet.*;
import java.awt.*;
import java.util.*;
import java.text.*;
/*<applet code="digitalclock.class"height=500 width=500>
</applet>*/
public class digitalclock extends Applet implements Runnable
{
Thread t=null;
int hours=0,minutes=0,seconds=0;
String timeString="";
public void init()
{
setBackground(Color.blue);
}
public void start()
{
t=new Thread(this);
t.start();
}
public void run()
{
try
{
while(true)
{
Calendar cal=Calendar.getInstance();
hours=cal.get(Calendar.HOUR_OF_DAY);
if(hours>12)hours-=12;
minutes=cal.get(Calendar.MINUTE );
seconds=cal.get(Calendar.SECOND );
SimpleDateFormat formatter=new SimpleDateFormat("hh:mm:ss");
Date date=cal.getTime();
timeString=formatter.format(date);
repaint();
t.sleep(1000);
}
}

catch(Exception e)
{
}
}
public void paint(Graphics g)
{
g.setColor(Color.black);
g.drawString(timeString,58,58);
}
}

OUTPUT:

RESULT :
Thus the above program was successfully executed.

Ex.No: 17 MENU EDITOR


Date:
AIM :
To write a java program in menu editor.

SOURCE CODE :

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<applet code="menu.class"height=500 width=500>
</applet>*/
class win extends Frame implements ActionListener
{
MenuBar MyMenu;
MenuItem f1,f2,f3,e1,e2,e3;
Menu file,edit,exit;
win(String title)
{
super(title);
setLayout(new GridLayout(1,1));
MyMenu=new MenuBar();
file=new Menu("File");
edit=new Menu("Edit");
exit=new Menu("Exit");
f1=new MenuItem("Open");
f2=new MenuItem("Close");
f3=new MenuItem("Exit");
file.add(f1);
file.add(f2);
file.add(f3);
f1.addActionListener(this);
f2.addActionListener(this);
f3.addActionListener(this);
e1=new MenuItem("Copy");
e2=new MenuItem("Cut");
e3=new MenuItem("Paste");
e1.addActionListener(this);
e2.addActionListener(this);
e3.addActionListener(this);
edit.add(e1);
edit.add(e2);
edit.add(e3);
MyMenu.add(file);
MyMenu.add(edit);
MyMenu.add(exit);
setMenuBar(MyMenu);
}
public void actionPerformed(ActionEvent ae)
{
}
}
public class menu extends Applet implements ActionListener
{
win w;
Button B1,B2;
public void init()
{
B1=new Button("show window");
B2=new Button("Hide window");
w=new win("My Own
Window"); add(B1);
add(B2);
w.setSize(200,200);
B1.addActionListener(this);
B2.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==B1)
w.setVisible(true);
else
if(ae.getSource()==B2)
w.setVisible(false);
}
}
OUTPUT:

RESULT :
Thus the above program was successfully executed.
Ex.No: 18 CHAT SERVER
Date:

AIM :
To write a java program in chat server.

SOURCE CODE :

chatclient.java
import java.io.*;
import java.net.*;
public class chatclient {
public static void main(String[] args)throws Exception
{
Socket sock=new Socket("127.0.0.1",3000);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
OutputStream os=sock.getOutputStream();
PrintWriter pw=new
PrintWriter(os,true); InputStream
is=sock.getInputStream();
BufferedReader r=new BufferedReader(new
InputStreamReader(is)); System.out.println("Start the chitchat,type
& press Enter key"); String receiveMessage, sendMessage;
while(true)
{
sendMessage=br.readLine();
pw.println(sendMessage);
pw.flush();
if((receiveMessage=r.readLine()) !=null)
{
System.out.println("Server :"+receiveMessage);
}
}
}
}

chatserver.java

import java.io.*;
import java.net.*;
public class chatserver
{
public static void main(String[] args)throws Exception
{
ServerSocket sersock=new ServerSocket(3000);
System.out.println("Server ready for chatting");
Socket sock=sersock.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
OutputStream os=sock.getOutputStream();
PrintWriter pw=new
PrintWriter(os,true); InputStream
is=sock.getInputStream();
BufferedReader r=new BufferedReader(new
InputStreamReader(is)); String receiveMessage, sendMessage;
while(true)
{
if((receiveMessage=r.readLine()) !=null)
{
System.out.println("Client :"+receiveMessage);
}
sendMessage=br.readLine();
pw.println(sendMessage);
pw.flush();
}
}
}

OUTPUT:

RESULT :
Thus the above program was successfully executed.
Ex.No: 19 COOKIES
Date:

AIM :
To write a java program in cookies.

SOURCE CODE :

index.html
<html>

<head></head>

<body>

<form action="login">

User Name:<input type="text" name="userName"/><br/>


Password:<input type="password" name="userPassword"/><br/>
<input type="submit" value="submit"/>

</form>
</body>
</html>

MyServlet1.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet1 extends HttpServlet
{
public void doGet(HttpServletRequest request,
HttpServletResponse response) {
try{
response.setContentType("text/html");
PrintWriter pwriter =
response.getWriter();
String name = request.getParameter("userName");
String password = request.getParameter("userPassword");
pwriter.print("<br>Hello "+name);
pwriter.print("<br>Your Password is: "+password);
//Creating two cookies
Cookie c1=new Cookie("userName",name);
Cookie c2=new Cookie("userPassword",password);
//Adding the cookies to response header
response.addCookie(c1);
response.addCookie(c2);
pwriter.print("<br><a href='welcome'>View
Details</a>"); pwriter.close();
}

catch(Exception exp){
System.out.println(exp);
}
}
}

MyServlet2.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet2 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response){
try{
response.setContentType("text/html");
PrintWriter pwriter =
response.getWriter();
//Reading cookies
Cookie c[]=request.getCookies();
//Displaying User name value from cookie
pwriter.print("<br>Welcome "+c[1].getValue());
//Displaying user password value from cookie
pwriter.print("<br>Your Password is
"+c[2].getValue()); pwriter.close();
}
catch(Exception exp){
System.out.println(exp);
}
}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>


<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-
app_3_1.xsd">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-class>MyServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Servlet2</servlet-name>
<servlet-class>MyServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet2</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping> <session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>

OUTPUT:

RESULT :
Thus the above program was successfully executed.
Ex.No: 20 SESSION TRACKING
Date:

AIM :
To write a java program in session tracking.

SOURCE CODE :

index.html
<html>
<head>
</head>
<body>
<form method="get">
<center><h1>Click On below link to go to our Web-Site</h1><br>
<a href="UserVisitServlet">Visit</a>
</form>
</body>
</html>

UserVisitServlet.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class UserVisitServlet extends HttpServlet
{ int counter = 0;
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out =
res.getWriter(); counter++;
out.println("<html>");
out.println("<body>");
out.println("<center><h1>" + "This Web-Site has been visited " + counter + " times." + "</h1>");
out.println("</body></html>");
}
}
web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-
app_3_1.xsd">
<servlet>

<servlet-name>UserVisitServlet</servlet-name>

<servlet-class>UserVisitServlet</servlet-class>
</servlet>

<servlet-mapping>

<servlet-name>UserVisitServlet</servlet-name>

<url-pattern>/UserVisitServlet</url-pattern>

</servlet-mapping>

<session-config>

<session-timeout>
30
</session-timeout>

</session-config>

</web-app>
OUTPUT:

RESULT :
Thus the above program was successfully executed.

Ex:No 21 LOGIN FORM


Date:

AIM :
To write a java program in login form.

SOURCE CODE :

index.html

<html>

<head>

</head>

<body>

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

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

<input type="password" name="pwd"> <br>

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

</form>

</body>

</html>

userlogin.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class userlogin extends HttpServlet

{
public void doGet(HttpServletRequest req, HttpServletResponse res)throws
ServletException,IOException
{

res.setContentType("text/html");
PrintWriter out=res.getWriter();
String name,pwd;

name=req.getParameter("uname");
pwd=req.getParameter("pwd");
if(name.equals("admin")&&pwd.equals("admin"))
out.println("<html><body>sucessfully registered</body></html>");
else
out.println("<html><body>check your username and password</body></html>");

}
OUTPUT:

RESULT :
Thus the above program was successfully executed.
Ex.No : 22 EMPLOYEE PAY BILL

Date :

AIM :
To write a java program in employee pay bill.

SOURCE CODE :

index.html

<html><head></head>

<body>

<h4><center>EMPLOYEE DETAILS</center></h4>

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

ENTER NAME <input type="text"


name="t1"><br> ENTER BASIC PAY <input
type="text" name="t2"><br> ENTER DA <input
type="text" name="t3"><br>
<input type="submit" value="calculate">

</form>

</body>

</html>

emp.java

import
java.io.*;
import
javax.servlet.
*;
import javax.servlet.http.*;

public class emp extends HttpServlet

{
public void doGet(HttpServletRequest req, HttpServletResponse
res)throws ServletException,IOException{

59
res.setContentType("tex
t/html"); PrintWriter
out=res.getWriter();
String name;
double da1,hra,pf,np;
name=req.getParame
ter("t1");
int
bp=Integer.parseInt(req.getParameter("
t2")); int
da=Integer.parseInt(req.getParameter("
t3"));

da1=10/100*bp;
hra=5/100
*bp;
np=bp+da
1+hra-pf;
out.println("<html><head></
head>");
out.println("<body>");
out.println("welcome
"+name);
out.println("<br><br>");
out.println("Basic Pay :
"+bp);
out.println("<br><br>");
out.println("NetPay : "+np);
out.println("</body></html>"
);
}

OUTPUT:

60
RESULT :
Thus the above program was successfully executed.

61

You might also like