0% found this document useful (0 votes)
33 views65 pages

Aaa

Uploaded by

yoratet699
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views65 pages

Aaa

Uploaded by

yoratet699
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 65

PRACTICAL NO.

X. PROGRAM CODE:

1. Design an applet/application to demonstrate the use of Radio Button and CheckBox.


Ans.
import java.awt.*;

import java.applet.*;

public class p1a extends Applet

public void init()

CheckboxGroup cbg =new CheckboxGroup(); Checkbox cl =


new Checkbox("java",true); Checkbox c2 =new
Checkbox("php",false); Checkbox g1 =new Checkbox("c+
+",true,cbg); Checkbox g2 =new Checkbox("c",false,cbg);
add(c1); add(c2); add(g1); add(g2);
}

/*<applet code = p1a.java width = 200 height = 200></applet>*/


2. Design an applet/application to create form using Text Field, Text Area, Button and Label.

Ans.

import java.awt.*;

import java.applet.*;

public class listlang extends Applet

public void init()

Label 11 =new Label("This is a label");

Button b1 =new Button("Button");

TextField t1 =new TextField(20);

String str ="This is TextArea";

TextArea ta1 =new TextArea(str);

add(11);add(b1);add(t1);add(ta1);

/*<applet code = listlang.java width = 200 height = 400></applet>*/


XIII. EXERCISE:

1. Develop a program using Label to display message “Welcome to java”. Ans.


import java.awt .*;

import java.applet .*;

public class form extends Applet

public void init()

Label 11=new Label("Welcome to java");

add(11);

/*<applet code = form.java width = 200 height = 400></applet>*/

2. Develop a program to select multiple languages known to user(e.g. Marathi, Hindi, English, Sanskrit).
Ans.
import java.awt . * ;

import java.applet .*;

public class e2 extends Applet

{
public void init()

List 11 =new List(4,true) ;

11.add(" Hindi ") ;

11.add(" Marathi ") ;

11.add(" Sanskrit ") ;

11.add (" English ");

add(11) ;

/*<applet code = e2.java width = 200 height = 200></applet>*/

3. Write a program to create three buttons with Captions OK, RESET and
CANCEL.
Ans.
import java.awt.*;

import java.applet.*;

public class e3 extends Applet

public void init()

{
Button b1 =new Button ("OK");

Button b2 =new Button("RESET");

Button b3 =new Button("CANCEL");

add(b1);add(b2);add(b3);

/*<applet code = e3.java width = 200 height = 200></applet>*/


PRACTICAL NO.2

X. PROGRAM CODE:

1. Write Java program to show following output.

Ans.

import java.awt.*; import


java.applet.*;
public class p2 extends Applet

public void init()

setLayout(new FlowLayout()); List


c1=new List(3);
c1.add("Summer");

c1.add("Winter");

c1.add("Rainy");
add(c1);
}

/*<applet code=p2.java height=200 width=200></applet>*/


XIII. EXERCISE:

1. Develop an applet/application using List Components to add names of 10 different cities.

Ans.

import java.awt.*;

import java.applet.*;

public class label1 extends Applet

public void init()

List l = new List(10,true);

l.add("Jaipur");

l.add("Thane");

l.add("Mumbai");

l.add("Andheri");

l.add("Vile Parle");

l.add("Santracuz");

l.add("Bandra");

l.add("Nallasopara");

l.add("Panvel");

l.add("Dahisar");

add(l);

/*<applet code = "label1" width=300 height=300></applet> */


2. Develop applet/application to select multiple names of news papers.

Ans.

import java.awt.*; import

java.applet.*;

public class news extends Applet

public void init()

List l = new List(10,true);

l.add("Mint");

l.add("Economic Times of India");

l.add("Gujarat Samachar");

l.add("Divya Bhaskar");

l.add("The Daily Bugle");

l.add("Hindustan Times");

l.add("Punjab Kesari");

l.add("Times of India");

l.add("Mid-Day");

l.add("Maharashtra Times");

add(l);

/*<applet code = "news" width=300 height=300></applet> */


PRACTICAL NO.3

X. PROGRAM CODE:

1. Write a program to demonstrate Grid of 5*5.

Ans.

import java.awt.*; import

javax.swing.*; import

java.awt.*;

public class practice extends JApplet

public void init()

setLayout(new GridLayout(5,5));

int a=1;

for(int i=0;i<5;i++)

for(int m=0;m<5;m++)

add(new JButton("BUTTON"+a));

A++;

/*<applet code="practice.class"height=200 width=200></applet>*/


2. Write a program to display the Number on Button from 0 to 9.

Ans.

import java.awt.*; import

javax.swing.*; import

java.awt.*;

public class practice1 extends JApplet

public void init()

setLayout(new GridLayout()); int a=0;

for(int i=0;i<9;i++)

add(new JButton("BUTTON"+a));
A++;

/*<applet code="practice1.class"height=200 width=200></applet>*/


XIII. EXERCISE:

1. Write a program to generate following output

Ans.

import java.awt.*;

import java.applet.*;

public class gridlayout extends Applet

public void init()

setLayout(new GridLayout(3,2,15,15)); Button b1=new

Button("Button1"); Button b2=new Button("Button2");

Button b3=new Button("Button3"); Button b4=new

Button("Button4"); Button b5=new Button("Button5");

add(b1);add(b2);add(b3);add(b4);add(b5);

/*<applet code="gridlayout.class" width=200 height=200></applet>*/


2. Write a program to generate following output using Border Layout.

Ans.

import java.awt.*;

import java.applet.*;

public class borderlayout extends Applet

public void init()

Button b1=new Button("NORTH");

Button b2=new Button("SOUTH");

Button b3=new Button("EAST");

Button b4=new Button("WEST");

Button b5=new Button("CENTER");

setLayout(new BorderLayout());

add(b1,BorderLayout.NORTH);

add(b2,BorderLayout.SOUTH);

add(b3,BorderLayout.EAST);

add(b4,BorderLayout.WEST);

add(b5,BorderLayout.CENTER);

}}
/*<applet code="borderlayout.class" width=200 height=200></applet>*/
PRACTICAL NO.4

