Sei sulla pagina 1di 14

IV SEM JAVA LAB MANUAL (MCA, VTU)

1a) prog1a.java o1.methodoverload(23134.5343f);


class overload { }
public overload() { }
System.out.println("this is a zero Output:
parameter constructor"); this is a zero parameter constructor
} this is a one parameter constructor
public overload(int a) { with integer a=12
System.out.println("this is a one this is a one parameter constructor
parameter constructor with integer with Floating b=1.2546546E7
a="+a); this is a one parameter constructor
} with Floating b=1256.2343
public overload(float b) { this is a zero parameter method
System.out.println("this is a one this is a one parameter method with
parameter constructor with Floating integer a=231
b="+b); this is a one parameter method with
} floating b=1.2546546E7
public void methodoverload() { this is a one parameter method with
System.out.println("this is a zero floating b=23134.535
parameter method");
} 1b) prog1b.java
public void methodoverload(int a) { class outer
System.out.println("this is a one {
parameter method with integer int a=12;
a="+a); private int b=21;
} protected int c=442;
public void methodoverload( float b) public int d=1111;
{ class inner
System.out.println("this is a one {
parameter method with floating int p=150;
b="+b); private int q=250;
} protected int r=380;
} public int s=450;
public class prog1a void inmeth()
{ {
public static void main(String arg[]) System.out.println("outer class default
{ member in inner class a="+a);
overload o1=new overload(); System.out.println("outer class private
overload o2=new overload(12); member in inner class b="+b);
overload o3=new System.out.println("outer class
overload(12546546l); protected member in inner class
overload o4=new c="+c);
overload(1256.23421f); System.out.println("outer class public
o1.methodoverload(); member in inner class d="+d);
o1.methodoverload(231); }
o1.methodoverload(12546546l); } // end of inner class
IV SEM JAVA LAB MANUAL (MCA, VTU)

void outmeth() 2a) prog2a.java


{ class A
inner i=new inner(); {
i.inmeth(); A()
System.out.println(); {
System.out.println("inner class default System.out.println("This is a
member in outer class p="+i.p); constructor inside class A");
System.out.println("inner class private }
member in outer class q="+i.q); int var=10;
System.out.println("inner class void meth()
protected member in outer class {
r="+i.r); System.out.println("this is class A
System.out.println("inner class public variable: "+var);
member in outer class s="+i.s); }
} } // end of class A
} class B extends A
public class prog1b {
{ int var=20;
public static void main(String arg[]) B(int a)
{ {
outer o=new outer(); var=a;
o.outmeth(); System.out.println("This is a
} constructor inside class B");
} }
Output: void meth()
outer class default member in inner {
class a=12 System.out.println("this is class B
outer class private member in inner variable:"+var);
class b=21 System.out.println("This is class A
outer class protected member in inner variable: Inside B class:"+super.var);
class c=442 super.meth();
outer class public member in inner }
class d=1111 } //end of class B
inner class default member in outer class C extends B
class p=150 {
inner class private member in outer C(float f)
class q=250 {
inner class protected member in outer super(150);
class r=380
inner class public member in outer System.out.println("Inside C
class s=450 constructor:"+f);
}
void meth()
{
super.meth();
IV SEM JAVA LAB MANUAL (MCA, VTU)

} throw new newexception();


} }
class prog2a }
{ }
public static void main(String class prog2b
arg[]) {
{ public static void main(String arg[])
C c=new C(125.45f); {
c.meth(); try
} {
} throwexception t=new
Output: throwexception();
This is a constructor inside class A t.meth();
This is a constructor inside class B }
Inside C constructor:125.45 catch(newexception ne)
this is class B variable:150 {
This is class A variable: Inside B System.out.println("ERROR:"+ne);
class:10 }
this is class A variable: 10 finally
{
2b) prog2b.java System.out.println("THIS IS EXECUTED
class newexception extends Exception FINALLY");
{ }
public String toString() } // end of main()
{ }
return("this is my Output:
exception"); ERROR: DIVIDED BY ZERO ERROR
} ERROR:this is my exception
} THIS IS EXECUTED FINALLY
class throwexception
{ 3a) prog3a.java
int a,b=21; interface myinterface
void meth() throws {
newexception int a=125;
{ float b=399.5f;
try double meth();
{ }
interface newinterface extends
System.out.println(b/a); myinterface
} {
void meth2();
catch(ArithmeticException ae) }
{ class interfaceclass implements
System.out.println("ERROR: DIVIDED newinterface
BY ZERO ERROR"); {
IV SEM JAVA LAB MANUAL (MCA, VTU)

public double meth() System.out.println("THIS THREAD


{ PRIORITY IS : "+getPriority());
return(a*b); start();
} }
public void meth2() public void run()
{ {
System.out.println("THE ADDITION OF try
THE GIVEN NUMBER : "+(a+b)); {
} for(int i=1;i<=5;i++)
} {
class prog3a System.out.println("CHILD:
{ "+getName()+" " +i);
public static void main(String sleep(500);
arg[]) }
{ }
interfaceclass i=new catch(InterruptedException ie)
interfaceclass(); {
i.meth2();

System.out.println("PRODUCT System.out.println("EXCEPTION : "+ie);


OF THE GIVEN NUMBER : "+i.meth()); }
} }
} }
Output: class prog3b
THE ADDITION OF THE GIVEN NUMBER {
: 524.5 public static void main(String
PRODUCT OF THE GIVEN NUMBER : arg[])
49937.5 {
childthread t1=new
3b) prog3b.java childthread("Thread-1");
class childthread extends Thread childthread t2=new
{ childthread("Thread-2");
childthread(String a) childthread t3=new
{ childthread("Thread-3");
super(a); Thread
System.out.println("THREAD t=Thread.currentThread();
NAME IS : "+getName()); System.out.println("MAIN
if(getName().equals("Thread-1")) THREAD: "+t);
setPriority(6); try
if(getName().equals("Thread-2")) {
setPriority(8); for(int i=1;i<=5;i+
if(getName().equals("Thread-3")) +)
setPriority(7); {

System.out.println(t.getName()
+" " +i);
IV SEM JAVA LAB MANUAL (MCA, VTU)

public class BanApplet extends Applet


t.sleep(1000); implements Runnable
} {
} Thread t=null;
String m;
catch(InterruptedException e) boolean f=false;
{ public void start()
{
System.out.println("Exception: t=new Thread(this);
"+e); m=" BIKOO CHANGMAI
} ";
} t.start();
} }
Output: public void run()
THREAD NAME IS : Thread-1 {
THIS THREAD PRIORITY IS : 6 for(;;)
THREAD NAME IS : Thread-2 {
CHILD: Thread-1 1 try
THIS THREAD PRIORITY IS : 8 {
THREAD NAME IS : Thread-3 repaint();
CHILD: Thread-2 1 Thread.sleep(100);
THIS THREAD PRIORITY IS : 7 char ch;
CHILD: Thread-3 1 ch=m.charAt(0);
MAIN THREAD: Thread[main,5,main]
main 1 m=m.substring(1,m.length());
CHILD: Thread-1 2 m+=ch;
CHILD: Thread-2 2 if(f)
CHILD: Thread-3 2 break;
CHILD: Thread-1 3 }
CHILD: Thread-2 3 catch(InterruptedException
main 2 ie)
CHILD: Thread-3 3 {
CHILD: Thread-1 4 System.out.println("Error: "+ie);
CHILD: Thread-2 4 }
CHILD: Thread-3 4 }
CHILD: Thread-1 5 }
CHILD: Thread-2 5 public void stop()
main 3 {
CHILD: Thread-3 5 f=true;
main 4 }
main 5 public void paint(Graphics g)
{
4a) BanApplet.java g.drawString(m,150,200);
import java.awt.*; }
import java.applet.*; } //end of class
IV SEM JAVA LAB MANUAL (MCA, VTU)

4b) parameterApplet.java try


import java.awt.*; {
import java.applet.*; Class.forName("sun.jdbc.odbc.JdbcOdb
public class parameterApplet extends cDriver");
Applet con=DriverManager.getConnection(url
{ );
String name,num; stat=con.createStatement();
public void start() }
{ catch(ClassNotFoundException c)
name=getParameter("name"); {
num=getParameter("num"); System.out.println("class not found");
} System.exit(0);
public void paint(Graphics g) }
{ catch(SQLException e)
try {
{ System.out.println(e);
g.drawString(name,150,100); System.exit(0);
g.drawString(num,150,120); }
} //menu frame
catch(NumberFormatException ne) menu=new JFrame("Menu");
{ JButton add=new JButton("ADD");
System.out.println(ne); JButton query=new JButton("Query");
} JButton exit=new JButton("Exit");
} //insert frame
} insert=new JFrame("Insert");
JLabel u=new JLabel("USN");
5) db.java JLabel n=new JLabel("Name");
import java.sql.*; JLabel s=new JLabel("Sem");
import java.awt.*; JLabel sub1=new JLabel("SUB1");
import javax.swing.*; JLabel sub2=new JLabel("SUB2");
import java.awt.event.*; JLabel sub3=new JLabel("SUB3");
public class db usn=new JTextField(15);
{ name=new JTextField(15);
Connection con; sem=new JTextField(15);
Statement stat; s1=new JTextField(15);
ResultSet result; s2=new JTextField(15);
JTextField usn,name, s3=new JTextField(15);
sem,s1,s2,s3,qsem,p1,p2; JButton iok=new JButton("OK");
JFrame menu,insert,fquery; //query frame
JTable tab; fquery=new JFrame("Query");
JScrollPane sp; JLabel semister=new
JFrame table; JLabel("Semister");
public db() JLabel per=new JLabel("Percentage");
{ qsem=new JTextField(15);
String url="jdbc:odbc:mca"; p1=new JTextField(15);
p2=new JTextField(15);
IV SEM JAVA LAB MANUAL (MCA, VTU)

JButton qok=new JButton("OK"); {


//adding objects to menu frame public void
menu.setSize(300,80); actionPerformed(ActionEvent e)
menu.setLayout(new FlowLayout()); {
menu.setDefaultCloseOperation(JFram fquery.setVisible(true);
e.EXIT_ON_CLOSE); }
menu.add(add); });
menu.add(query); exit.addActionListener(new
menu.add(exit); ActionListener()
//adding objects to insert frame {
insert.setSize(230,220); public void
insert.setLayout(new FlowLayout()); actionPerformed(ActionEvent e)
insert.add(u); {
insert.add(usn); try
insert.add(n); {
insert.add(name); con.close();
insert.add(s); stat.close();
insert.add(sem); }
insert.add(sub1); catch(SQLException sq)
insert.add(s1); {
insert.add(sub2); System.out.println(sq);
insert.add(s2); }System.exit(0);
insert.add(sub3); }
insert.add(s3); });
insert.add(iok); iok.addActionListener(new
//adding objects to query frame ActionListener()
fquery.setSize(270,120); {
fquery.setLayout(new FlowLayout()); public void
fquery.add(semister); actionPerformed(ActionEvent e)
fquery.add(qsem); {
fquery.add(per); int tot,a,b,c;
fquery.add(p1); float per;
fquery.add(p2); String q="",res;
fquery.add(qok); try
menu.setVisible(true); {
add.addActionListener(new a=Integer.parseInt(s1.getText());
ActionListener() b=Integer.parseInt(s2.getText());
{ c=Integer.parseInt(s3.getText());
public void tot=a+b+c;
actionPerformed(ActionEvent e) per=(float)tot/3;
{ res=(per<40)?"Fail":(per<50)?"Pass":
insert.setVisible(true); (per<60)?"Second":
} (per<70)?"First":"Distinction";
}); q="INSERT INTO student
query.addActionListener(new VALUES("+usn.getText()
ActionListener() +",'"+name.getText()
IV SEM JAVA LAB MANUAL (MCA, VTU)

+"','"+sem.getText() ))+" AND per BETWEEN


+"',"+a+","+b+","+c+","+tot+","+per+",'" "+Integer.parseInt(p1.getText())+"
+res+"')"; AND "+Integer.parseInt(p2.getText());
stat.executeUpdate(q); result=stat.executeQuery(q);
name.setText(""); flag=result.next();
usn.setText(""); if(!flag)
sem.setText(""); {
s1.setText(""); d[0][0]="no data found";
s2.setText(""); }
s3.setText(""); else
} {
catch(NumberFormatException ne) do
{ {
System.out.println("Error in input"); d[i]
} [0]=Integer.toString(result.getInt(1));
catch(SQLException se) d[i][1]=result.getString(2);
{ d[i]
System.out.println(q); [2]=Integer.toString(result.getInt(3));
System.out.println("Error in d[i]
Query"+se); [3]=Integer.toString(result.getInt(4));
} d[i]
insert.setVisible(false); [4]=Integer.toString(result.getInt(5));
} d[i]
}); [5]=Integer.toString(result.getInt(6));
qok.addActionListener(new d[i]
ActionListener() [6]=Integer.toString(result.getInt(7));
{ d[i]
public void [7]=Integer.toString(result.getInt(8));
actionPerformed(ActionEvent e)
{ d[i]
fquery.setVisible(false); [8]=result.getString(9);
table=new JFrame("Student i++;
Information"); System.out.println(d[i][1]);
table.setSize(800,200); }while(result.next());
Object data[][]; }
String d[][]=new String[50][9],q=""; data= (Object[][]) d;
String head[] = tab=new JTable(data,head);
{"USN","NAME","SEMISTER","SUB1","S sp=new JScrollPane(tab);
UB2","SUB3","TOTAL","PERCENTAGE", table.add(sp);
"RESULT"}; table.setVisible(true);
int i=0; qsem.setText("");
boolean flag=false; p1.setText("");
try p2.setText("");
{ }
q="SELECT * FROM student WHERE catch(SQLException se)
sem="+Integer.parseInt(qsem.getText( {
IV SEM JAVA LAB MANUAL (MCA, VTU)

System.out.println(q); sdos.writeUTF("server.java");
System.out.println("Error in sin=s.getInputStream();
query"+se); dis=new DataInputStream(sin);
} str=dis.readUTF();
catch(NumberFormatException ne) while(!str.equals("end"))
{ {

System.out.println("Error "+ne); System.out.println(str);


} str=dis.readUTF();
} }
}); sin.close();
} s.close();
public static void main(String }catch(Exception e)
arg[]) {
{ System.out.println("Client Error " +e);
SwingUtilities.invokeLater(new }
Runnable() }
{ }
public void run() Server.java
{ import java.io.*;
new db(); import java.net.*;
} public class server
}); {
} public static void main(String arg[]){
} ServerSocket ss=null;
Socket s=null;
6) Client.java String str;
import java.io.*; InputStream sin;
import java.net.*; FileReader fis;
public class client BufferedReader br;
{ DataInputStream dis;
public static void main(String arg[]) OutputStream sout;
{ DataOutputStream sdos;
int c; try
Socket s; {
InputStream sin; ss=new ServerSocket(8080);
DataInputStream dis; System.out.println("server started");
OutputStream sout; }
DataOutputStream sdos; catch(Exception e)
String str; {
try System.out.println("server error");
{ }
s=new Socket("localhost",8080); while(true)
sout=s.getOutputStream(); {
sdos=new DataOutputStream(sout); try
IV SEM JAVA LAB MANUAL (MCA, VTU)

{ {
s=ss.accept(); static String data[]={"Remote " ,
sin=s.getInputStream(); "Method " , "Invocation " , "is " , "Greate
dis=new DataInputStream(sin); "};
String strf=new String(dis.readUTF()); public ServerRMI() throws
fis=new FileReader(strf); RemoteException
br=new BufferedReader(fis); {
sout=s.getOutputStream(); super();
sdos=new DataOutputStream(sout); }
while((str=br.readLine())!=null) public int getLength() throws
{ RemoteException
sdos.writeUTF(str); {
} return data.length;
sdos.writeUTF("end"); }
sout.close(); public String getData(int a)
s.close(); throws RemoteException
} {
catch(Exception e) return data[a];
{ }
System.out.println("server connection public static void main(String
error:"+e); arg[])
System.exit(0); {
}
} try
} {
} ServerRMI sr=new ServerRMI();
Output:
Naming.rebind("ServerRMI",sr);
7)RemoteInterface.java
import java.rmi.*; System.out.println("I am
import java.rmi.server.*; Registered");
public interface RemoteInterface }
extends Remote catch(Exception e)
{ {
public int getLength() throws
RemoteException; System.out.println("Server
public String getData(int a) Error "+e);
throws RemoteException; }
} }
ServerRMI.java }
import java.rmi.*; ClientRMI.java
import java.rmi.server.*; import java.rmi.*;
public class ServerRMI extends import java.rmi.server.*;
UnicastRemoteObject implements class ClientRMI
RemoteInterface {
IV SEM JAVA LAB MANUAL (MCA, VTU)

public static void main(String pw.println("<head>");


arg[]) pw.println("<title>User Name &
{ Password</title>");
try pw.println("</head>");
{ pw.println("<body>");
RemoteInterface pw.println("<h1>Welcome
ri=(RemoteInterface)Naming.lookup(" "+uname+"</h1>");
ServerRMI"); pw.println("<h1>Your Password is
int n=ri.getLength(); "+psd+"</h1>");
for(int i=0;i<n;i++) pw.println("</body>");
System.out.println(ri.getData(i)); pw.println("</html>");
} pw.close();
catch(Exception e) }catch(Exception e)
{ {
PrintWriter pw =
System.out.println("Clieng Error response.getWriter();
"+e); pw.println("Servlet Error");
} pw.close();
} }
} }
}
8) DynamicServlet.java DynamicServlet.html
import java.io.*; <!DOCTYPE HTML PUBLIC
import javax.servlet.*; "-//W3C//DTD HTML 4.01
Transitional//EN">
public class DynamicServlet extends <html>
GenericServlet <head>
{ <title></title>
public void </head>
service(ServletRequest request, <body>
ServletResponse response) throws <form method="POST"
ServletException, IOException action="http://localhost:8080/exampl
{ es/servlet/DynamicServlet">
try <p>&nbsp;&nbsp;&nbsp; Enter your
{ Username and Password</p>
response.setContentType("text/ <p>&nbsp;</p>
html"); <p>&nbsp;&nbsp;&nbsp;&nbsp;
PrintWriter pw = &nbsp;&nbsp;&nbsp;
response.getWriter(); Username&nbsp;&nbsp;&nbsp;
String uname= <input type="text"
request.getParameter("username"); name="username" size="20"></p>
String psd=
request.getParameter("password"); <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
pw.println("<html>"); ;&nbsp;&nbsp;
pw.println("<body>"); Password&nbsp;&nbsp;&nbsp;
IV SEM JAVA LAB MANUAL (MCA, VTU)

<input type="password" pw.write("</h3></pre>");


name="password" size="20"></p> }
<p>&nbsp;</p> pw.flush();
fis.close();
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp pw.close();
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n }catch(Exception e)
bsp;&nbsp;&nbsp;&nbsp;&nbsp; {
<input type="submit" PrintWriter pw =
value="Submit" name="B1"><input response.getWriter();
type="reset" value="Reset" pw.println("Servlet Error");
name="B2"></p> pw.close();
</form> }
}
</body> }
</html> DownloadServlet.html

9) DownloadServlet.java <!DOCTYPE HTML PUBLIC


import java.io.*; "-//W3C//DTD HTML 4.01
import java.net.*; Transitional//EN">
import javax.servlet.*; <html>
<head>
public class DownloadServlet extends <title></title>
GenericServlet </head>
{ <body>
public void <h1><a
service(ServletRequest request, href="http://localhost:8080/examples
ServletResponse response) throws /servlet/DownloadServlet">Dynamic
ServletException, IOException Servlet</a></h1>
{ </body>
try </html>
{
FileReader fis = new 10b) GetServlet.java
FileReader("D:\\ksp\\servlet\\Downl import java.io.*;
oadServlet.java"); import javax.servlet.*;
BufferedReader br=new import javax.servlet.http.*;
BufferedReader(fis);
response.setContentType("text/ public class GetServlet extends
html"); HttpServlet
PrintWriter pw = {
response.getWriter(); public void
String str; doGet(HttpServletRequest request,
while((str=br.readLine())!=null) HttpServletResponse response)
{ throws ServletException, IOException
pw.write("<pre><h3>"); {
pw.write(str);
IV SEM JAVA LAB MANUAL (MCA, VTU)

String </head>
name=request.getParameter("T2"); <body>
<form method="POST"
action="http://localhost:8080/exampl
response.setContentType("text/html") es/servlet/PostServlet">
; <p>&nbsp;</p>
PrintWriter <p><b><font size="4> This is for
pw=response.getWriter(); Post Method</font></b></p>
pw.println("<h1>Your <p> Enter Your Name <input
name is </h1>"+name); type="text" name="T1"
pw.close(); size="20"></p>
} <p> <input type="submit"
} value="Enter" name="B1"><input
type="reset" value="Clear"
PostServlet.java name="B2"></p>
import java.io.*; </form>
import javax.servlet.*; <form method="GET"
import javax.servlet.http.*; action="http://localhost:8080/exampl
es/servlet/GetServlet">
public class PostServlet extends <p>&nbsp;</p>
HttpServlet <p> <b><font size="4">This is for
{ Get Method</font></b></p>
public void <p> Enter Your Name <input
doPost(HttpServletRequest request, type="text" name="T2"
HttpServletResponse response) size="20"></p>
throws ServletException, IOException <p><input type="submit"
{ value="Enter" name="B3"><input
String name= type="reset" value="Clear"
request.getParameter("T1"); name="B4"></p>
response.setContentType("text/ </form> </body> </html>
html");
PrintWriter pw=response.getWriter(); 11)
pw.println("Your name is "+name); RedirectServlet.java
pw.close(); import java.io.*;
} import javax.servlet.*;
} import javax.servlet.http.*;
public class RedirectServlet extends
GetPost.html HttpServlet
<!DOCTYPE HTML PUBLIC {
"-//W3C//DTD HTML 4.01 public void
Transitional//EN"> service(HttpServletRequest request,
HttpServletResponse response)
<html> throws ServletException, IOException
<head> {
<title></title>
IV SEM JAVA LAB MANUAL (MCA, VTU)

13a)
response.setContentType("text/html") 13a.jsp
; <html>
PrintWriter pw = <head><title> PRINTING 10 VEN AND
response.getWriter(); ODD NUMBER</title></head>
response.sendRedirect("http://localho <body bgcolor="cyan">
st:8080/examples/servlets/DynamicS <font size="4">
ervlet.html"); <%
} int i;
} %>
<b><h1>FIRST 10 EVEN NUMBER
12) ARE:</h1></b>
SessionServlet.java <%
import java.io.*; for(i=0;i<20;i+=2)
import java.util.*; {
import javax.servlet.*; %>
import javax.servlet.http.*; <p><%=i%></p>
<%
public class SessionServlet extends ;}
HttpServlet %>
{ <b><h1>FIRST 10 ODD NUMBER
public void ARE:</h1></b>
doGet(HttpServletRequest request, <%
HttpServletResponse response) for(i=1;i<20;i+=2)
throws ServletException, IOException {
{ %>
HttpSession ses= <p><%=i%></p>
request.getSession(true); <%
response.setContentType("text/ ;}
html"); %>
PrintWriter </body>
pw=response.getWriter(); </html>
pw.println("<b>");
Date
d=(Date)ses.getAttribute("date");
if(d!=null)
pw.print("Last access:
"+d+"<br>");
d=new Date();
ses.setAttribute("date",d);
pw.println("Current date :
"+d);
}
}

Potrebbero piacerti anche