X. PROGRAM CODE:

1. Execute the following Program and write the output. import java.awt.*;

import java.awt.event.*; import

javax.swing.*;

public class CardLayoutExample extends JFrame implements ActionListener{

CardLayout card; JButton b1,

b2, b3; Container c;

CardLayoutExample()

c=getContentPane(); card=new

CardLayout(40,30);

//create CardLayout object with 40 hor space and 30 ver space c.setLayout(card);

b1=new JButton("Apple"); b2=new

JButton("Boy"); b3=new

JButton("Cat");

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);
c.add("a",b1);c.add("b",b2);c.add("c",b3);

public void actionPerformed(ActionEvent e);

card.next(c);

public static void main(String[] args){

CardLayoutExample cl=new CardLayoutExample();

cl.setSize(400,400);

cl.setVisible(true);

cl.setDefaultCloseOperation(EXIT_ON_CLOSE);

}
XIII. EXERCISE:

1. Write Java program to display following output.

Ans.

import java.awt.*;

import javax.swing.*;

public class gridbag123{

public static void main(String args[])

JFrame f=new JFrame("GridBag Layout Demo"); Container co=f.getContentPane();


JButton b1=new JButton("Button 1");

JButton b2=new JButton("Button 2");

JButton b3=new JButton("Button 3");

JButton b4=new JButton("Button 4");

JButton b5=new JButton("Button 5");

JButton b6=new JButton("Button 6");

JPanel jp=new JPanel();

jp.setLayout(new GridBagLayout());

GridBagConstraints cst=new GridBagConstraints();

cst.fill=GridBagConstraints.HORIZONTAL;

cst.gridx=0;

cst.gridy=0;

jp.add(b1,cst);

cst.fill=GridBagConstraints.HORIZONTAL;

cst.gridx=1;

cst.gridy=0;

jp.add(b2,cst);

cst.fill=GridBagConstraints.HORIZONTAL;

cst.gridx=0;

cst.gridy=1;

jp.add(b3,cst);

cst.fill=GridBagConstraints.HORIZONTAL;

cst.gridx=1;

cst.gridy=1;

jp.add(b4,cst);

cst.fill=GridBagConstraints.HORIZONTAL;

cst.gridwidth=2;

cst.gridx=0;

cst.gridy=2;

jp.add(b5,cst);

cst.fill=GridBagConstraints.HORIZONTAL;

cst.gridwidth=2;
cst.gridx=0;

cst.gridy=3;

jp.add(b6,cst);

co.add(jp);

f.setSize(200,200);

f.setVisible(true);

}
}
2. Write Java Program to display following output.

Ans.

import java.awt.*;

import javax.swing.*;

public class ex4b{

public static void main(String args[])

JFrame f=new JFrame("GridBag Layout Demo"); Container


co=f.getContentPane(); co.setLayout(new BorderLayout()); JLabel
L1=new JLabel(" Name ");
JTextField T1=new JTextField(8);

JLabel L2=new JLabel("Comments ");

JTextArea T2=new JTextArea(10,20);

JButton B1=new JButton("SUBMIT");

JPanel jp=new JPanel();

jp.setLayout(new GridBagLayout());
GridBagConstraints cst=new GridBagConstraints();

cst.fill=GridBagConstraints.HORIZONTAL;

cst.gridx=0;

cst.gridy=0;

cst.weighty=0.1;

jp.add(L1,cst);

cst.fill=GridBagConstraints.HORIZONTAL;

cst.gridx=1;

cst.gridy=0;

jp.add(T1,cst);

cst.fill=GridBagConstraints.HORIZONTAL;

cst.gridx=0;

cst.gridy=1;

jp.add(L2,cst);

cst.fill=GridBagConstraints.HORIZONTAL;

cst.gridx=1;

cst.gridy=1;

jp.add(T2,cst);

cst.fill=GridBagConstraints.HORIZONTAL;

cst.gridx=1;

cst.gridy=2;

jp.add(B1,cst);

int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; int


h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS; JScrollPane jsp=new
JScrollPane(jp,v,h); co.add(jsp);
co.add(jp);

f.setSize(200,200);

f.setVisible(true);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
PRACTICAL NO. 5

X. PROGRAM CODE:

1. Write a program which creates Menu of different colors and disable menu item for Black color.

Ans.

import java.awt.*;

import java.awt.event.*;

class menucolor extends Frame{

menucolor(String s)

super(s);

MenuBar mb=new MenuBar();

setMenuBar(mb);

Menu m1=new Menu("Color");

mb.add(m1);

MenuItem i1=new MenuItem("RED");

MenuItem i2=new MenuItem("GREEN");

MenuItem i3=new MenuItem("BLACK");

m1.add(i1);

m1.add(i2);

m1.add(i3);

i3.setEnabled(false);

public static void main(String arg[])

menucolorobj=new menucolor("MENU COLOR");

obj.setSize(200,200);

obj.setVisible(true)

}}
XIII. EXERCISE:

1. Find errors in following program and display output as shown below.

Ans.

import java.awt.*; import java.awt.event.*;


public class tut3 extends Frame
{

MenuBar mb;

MenuItem m1,m2,m3,m4; Menu


mn; MenuShortcut ms; tut3()

setTitle("MenuBar demo");
setSize(500,500); setLayout(null);

MenuShortcut ms=new MenuShortcut(KeyEvent.VK_X); Menu mn=new


Menu("File");

MenuBar mb=new MenuBar();


setMenuBar(mb);

MenuItem m1=new MenuItem("New"); MenuItem


m2=new MenuItem("Open"); MenuItem m3=new
MenuItem("Save As"); MenuItem m4=new
MenuItem("Exit",ms); mn.add(m1);
mn.add(m2);

mn.add(m3);

mn.addSeparator();

mn.add(m4);

mb.add(mn);

public static void main(String ar[])

tut3 md=new tut3();

md.setVisible(true);

}
PRACTICAL NO. 6

X. PROGRAM CODE:

1. Write a program to generate following output.

Ans.

import javax.swing.*;

import java.awt.*;

import java.util.*;

public class practice6 extends JApplet

public void init()

Container co=getContentPane();

co.setLayout(new FlowLayout());

JLabel l1=new JLabel();

JComboBox c1=new JComboBox();

c1.addItem("Mumbai");

c1.addItem("Solapur");

c1.addItem("Pune");

c1.addItem("Banglore");

co.add(c1);

co.add(l1);
l1.setText("You are in "+c1.getSelectedItem());

/*<applet code="practice6.java" width=200 height=200></applet>*/


XIII. EXERCISE:

1. Write a program to develop a frame to select the different states of India using JComboBox.

Ans.

import javax.swing.*;

import java.awt.*;

import java.util.*;

public class e1 extends JApplet

public void init()

Container co=getContentPane();

co.setLayout(new FlowLayout());

co.setSize(200,200);

co.setVisible(true);

JComboBox c=new JComboBox();

c.addItem("Maharashtra");

c.addItem("Gujrat");

c.addItem("Punjab");

c.addItem("Karnataka");

co.add(c);

/*<applet code="e1.java" width=200 height=200></applet>*/


2. Develop a program to demonstrate the use of ScrollPane in Swings.

Ans.

import javax.swing.*; import

java.awt.*; import java.util.*;

public class scrollpane123 extends JApplet

public void init()

Container co=getContentPane(); co.setLayout(new

FlowLayout()); JCheckBox cb1=new JCheckBox("Cricket");

JCheckBox cb2=new JCheckBox("Hockey"); JCheckBox

cb3=new JCheckBox("Football"); JCheckBox cb4=new

JCheckBox("Cricket"); JCheckBox cb5=new

JCheckBox("Hockey"); JCheckBox cb6=new

JCheckBox("Football"); JPanel jp=new JPanel();

jp.add(cb1);

jp.add(cb2);

jp.add(cb3);

jp.add(cb4);

jp.add(cb5);

jp.add(cb6);

int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; int

h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS; JScrollPanejsp=new

JScrollPane(jp,v,h); co.add(jsp);

/*<applet code="scrollpane123.java" height=200


width=200></applet>*/
PRACTICAL NO. 7

X. PROGRAM CODE:

1. Develop a program to demonstrate the use of tree component in swing.

Ans.

import java.awt.*; import


javax.swing.*; import
javax.swing.tree.*;
public class fruitnode123 extends JApplet

public void init()

Container c=getContentPane();

DefaultMutableTreeNode t1=new DefaultMutableTreeNode("Fruits"); DefaultMutableTreeNode


t2=new DefaultMutableTreeNode("Apple");

DefaultMutableTreeNode t3=new
DefaultMutableTreeNode("Mango");

DefaultMutableTreeNode t4=new
DefaultMutableTreeNode("Banana");

t1.add(t2);t1.add(t3);t1.add(t4);

JTree t=new JTree(t1);

add(t);

/*<applet code="fruitnode123.class" width=200 height=200></applet>*/


2. Write a program to generate the following output.

Ans.

import java.awt.*; import


javax.swing.*; import
javax.swing.tree.*;
public class rootnode123 extends JApplet

public void init()

Container c=getContentPane();

DefaultMutableTreeNode top=new
DefaultMutableTreeNode("INDIA");

DefaultMutableTreeNode m=new
DefaultMutableTreeNode("MAHARASHTRA");

top.add(m);

DefaultMutableTreeNode mi=new
DefaultMutableTreeNode("MUMBAI");

DefaultMutableTreeNode p=new DefaultMutableTreeNode("PUNE");

DefaultMutableTreeNode n=new
DefaultMutableTreeNode("NASHIK");

DefaultMutableTreeNode nag=new
DefaultMutableTreeNode("NAGPUR");

m.add(mi);m.add(p);m.add(n);m.add(nag);

DefaultMutableTreeNode g=new
DefaultMutableTreeNode("GUJRAT");

top.add(g);

JTree t=new JTree(top);

int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; int


h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS; JScrollPane jp=new
JScrollPane(t,v,h); c.add(jp);

/*<applet code="rootnode123.class" width=200


height=200></applet>*/
XIII. EXERCISE:

1. Write a JTree program to show root directory and its subFolders of your system.

Ans.

import java.awt.*;

import javax.swing.*;

import javax.swing.tree.*;

public class treenode123 extends JApplet

{ public void init()

{ Container c=getContentPane();

DefaultMutableTreeNode top=new
DefaultMutableTreeNode("OS(C:)");

DefaultMutableTreeNode f=new DefaultMutableTreeNode("Program


Files");

top.add(f);

DefaultMutableTreeNode a=new
DefaultMutableTreeNode("Android");

DefaultMutableTreeNode m=new
DefaultMutableTreeNode("Notepad++");

DefaultMutableTreeNode g=new
DefaultMutableTreeNode("Microsoft Office");

f.add(a);f.add(m);f.add(g);

DefaultMutableTreeNode nvg=new
DefaultMutableTreeNode("Drivers");

top.add(nvg);

DefaultMutableTreeNode r=new DefaultMutableTreeNode("audio");

DefaultMutableTreeNode p=new DefaultMutableTreeNode("input");

DefaultMutableTreeNode cm=new
DefaultMutableTreeNode("network");

DefaultMutableTreeNode b=new DefaultMutableTreeNode("video");

nvg.add(r);nvg.add(p);nvg.add(cm);nvg.add(b); JTree t=new JTree(top);

int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; int


h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS; JScrollPanejsp=new
JScrollPane(t,v,h); c.add(jsp);

} } /*<applet code="treenode123.class" width=200 height=200></applet>*/


PRACTICAL NO. 8

X. PROGRAM CODE:

1. Develop a program to demonstrate the use of JTable.

Ans.

import java.awt.*; import


javax.swing.*; import
java.applet.*;
public class table123 extends JApplet

public void init()

Container co=getContentPane(); co.setLayout(new


BorderLayout()); String co1[]={"roll
no","name","address"}; String rows[]
[]={{"1","Isha","mumbai"},
{"2","Tanu","delhi"},

{"3","Aditya","pune"},

{"4","Adeen","bhopal"}};

JTable t=new JTable(rows,co1);

int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; int


h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS; JScrollPane jsp=new
JScrollPane(t,v,h); co.add(jsp);
}

/*<applet code=table123.java width=500 height=500></applet


2. Write a program code to generate the following output.

Ans.

import java.awt.*;

import javax.swing.*;

import java.applet.*;

public class jtable extends JApplet

public void init()

Container co=getContentPane();

co.setLayout(new BorderLayout());

String co1[]={"ID","NAME","SALARY"};

String rows[][]={{"101","Isha","670000"},

{"102","Tanu","780000"},

{"101","Aditya","700000"}};

JTable t=new JTable(rows,co1);

int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; int


h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS; JScrollPanejs=new
JScrollPane(t,v,h); co.add(js);
}

/*<applet code=jtable.java width=500 height=500></applet>*/


XIII. EXERCISE:

1. Write a Java program to create a table of Name of Student, Percentage and Grade of 10 students using
JTable.

Ans.

import java.awt.*;

import javax.swing.*;

import java.applet.*;

public class table12 extends JApplet

public void init()

Container co=getContentPane();

co.setLayout(new BorderLayout());

String co1[]={"Name of the Student","Percentage","Grade"}; String rows[][]={{"Isha


Darekar","90","Grade A"},
{"Khushi","70","Grade B"},

{"Rose DSouza","80","Grade A"},

{"Mahir Malhotra","69","Grade C"},

{"Vijay Tiwari","58","Grade D"},

{"Aditya Kamble","88","Grade A"},

{"neha Shah","66","Grade C"},

{"nish Khan","90","Grade A"},

{"Tanu Narkar","75","Grade B"},

{"Shashank Bala","70","Grade B"}};

JTable t=new JTable(rows,co1);

int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; int


h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS; JScrollPanejs=new
JScrollPane(t,v,h);
co.add(js);

/*<applet code=table12.java width=500 height=500></applet>*/


PRACTICAL NO. 9

X. PROGRAM CODE:

1. Write a program code to generate the following output.

Ans.

import java.awt.*;

import javax.swing.*;

public class jpbar123 extends JFrame{

JProgressBarjb;

int i=0, num=0;

jpbar123()

jb=new JProgressBar();

jb.setBounds(40,40,160,30);

jb.setValue(0);

jb.setStringPainted(true);

add(jb);

setSize(250,150);

setLayout(null);

public void iterate(){

while(i<=2000){

jb.setValue(i);

i=i+20;

try{Thread.sleep(150);}catch(Exception e){}

public static void main(String args[]){

jpbar123 jp=new jpbar123();

jp.setVisible(true);

jp.iterate();
jp.setTitle("JPROGRESSBAR");}}
XIII. EXERCISE:

1. Develop a program to demonstrate the use of JProgressBar.

Ans.

import java.awt.*; import


javax.swing.*;
public class jpbar12 extends JFrame

JProgressBar jb;
jpbar12()
{

jb=new JProgressBar(0,2000);
setLayout(null); jb.setValue(0);
jb.setBounds(40,40,160,30);
jb.setStringPainted(true); add(jb);
setSize(200,200); setVisible(true);
}

public void loop()

int i=0;
while(i<=1000)
{

jb.setValue(i+10);

i=i+20;

try
{

Thread.sleep(1000);

catch(Exception e){}

public static void main(String args[]){

jpbar12 jp=new jpbar12();

jp.loop();

}}
2. Write a Program using JProgressBar to show the progress of ProgressBar when user clicks on
JButton.

Ans.

import java.awt.event.*;

import javax.swing.*;

class jpbar1 extends JFrame implements ActionListener

JProgressBar pb;

JButton b1=new JButton("LOGIN");

jpbar1()

pb=new JProgressBar(0,2000);
setLayout(null);

pb.setValue(0);

pb.setBounds(110,20,200,25);

b1.setBounds(20,20,80,25);

pb.setStringPainted(true);

add(pb);

add(b1);

b1.addActionListener(this);

setResizable(false);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setSize(200,200);

setVisible(true);}
public void actionPerformed(ActionEvent ae)

int i=0;

if(ae.getSource()==b1)

pb.setVisible(true);

try

while(i<=100)

Thread.sleep(50);

pb.paintImmediately(0,0,200,25);
pb.setValue(i);

i++;

}
}

catch(Exception e1)

System.out.println("exception is="+e1);

public class Progress1

public static void main (String args[])

jpbar1 m=new jpbar1();

m.setSize(300,100);

m.setVisible(true);

}
PRACTICAL NO. 10

X. PROGRAM CODE:

1. Write a program to generate KeyEvent when a ke is pressed and display “Key Pressed” message.

Ans. import java.awt.*;

import java.applet.*;

import java.awt.event.*;

public class keyevent1234 extends Applet

public void init()

addMouseListener(new mymouselistener());

class mymouselistener extends MouseAdapter

public void mousePressed(MouseEvent me)

showStatus("pressed");

repaint();

/*<applet code="keyevent1234.java" width=200 height=200></applet>*/


2. Develop a program which will implement special keys such as function keys and arrow keys.

Ans.

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

public class keyevent34 extends Applet implements KeyListener

String msg="";

public void init()

addKeyListener(this);

public void keyReleased(KeyEvent ke)

showStatus("UP");

public void keyPressed(KeyEvent ke)

{
showStatus("DOWN");

public void keyTyped(KeyEvent a)

msg+=a.getKeyChar();

repaint();

public void paint(Graphics g)

g.drawString(msg,30,70);

/*<applet code="keyevent34.java" width=200 height=200></applet>*/


XIII. EXERCISE:

3. Develop a program to accept two numbers and display product of two numbers when user pressed “Multiply” button.

Ans.

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

public class minical extends Applet implements ActionListener

TextField t1, t2, t3;

Button b1;

Label l1, l2, l3;

public void init()

setLayout(new GridLayout(2,2));

t1=new TextField(15);

t2=new TextField(15);

t3=new TextField(15);

l1=new Label("1st NUMBER");

l2=new Label("2nd NUMBER");

l3=new Label("MULTIPLICATION IS");

b1=new Button("MULTIPLICATION");

add(l1);add(t1);

add(l2);add(t2);

add(l3);add(t3);

add(b1);

b1.addActionListener(this);

public void actionPerformed(ActionEvent ae)

if(ae.getSource()==b1)

int n=Integer.parseInt(t1.getText());

int m=Integer.parseInt(t2.getText());

int s=n*m;

t3.setText(Integer.toString(s));

/*<applet code=minical.java width=200 height=200></applet>*/


PRACTICAL NO. 11

X. PROGRAM CODE

1. Debug the following code and write the output.

Ans.

import java.applet.Applet; import java.awt.*; import


java.awt.event.*;

/*<applet code= "mousedemo.java" width=300 height=200></applet>*/ public class mousedemo extends Applet implements
MouseListener

Label l;

public void init()

setLayout(null);

l=new Label("Hello Mouse");

l.setBounds(0,100,100,100);

add(l);

public void mousePressed(MouseEvent e)

l.setText("Mouse Pressed no. of clicks:" + e.getClickCount() + "at position" + e.getX() + "," +e.getY());

public void mouseReleased(MouseEvent e)

l.setText("Mouse Realeased;# of clicks:" + e.getClickCount());

public void mouseEntered(MouseEvent e)

{l.setText("Mouse Entered");

public void mouseExited(MouseEvent e)

l.setText("Mouse exited");

public void mouseClicked(MouseEvent e)

l.setText("mouse clicked(# of clicks:" + e.getClickCount());

}
XIII. EXERCISE:

1. Write a program to change the background color of Applet when user performs events using Mouse.

Ans.

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

public class Mousecolor extends Applet implements MouseListener

Label l;

public void init()

setLayout(null);

addMouseListener(this);

public void mousePressed(MouseEvent e)

setBackground(Color.red);

public void mouseReleased(MouseEvent e)

setBackground(Color.green);

public void mouseEntered(MouseEvent e)

setBackground(Color.pink);

public void mouseExited(MouseEvent e)

setBackground(Color.red);

public void mouseClicked(MouseEvent e)

setBackground(Color.yellow);

/*<applet code="Mousecolor.java" width=200 height=200></applet>*/


2. Write a program to count the number of clicks performed by the user in a Frame window.

Ans.

3. Write a program to demonstrate the use of mouseDragged and mouseMoved method of


MouseMotionListener.

Ans.

import java.awt.event.*;

import java.applet.Applet;

public class exp11_3 extends Applet

public void init()

addMouseMotionListener(new demo());

public class demo implements MouseMotionListener

public void mouseMoved(MouseEvent me)

int x=me.getX();

int y=me.getY();

showStatus("Moved at"+x+","+y);

public void mouseDragged(MouseEvent me)

int x=me.getX();

int y=me.getY();

showStatus("Dragged at"+x+","+y);

}
}

/*<applet code="exp11_3.java" width=300 height=200></applet>*/


PRACTICAL NO. 12

X. PROGRAM CODE:

1. Write a program using JPasswordField to set the password character as ‘#’ instead of ‘*’.

Ans.

import java.awt.*;

import java.applet.*;

import javax.swing.*;

public class exp12p extends JApplet

public void init()

setLayout(new FlowLayout(FlowLayout.LEFT)); JLabel l1=new


JLabel("ENTER NAME:"); JLabel l2=new JLabel("ENTER
PASSWORD:"); JTextField t=new JTextField(15); JPasswordField
p =new JPasswordField(15); p.setEchoChar('#');
JButton b1=new JButton("LOGIN");

add(l1);add(t);add(l2);add(p);add(b1);

/*<applet code=exp12p.java height=200 width=200></applet>*/


XIII. EXERCISE:

1. Write a program using JPasswordField and JTextField to demonstrate the use of user authentication.

Ans.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class exp12_1 extends JApplet implements ActionListener

Label l1,l2,l3;

JTextField t1;

JPasswordField jp1;

JButton b1;

public void init()

setLayout(new FlowLayout());

l1=new Label("ENTER NAME:");

l2=new Label("ENTER PASSWORD:");

t1=new JTextField(15);

jp1 =new JPasswordField(15);

l3=new Label("");

b1=new JButton("LOGIN");
add(l1);add(t1);add(l2);add(jp1);add(b1);add(l3);
b1.addActionListener(this);
}

public void actionPerformed(ActionEvent ae)

if(t1.getText().equals("Isha")&&(jp1.getText().equals("tpoly")))

l3.setText("YOU ARE AUTHENTUCATED");

else

l3.setText("Invalid User");

/*<applet code=exp12_1.java height=200 width=200></applet>*/


2. Write a program using JTextField to perform addition of two numbers.

Ans.

import java.awt.*; import


java.awt.event.*; import javax.swing.*;
public class addition extends JFrame implements ActionListener

JLabel l1,l2,l3; JTextField


t1,t2; JButton b1; public
addition()
{

l1=new JLabel("Enter 1st number"); l2=new JLabel("Enter 2nd


number"); l3=new JLabel();
t1=new JTextField(10);
t2=new JTextField(10);

b1=new JButton("Addition");

b1.addActionListener(this);

add(l1);add(t1);add(l2);add(t2);add(b1);add(l3);

setLayout(new FlowLayout());

setSize(500,500);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

public void actionPerformed(ActionEvent e)

int a,b,c;

a=Integer.parseInt(t1.getText());

b=Integer.parseInt(t2.getText());

c=a+b;

l3.setText("Addition:-"+Integer.toString(c));

public static void main(String[] args)

addition a=new addition();

}
3. Write a program using JPasswordField to accept password from user and if the length is less than 6 characters then error message
should be displayed “Password length must be >6 characters”.

Ans.

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

import javax.swing.*;

public class exp12_3 extends JApplet implements ActionListener

JLabel l1,l2,l3;

JTextField t1;

JPasswordField p1;

JButton b1;

public void init()

setLayout(new FlowLayout(FlowLayout.LEFT));

l1=new JLabel("Enter username");

l2=new JLabel("Enter password");

l3=new JLabel("");

t1=new JTextField(15);

p1=new JPasswordField(15);

b1=new JButton("LOGIN");

b1.addActionListener(this);

add(l1);add(t1);add(l2);add(p1);add(b1);add(l3);

b1.addActionListener(this);

public void actionPerformed(ActionEvent e)

{
if((p1.getPassword().length<6))

l3.setText("Password must be >6 characters");

else

l3.setText("OK");

/*<applet code=exp12_3.java width=200 height=200></applet>*/


PRACTICAL NO. 14

X. PROGRAM CODE:

Execute the following code and write the output.

import java.io.*;

import java.net.*;

public class InetDemo

public static void main(String[] args)

try

InetAddress ip=InetAddress.getByName("localhost");

System.out.println("Host Name: "+ip.getHostName());

System.out.println("IP Address: "+ip.getHostAddress());

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

}
XIII. EXERCISE:

1. Develop a program using InetAddress class to retrieve IP address of computer when


hostname is entered by the user.

Ans.

import java.net.*;

public class exp14_1

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

try

InetAddress i=InetAddress.getByName("www.msbte.com");

System.out.println(i);

catch(Exception e){}

}
PRACTICAL NO. 15

X. PROGRAM CODE:

Execute the following code and write the output.

import java.net.*;

class URLDemo

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

URL hp=new URL("https://www.javatpoint.com/javafx-tutorial");


System.out.println("Protocol: "+hp.getProtocol()); System.out.println("Port:
"+hp.getPort()); System.out.println("Host: "+hp.getHost()); System.out.println("File:
"+hp.getFile()); System.out.println("Ext: "+hp.toExternalForm());
}

}
XIII. EXERCISE:

1. Write a program using URL class to retrieve the host, protocol, port and file of URL http://www.msbte.org.in

Ans.

import java.net.*;

public class exp15_1

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

URL hp=new URL("https://msbte.org.in");

System.out.println("Protocol: "+hp.getProtocol());

System.out.println("Port: "+hp.getPort());

System.out.println("Host: "+hp.getHost());

System.out.println("File: "+hp.getFile());

2. Write a program using URL and URLConnection class to retrieve the date, content type, content length information
of any entered URL

Ans.

import java.net.URL;

import java.net.URLConnection;

import java.util.Date;

public class exp15_2

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

URL url=new URL("https://msbte.org.in"); URLConnection u=url.openConnection(); System.out.println("Content Type:


"+u.getContentType()); System.out.println("Date: "+new Date(u.getDate())); System.out.println("Last Modified: "+new
Date(u.getLastModified())); System.out.println("Expiration Dtae: "+new Date(u.getExpiration()));

System.out.println("Content Length: "+new Date(u.getContentLength()));

}
PRACTICAL NO. 18

XIII. EXERCISE:

1. Develop a program to create employee table in database having two columns “emp_id” and “emp_name”.

Ans.

import java.sql.*;

class create {
public static void main(String ar[]) throws Exception { try {
Class.forName("com.mysql.cj.jdbc.Driver"); System.out.println("driver
loaded"); String uname = "root"; String pass = "Vscode@1";

String url = "jdbc:mysql://localhost:3306/student";


Connection con = DriverManager.getConnection(url, uname, pass);
System.out.println("connection established");
Statement st = con.createStatement();
String sql = "CREATE TABLE Employee" +
"(emp_id int not null primary key," +
" emp_namevarchar(20))"; st.executeUpdate(sql);
System.out.println("Table Created"); st.close();
con.close();
} catch (Exception e) {
}
}
}

2. Develop a program to display the name and roll_no of students from “student table” having
percentage >70.
Ans.
import java.sql.*;

class develop {
public static void main(String ar[]) throws Exception { try {
Class.forName("com.mysql.cj.jdbc.Driver"); System.out.println("driver
loaded"); String uname = "root"; String pass = "Vscode@1";

String url = "jdbc:mysql://localhost:3306/student";


Connection con = DriverManager.getConnection(url, uname, pass);
System.out.println("connection established");
Statement st = con.createStatement();
String sql = "Select * from Student where Percentage > 70"; ResultSetrs = st.executeQuery(sql); while
(rs.next()) {

System.out.println(
"Roll no. " + rs.getInt(1) + "\nName: " + rs.getString(2) +
"\nPercentage: " + rs.getFloat(3));
}
System.out.println("data Displayed");
st.close();
con.close();
} catch (Exception e) {
}
}
}
PRACTICAL NO. 19

X. PROGRAM CODE:

1. Write a Program to update row of student table from MSBTE database using Mysql 5.5 a database server.
Ans:
import java.sql.*;

class Exp19_1 {
public static void main(String ar[]) throws Exception { try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("driver loaded"); String uname = "root";
String pass = "Vscode@1";

String url = "jdbc:mysql://localhost:3306/MSBTE";


Connection con = DriverManager.getConnection(url, uname, pass);
System.out.println("connection established");
String sql = "Update student set name='Levi', Location='Titan' Where roll=2";
PreparedStatement ps = con.prepareStatement(sql);
ps.executeUpdate();
System.out.println("data Updated");
ps.close();
con.close();
} catch (Exception e) {
}
}
}

2. Write the output of following JDBC code. Use Mysql server 5.5 as database Server.
Ans:
XIII. EXERCISE:

1. Develop JDBC program to retrieve data using ResultSet.


Ans:
import java.sql.*;

class Exp19_3 {
public static void main(String ar[]) throws Exception { String url =
"jdbc:mysql://localhost:3306/MSBTE"; String uname = "root";
String pass = "Vscode@1";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection(url, uname, pass); String query = "select *
from student";
PreparedStatementst = con.prepareStatement(query); ResultSet rs =
st.executeQuery(); while (rs.next()) {
System.out.println("Roll no. " + rs.getInt(1) + "\t");
System.out.println("Name: " + rs.getString(2) + "\t");
System.out.print("Location: " + rs.getString(3));
}
st.close();
con.close();
}
}

2. Develop a program to update a record in database table


Ans:

import java.sql.*;

class Exp19_4 {
public static void main(String ar[]) throws Exception { try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("driver loaded");
String uname = "root";
String pass = "Vscode@1";
String url = "jdbc:mysql://localhost:3306/MSBTE";
Connection con = DriverManager.getConnection(url, uname, pass);
System.out.println("connection established");
String sql = "Update student set name='Levi', Location='Titan' Where roll=2";
PreparedStatementps = con.prepareStatement(sql);
ps.executeUpdate();
System.out.println("data Updated");
ps.close();
con.close();
} catch (Exception e) {
}
}
}
PRACTICAL NO. 20
X. PROGRAM CODE:

1. Write a program to delete a record from a table.


Ans:
import java.sql.*;

class Exp20_1 {
public static void main(String ar[]) throws Exception { try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("driver loaded"); String uname = "root";
String pass = "Vscode@1";

String url = "jdbc:mysql://localhost:3306/MSBTE";


Connection con = DriverManager.getConnection(url, uname, pass);
System.out.println("connection established");
String sql = "Delete From student where roll=2";
PreparedStatement ps = con.prepareStatement(sql);
ps.executeUpdate();
System.out.println("data deleted");
ps.close();
con.close();
} catch (Exception e) {
}
}
}

2. Write the output of following JDBC code.


Ans:
XIII. EXERCISE:

1. Develop a program to update name of a student from Jack to John.


Ans:
import java.sql.*;

class Exp20_3 {
public static void main(String ar[]) throws Exception { try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("driver loaded"); String uname = "root";
String pass = "Vscode@1";

String url = "jdbc:mysql://localhost:3306/MSBTE";


Connection con = DriverManager.getConnection(url, uname, pass);
System.out.println("connection established");
String sql = "Update student set name='John' where name='Jack'";
PreparedStatement ps = con.prepareStatement(sql);
ps.executeUpdate();
System.out.println("data Updated");
ps.close();
con.close();
} catch (Exception e) {
}
}
}
2. Develop a program to delete all record for a product whose "price is greater than 500" and Id is "P1234".
Ans.
import java.sql.*;

class Exp20_4 {
public static void main(String ar[]) throws Exception {

try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("driver loaded");
String uname = "root";
String pass = "Vscode@1";
String url = "jdbc:mysql://localhost:3306/MSBTE";
Connection con = DriverManager.getConnection(url, uname, pass);
System.out.println("connection established");
String sql = "Delete from product where price>500 and id='P1234'";
PreparedStatementps = con.prepareStatement(sql);
ps.executeUpdate();
System.out.println("data deleted");
ps.close();
con.close();
} catch (Exception e) {
}
}
}
PRACTICAL NO. 21
X. PROGRAM CODE:

1. Write a Program to display following output in browser Window.


Ans:
<!DOCTYPE
html><html>
<head>
<title>Welcome</title><meta
charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"></head>
<body>
<b>Welcome to Maharashtra State Board of Technical Education</b></body>
</html>

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ser1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
pw.close();
}
}

2. Write the output of following code considering below HTML is front end and servlet as back end
Ans:
XIII. EXERCISE:
1. Develop servlet program to print Hello MSBTE in browser window.
Ans:
<!DOCTYPE
html><html>
<head>
<title>Welcome</title><meta
charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"></head>
<body>
<b>Hello
MSBTE</b></body>
</html>

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class ser1
extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
pw.close();
}
}
2. Develop a program to receive the parameter through HTML forms and send back
received parameter to browser.
Ans:
<html>
<head>
<title>Servlet 3</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="pass">
Enter Username: <input type = "text" name = "n">
<!--Enter Password: <input type = "password" name = "p">--><input type="submit"
name="SUBMIT">
</form>

</body>
</html>

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

public class pass extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException
{ response.setContentType("text/html"); PrintWriter
pw=response.getWriter(); String name =
request.getParameter("n"); pw.println("Name: "+name);
}
}
PRACTICAL NO. 22
X. PROGRAM CODE:
1. Write a Program to send the username to server and server will send the length of username to client.

Ans:

<!DOCTYPE html>

<html>

<head>

<title>Servlet</title>

<meta charset="UTF-8">

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

<body><form action="length1">

Enter Name: <input type="text" name="n">

<input type="submit" value="SUBMIT"></form></body></html>

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class length1 extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter pw=response.getWriter();

String len=request.getParameter("n");

pw.println("Length= "+len.length());
pw.close();

}
2. Write the output of following code considering below HTML is front end and servlet as back end.

Ans:
XIII. EXERCISE:

1. Develop servlet program to retrieve data from List and Radio Button using HTML Forms.

Ans:

<html>

<head>

<title>Servlet 4</title>

<meta charset="UTF-8">

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

</head>

<body>

<form action="last">

Enter name: <input type="text" name="n"></br></br>

Select Gender: <input type="radio" name="g" value="Male">Male <input type="radio" name="g"


value="Female">Female</br></br><select name="sub" size="4" >

<option value="AJP">AJP</option>

<option value="OSY">OSY</option>

<option value="ACN">ACN</option>

<option value="EST">EST</option>

</select><br>

<input type="submit" name="SUBMIT"></form>>

</body>

</html>

import java.io.IOException;

import java.io.PrintWriter;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class last extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException{


response.setContentType("text/html"); PrintWriter pw=response.getWriter();
String name=request.getParameter("n"); String
gender=request.getParameter("g"); String
subject=request.getParameter("sub"); pw.println("Name= "+name);
pw.println("\nGender= "+gender); pw.println("\nSubject= "+subject);
pw.close();

}
2. Develop a program to receive student subject marks through HTML forms TextField and send the response as passed or Failed in
Examination...
Ans:

<!DOCTYPE html>

<html>

<head>

<title>Experiment 22</title>

<meta charset="UTF-8">

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

</head>

<body>

<form action="StudentMarks">

Enter Your Total_Marks: <input type="number" name="m"><input type="submit" name="submit">


</form>

</body>

</html>

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class StudentMarks extends HttpServlet {

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter pw=response.getWriter();

String marks=request.getParameter("m");

if(Integer.parseInt(marks)>=40)

pw.println("Pass");

else

pw.println("Fail");

}
PRACTICAL NO. 23

X. PROGRAM CODE:

1. Write a Program to Session id and Session time in Browser Window.

Ans:

import java.io.IOException; import


java.io.PrintWriter;

import javax.servlet.ServletException; import


javax.servlet.http.HttpServlet; import
javax.servlet.http.HttpServletRequest; import
javax.servlet.http.HttpServletResponse; import
javax.servlet.http.HttpSession;
public class Exp_23 extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter pw = response.getWriter();

HttpSession hs=request.getSession();

String id=hs.getId();

pw.println("Session Id: "+id);

pw.close();

2. Write the output of following code considering below HTML is front end and servlet as back end.

Ans.
XIII. EXERCISE:

1. Develop servlet program to display various details about session using HttpSession methods.

Ans:

<!DOCTYPE html>

<html>

<head>

<title>Exp23_3</title>

<meta charset="UTF-8">

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

</head>

<body>

<form action="Servlet1">

Name:<input type="text" name="n"/><br/><input type="submit"


value="go"/>

</form>

</body>

</html>

import java.io.*;

import java.servlet.*;

import java.servlet.http.*;

public class Servlet1 extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException,
IOException{ response.setContentType("text/html"); PrintWriter
pw=response.getWriter(); String name=request.getParameter("n");
pw.println("Welcome "+name); HttpSession
session=request.getSession();
session.setAttribute("uname",name); pw.println("<a href='Servlet2'> visit
<a/>"); pw.close();

import java.io.*;

import java.servlet.*;

import java.servlet.http.*;

public class Servlet2 extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter pw=response.getWriter();

HttpSession session=request.getSession(false);

String n=(String)session.getAttribute("uname");

pw.print("Hello "+n);

pw.close();}}
PRACTICAL NO. 24

X. EXERCISE:
1. Develop a program to collect user information using cookie.

Ans.

<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>
<form action="cookie1">
Enter Cookie data: <input type="text" name="data"><input type="submit" value="submit">
</form>
</body>
</html>

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class cookie1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
String d = request.getParameter("data");
Cookie co = new Cookie("MyCookie",d);
response.addCookie(co);
pw.println("your cookie has been set to"+d);
pw.println("<a href='cooval1'>view details</a>");
pw.close();
}
}
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class cooval1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
Cookie co[]=request.getCookies();
for(int i=0; i<co.length; i++){ pw.println("Name-> "+co[i].getName());
pw.println("Value-> "+co[i].getValue());
}}

You might also like