Ajp Exp 1-24
Ajp Exp 1-24
Ajp Exp 1-24
Seal of
Institution
.
Advanced Java Programming (22517)
Preface
The primary focus of any engineering laboratory/field work in the technical education
system is to develop the much-needed industry relevant competencies and skills. With this in
view, MSBTE embarked on this innovative ‘I’ Scheme curricula for engineering Diploma
programmes with outcome-based education as the focus and accordingly, relatively large
amount of time is allotted for the practical work. This displays the great importance of
laboratory work making each teacher, instructor and student to realize that every minute of
the laboratory time need to be effectively utilized to develop these outcomes, rather than
doing other mundane activities. Therefore, for the successful implementation of this
outcome-based curriculum, every practical has been designed to serve as a ‘vehicle’ to
develop this industry identified competency in every student. The practical skills are difficult
to develop through ‘chalk and duster’ activity in the classroom situation. Accordingly, the ‘I’
scheme laboratory manual development team designed the practicals to focus on outcomes,
rather than the traditional age-old practice of conducting practical’s to ‘verify the theory’
(which may become a byproduct along the way).
This laboratory manual is designed to help all stakeholders, especially the students,
teachers and instructors to develop in the student the pre-determined outcomes. It is expected
from each student that at least a day in advance, they have to thoroughly read the concerned
practical procedure that they will do the next day and understand minimum theoretical
background associated with the practical. Every practical in this manual begins by identifying
the competency, industry relevant skills, course outcomes and practical outcomes which
serve as a key focal point for doing the practical. Students will then become aware about the
skills they will achieve through procedure shown there and necessary precautions to be taken,
which will help them to apply in solving real-world problems in their professional life.
This manual also provides guidelines to teachers and instructors to effectively facilitate
student-centered lab activities through each practical exercise by arranging and managing
necessary resources in order that the students follow the procedures and precautions
systematically ensuring the achievement of outcomes in the students.
Although all care has been taken to check for mistakes in this laboratory manual, yet it
is impossible to claim perfection especially as this is the first edition. Any such errors and
suggestions for improvement can be brought to our notice and are highly welcome.
Programme Outcomes (POs) to be achieved through Practical’s of this
Course:
Following programme outcomes are expected to be achieved significantly out of the ten
programme outcomes and Information Technology programme specific outcomes through
the practical’s of the course on Advanced Java Programming.
PO 3. Experiments and practice: Plan to perform experiments, practices and to use the results
to solve Computer Engineering related problems.
PO 5. The engineer and society: Assess societal, health, safety and legal issues and the
consequent responsibilities relevant to practice in the field of Computer Engineering.
PO 8. Individual and team work: Function effectively as a leader and team member in diverse/
multidisciplinary teams.
PO 10. Life-long learning: Engage in independent and life-long learning along with the
technological changes in the IT and allied industry.
The following industry relevant skills of the competency “Apply advanced Java
Programming Concepts” are expected to be developed in you by performing practical’s of
this laboratory manual.
1. Create GUI Applications.
2. Handle events using event delegation model.
3. Write and execute programs for web based as well as window-based applications.
4. Write and execute programs for networking.
5. Write and execute programs to store the data in database to perform CRUD operations.
6. Write and execute programs for client server using Servlets.
Student shall read the points given below for understanding the theoretical concepts and
practical applications.
1. Students shall listen carefully the lecture given by teacher about importance of subject,
learning structure, course outcomes.
2. Students shall organize the work in the group of two or three members and make a record
of all observations.
3. Students shall understand the purpose of experiment and its practical implementation.
4. Students shall write the answers of the questions during practical.
5. Student should feel free to discuss any difficulty faced during the conduct of practical.
6. Students shall develop web based and window-based applications as expected by the
industries.
7. Student shall attempt to develop related hands on skills and gain confidence.
8. Students shall refer technical magazines; websites related to the scope of the subjects
and update their knowledge and skills.
9. Students shall develop self-learning techniques.
10. Students should develop habit to submit the write-ups on the scheduled dates and time.
Content Page
List of Practical’s and Progressive Assessment Sheet
Date of Dated
Sr. Page Date of Assessment Remarks
Title of the practical submissi sign. of
No. No. performance Marks (50) (if any)
on teacher
Write a program to
demonstrate the use of
AWT components like
1. Label, Textfield, 1 2/09/21 09/09/21
TextArea, Button,
Checkbox, RadioButton
etc.
Write a program to
design a form using the
2. 6 3/09/21 16/09/21
components List and
Choice.
Write a program to
design simple calculator
3. 12 03/09/21 16/09/21
with the use of
GridLayout
Write a program to create
a two-level card deck that
allows the user to select
4. 19 16/09/21 23/09/21
component of Panel
using
CardLayout
Write a program to
demonstrate the use of
14. 75 14/10/21 21/10/21
InetAddress class and
its factory methods.
Write a program to
demonstrate the use of
15. 80 21/10/21 28/10/21
URL and URLConnection
class and its methods
Write a program to
implement chat Server
16. 85 22/10/21 29/10/21
using Server Socket and
Socket class.
Write a program to
demonstrate use of
17. DatagramSocket and 90 22/10/21 29/10/21
DataGram Packet
Write a program to insert
and retrieve the data
18. 95 28/10/21 19/11/21
from database using
JDBC
Write a program to
demonstrate the use of
19. PreparedStatement and 102 29/10/21 25/11/21
ResultSet interface.
Write a program to update
20. and delete a record from a 110 29/10/21 25/11/21
database table.
Write a program to
demonstrate the use of
21. HttpServlet as a 116 18/11/21 02/12/21
parameterized servlet
Write a Servlet program
to send username and
22. password using HTML 123 25/11/21 03/12/21
forms and authenticate
the user
Write a program to create
23. Session using HttpSession 130 25/11/21
03/12/21
class.
Write a program to
24. implement Session 137 02/12/21 09/12/21
tracking using Cookies.
Total Marks
1.
import java.awt.*;
AwtExample()
{
setSize(500,500);
setLayout(null);
setVisible(true);
Output:
2.
import javax.swing.*;
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Output:
3.
import javax.swing.*;
import java.awt.*;
JCheckBox c2 = new
JCheckBox("Marathi");
c2.setBounds(150,130,100,20); f.add(c2);
JCheckBox c4 = new
JCheckBox("Sanskrit");
c4.setBounds(150,190,100,20); f.add(c4);
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main (String
args[])
{
new JCBExample();
}
}
Output:
4.
import javax.swing.*;
import java.awt.*;
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main (String
args[])
{
new JButtonExample();
}
}
Output:
Import java.awt.*;
Class Uselist
Int I;
f.setSize(350,300);
f.setLayout(new FlowLayout());
f.setVisible(true);
Season.add(“Summer”);
Season.add(“Winter”); Season.add(“Rainy”);
f.add(season);
Output:-
1.
Import java.applet.*;
Import java.awt.*;
Import java.awt.event.*;
L1.setBounds(50,60,100,120);
L1.add(“DSU”);
L1.add(“PIC”);
L1.add(“OOP”);
L1.add(“CGR”);
L1.add(“DTE”);
L1.add(“Microprocessor”);
L1.add(“CN”);
L1.add(“DBMS”);
L1.add(“SEN”);
L1.add(“JAVA”);
Add(l1);
setLayout(null);
}
Public void paint(Graphics g)
Repaint();
Output:-
2.
Import java.applet.*;
Import java.awt.*;
Import java.awt.event.*;
L1.add(“Times of India”);
L1.add(“Cwiedu”);
L1.add(“NDTEV”);
L1.add(“Gadgets360”);
Add(l1);
setLayout(null);
Repaint();
Output:-
1.
Import java.awt.*;
f.setVisible(true);
f.setSize(400,400);
f.setLayout(new GridLayout(2,2));
f.setFont(font);
String s = “”;
S = s.valueOf(i+1);
System.out.println©;
L[i].setBackground©;
L[i].setText(s);
{
f.add(l[i]);
Output:-
2.
Import java.awt.*;
Import java.awt.event.*;
Grid()
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(b7);
f.add(b8);
f.add(b9);
f.setLayout(new GridLayout(3,3));
f.setSize(500,500);
f.setVisible(true);
New Grid();
Output:-
1.
Import java.awt.*;
Import java.applet.*;
Frame f;
MyGridLayout()
F = new Frame();
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.setLayout(new GridLayout(3,2));
f.setVisible(true);
New MyGridLayout();
Output:-
2.
Import java.awt.*;
Import java.awt.event.*;
Region()
f.add(b1,BorderLayout.EAST);
f.add(b2,BorderLayout.WEST);
f.add(b3,BorderLayout.SOUTH);
f.add(b4,BorderLayout.NORTH);
f.add(b5,BorderLayout.CENTER);
f.setSize(300,300);
f.setVisible(true);
New Region();
Output:-
1.
Import java.awt.Button;
Import java.awt.GridBagConstraints;
Import java.awt.GridBagLayout;
Import javax.swing.*;
Public GridBagLayoutExample() {
setLayout(grid);
This.setLayout(layout);
Gbc.fill = GridBagConstraints.HORIZONTAL;
Gbc.gridx = 0;
Gbc.gridy = 0;
Gbc.gridx = 1;
Gbc.gridy = 0;
Gbc.fill = GridBagConstraints.HORIZONTAL;
Gbc.ipady = 20;
Gbc.gridx = 0;
Gbc.gridy = 1;
Gbc.gridx = 1;
Gbc.gridy = 1;
This.add(new Button(“Button Four”), gbc);
Gbc.gridx = 0;
Gbc.gridy = 2;
Gbc.fill = GridBagConstraints.HORIZONTAL;
Gbc.gridwidth = 2;
setSize(300, 300);
setPreferredSize(getSize()); setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Output:-
2.
Import java.awt.Button;
Import java.awt.GridBagConstraints;
Import java.awt.GridBagLayout;
Import javax.swing.*;
Public PR4b() {
GridBagLayout grid = new GridBagLayout();
Example”);
SP.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
This.setLayout(layout);
Gbc.fill = GridBagConstraints.HORIZONTAL;
Gbc.gridx = 0;
Gbc.gridy = 0;
This.add(L1, gbc);
Gbc.gridx = 1;
Gbc.gridy = 0;
This.add(T1, gbc);
Gbc.fill = GridBagConstraints.HORIZONTAL;
Gbc.gridx = 0;
Gbc.gridy = 1;
This.add(L2, gbc);
Gbc.gridx = 1;
Gbc.gridy = 1;
This.add(T2, gbc);
This.getContentPane().add(SP);
Gbc.gridx = 0;
Gbc.gridy = 2;
Gbc.fill = GridBagConstraints.HORIZONTAL;
Gbc.gridwidth = 1;
This.add(new Button(“Submit”), gbc);
setSize(300, 300);
setPreferredSize(getSize()); setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Output:-
X. Program Code.
Import java.awt.*;
MenuEx()
setMenuBar(mr);
Mn3.setEnabled(false);
M1.add(mn1);
M1.add(mn2);
M1.add(mn3);
M1.add(mn4);
M1.add(mn5);
Mr.add(m1);
Class MenuBarEx
m.setTitle(“Menu Bar”);
m.setSize(500,500);
m.setVisible(true);
}
Xl. Result:-
CODE :- import java.awt.*; import
MenuBar mb;
MenuItem m1,m2,m3,m4;
Menu mn;
MenuShortcut ms;
MenuDemo1()
ms=new MenuShortcut(KeyEvent.VK_X);
MenuItem("New..."); m2=new
MenuItem("Open..."); m3=new
MenuItem("Exit",ms); mn.add(m1);
mn.add(m2); mn.add(m3);
mn.addSeparator(); mn.add(m4);
mb.add(mn);
md.setTitle(“MenuBar Demo”);
md.setSize(500,500); md.setVisible(true);
Output:-
Code And Output :-
1.
2.
Output:-
import javax.swing.*; import
javax.swing.tree.*; import
java.awt.*;
maharashtraNode.add(mumbaiSubNode);
maharashtraNode.add(puneSubNode);
maharashtraNode.add(nashikSubNode);
maharashtraNode.add(nagpurSubNode); JTree tree = new
JTree(rootNode);
JFrameMain.add(tree);
}
}
import javax.swing.*;
import
javax.swing.tree.*;
import java.awt.*; public
class Tree
{
Tree()
{
JFrame f = new JFrame();
DefaultMutableTreeNode s=new DefaultMutableTreeNode("Root");
DefaultMutableTreeNode s2=new DefaultMutableTreeNode("This PC");
DefaultMutableTreeNode s3=new DefaultMutableTreeNode("Desktop");
s.add(s2);
s.add(s3);
DefaultMutableTreeNode s4=new DefaultMutableTreeNode("Documents");
DefaultMutableTreeNode s5=new DefaultMutableTreeNode("Downloads");
DefaultMutableTreeNode s6=new DefaultMutableTreeNode("Music");
DefaultMutableTreeNode s7=new
DefaultMutableTreeNode("Pictures"); s2.add(s4); s2.add(s5);
s2.add(s6); s2.add(s7); JTree j=new JTree(s); f.add(j);
f.setSize(200,200);
f.setVisible(true); }
public static void main(String[] args)
{ new
Tree();
}
}
Output :-
Code:-
import javax.swing.*;
import java.awt.*; import
javax.swing.table.*;
Object data[][] = {
{101,"Amit",670000},
{102,"Jai",780000},
{101,"Sachin",700000}
};
JTable JTableObj = new JTable(data,colHeads);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(JTableObj,v,h);
JFrameMain.add(jsp,BorderLayout.CENTER);
}
}
Output :-
Code :-
import javax.swing.*;
import java.awt.*; import
javax.swing.table.*;
Object data[][] = {
{"Amit","96%","C"},
{"jai","83%","A"},
{"Sachin","69%","B"},
{"Karan","73%","B"},
{"Amit","58%","A"},
{"Aadi","67%","A"},
{"Aman","81%","C"},
{"Nur","69%","C"},
{"Kal","93%","D"},
{"Rud","52%","D"}
};
JTable JTableObj = new JTable(data,colHeads);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(JTableObj,v,h);
JFrameMain.add(jsp,BorderLayout.CENTER);
}
}
Output :-
Code :-
import javax.swing.*;
public class GenerateJProgressBar extends JFrame
{
JProgressBar jb;
int i=0,num=0;
GenerateJProgressBar()
{
jb=new JProgressBar(0,2000);
jb.setBounds(40,40,250,30);
jb.setValue(0);
jb.setStringPainted(true);
add(jb);
setSize(500,600);
setLayout(null);
}
public void iterate()
{
while(i<=2000) {
jb.setValue(i);
i=i+50; try{
Thread.sleep(200);
}
catch(Exception e)
{
}
} }
public static void main(String[] args)
{
GenerateJProgressBar m=new GenerateJProgressBar();
m.setVisible(true);
m.iterate();
}
}
Output :-
Code :-
import javax.swing.*;
public class JProgressBarApplet extends JFrame
{
JProgressBar jb;
JButton JB; int
i=0,num=0;
JProgressBarApplet()
{
JB=new JButton("Click");
jb=new JProgressBar(0,2000);
JB.setBounds(150,150,150,25);
jb.setBounds(100,200,250,25);
jb.setValue(0);
jb.setStringPainted(true);
add(jb); add(JB);
setSize(500,500);
setLayout(null);
}
public void iterate()
{
while(i<=2000) {
jb.setValue(i);
i=i+50;
try{
Thread.sleep(200);
}
catch(Exception e)
{
}
} }
public static void main(String[] args)
{
JProgressBarApplet m=new JProgressBarApplet();
m.setVisible(true);
m.iterate();
}
}
Output :-
Input:
import java.awt.event.*;
import java.awt.*;
Output:
import java.awt.*;
import java.awt.event.*;
class Exp10X_2 extends Frame implements KeyListener{
Label l1,l2;
TextField tf1,tf2;
public Exp10X_2(String s){
super(s);
setVisible(true);
setLayout(null);
setSize(500,500);
tf1=new TextField();
tf2=new TextField();
tf1.setBounds(160,100,150,40);
tf2.setBounds(160,150,150,40);
tf1.addKeyListener(this);
tf2.addKeyListener(this);
add(l1);
add(l2);
add(tf1);
add(tf2);
}
public void keyPressed(KeyEvent e){
int keyNumber=e.getKeyCode();
switch(keyNumber){
case KeyEvent.VK_F1:
tf1.setText(keyNumber+"");
tf2.setText("Functional Key F1");
break;
case KeyEvent.VK_F2:
tf1.setText(keyNumber+"");
tf2.setText("Functional Key F2");
break;
case KeyEvent.VK_F3:
tf1.setText(keyNumber+"");
tf2.setText("Functional Key F3");
break;
case KeyEvent.VK_F4:
tf1.setText(keyNumber+"");
tf2.setText("Functional Key F4");
break;
case KeyEvent.VK_F5:
tf1.setText(keyNumber+"");
tf2.setText("Functional Key F5");
break;
case KeyEvent.VK_F6:
tf1.setText(keyNumber+"");
tf2.setText("Functional Key F6");
break;
case KeyEvent.VK_F7:
tf1.setText(keyNumber+"");
tf2.setText("Functional Key F7");
break;
case KeyEvent.VK_F8:
tf1.setText(keyNumber+"");
tf2.setText("Functional Key F8");
break;
case KeyEvent.VK_F9:
tf1.setText(keyNumber+"");
tf2.setText("Functional Key F9");
break; case KeyEvent.VK_F10:
tf1.setText(keyNumber+"");
tf2.setText("Functional Key F10");
break;
case KeyEvent.VK_F11:
tf1.setText(keyNumber+"");
tf2.setText("Functional Key F11");
break;
case KeyEvent.VK_F12:
tf1.setText(keyNumber+"");
tf2.setText("Functional Key F12");
break;
case KeyEvent.VK_UP:
tf1.setText(keyNumber+"");
tf2.setText("UP ARROW");
break;
case KeyEvent.VK_DOWN:
tf1.setText(keyNumber+"");
tf2.setText("DOWN ARROW");
break;
case KeyEvent.VK_RIGHT:
tf1.setText(keyNumber+"");
tf2.setText("RIGHT ARROW");
break;
case KeyEvent.VK_LEFT:
tf1.setText(keyNumber+"");
tf2.setText("LEFT ARROW");
break;
}
}
public void keyTyped(KeyEvent e){
}
public void keyReleased(KeyEvent e){
}
public static void main(String[] args){
}
}
Output:
Input:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Exp10XIII_3 extends JFrame implements ActionListener {
JLabel l1,l2,l3;
JTextField t1,t2;
JButton b1;
public Exp10XIII_3()
{
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("Multiply");
b1.addActionListener(this);
add(l1);
add(t1);
add(l2);
add(t2);
add(b1);
add(l3);
setLayout(new FlowLayout());
setSize(500,500);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{ int a,b,c;
a=Integer.parseInt(t1.getText());
b=Integer.parseInt(t2.getText());
c=a*b;
l3.setText("Multiplication of the two number is:-
"+Integer.toString(c));
}
public static void main(String[] args)
{
Exp10XIII_3 m=new Exp10XIII_3();
}
}
Output:
Line Missing:
addMouseListener(this);
Correct Code:
import java.applet.Applet;
import java.awt.*; import
java.awt.event.*;
/* <APPLET CODE ="MouseDemo.class" WIDTH=300 HEIGHT=200>
</APPLET> */
Output:
Input:
import java.awt.*; import java.awt.event.*;
import javax.swing.*;
Exp11XIII_2()
{
res.setHorizontalAlignment(JLabel.CENTER);
add(res, BorderLayout.CENTER);
addMouseListener(this);
setSize(400,400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
Input:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Exp11XIII_3 extends JFrame implements MouseMotionListener
{
Exp11XIII_3()
{
addMouseMotionListener(this);
setSize(500,500);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void mouseDragged(MouseEvent e)
{
Graphics g=getGraphics();
g.setColor(Color.YELLOW);
g.fillOval(e.getX(),e.getY(),20,20);
}
public void mouseMoved(MouseEvent e)
{
Graphics g1 = getGraphics();
g1.setColor(Color.RED);
g1.fillOval(e.getX(),e.getY(),20,20);
}
Output:
import javax.swing.*;
import java.awt.*;
public class JPasswordChange
{
public static void main(String[] args) {
JFrame f = new JFrame();
f.setVisible(true);
f.setSize(400,400);
f.setLayout(new FlowLayout());
JPasswordField pf = new JPasswordField(20);
pf.setEchoChar('#');
f.add(pf);
}
}
Output :-
1.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class text extends JFrame implements ActionListener {
static JTextField t;
static JPasswordField pass;
static JFrame f;
static JButton b;
static JLabel l;
text()
{
}
public static void main(String[] args)
{
f = new JFrame("textfield");
l = new JLabel("nothing entered");
b = new JButton("submit");
text te = new text();
b.addActionListener(te);
t = new JTextField("enter name", 16);
pass = new JPasswordField(16);
Font fo = new Font("Serif", Font.ITALIC, 20);
t.setFont(fo);
JPanel p = new JPanel();
p.add(t);
p.add(pass);
p.add(b);
p.add(l);
f.add(p);
f.setSize(300, 300);
f.show();
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("submit")) {
l.setText("name = " + t.getText() + "\t Password = " +
pass.getText());
t.setText(" ");
pass.setText("");
}
}
}
Output :-
2.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
Output :-
3.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Exp12XIII_3 extends JFrame implements ActionListener
{
JPasswordField t2;
JTextField t1;
JLabel l1,l2,l3;
JButton b;
String msg;
Exp12XIII_3()
{
setSize(350,350);
setVisible(true);
setLayout(new
FlowLayout(FlowLayout.CENTER,20,20));
l1=new JLabel("Enter Username : ");
t1=new JTextField(20);
l2=new JLabel("Enter Password : ");
t2=new JPasswordField(20);
t2.setEchoChar('*');
b=new JButton("SUBMIT");
b.addActionListener(this);
l3=new JLabel("");
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(b);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{ String s=new String(t2.getPassword());
if(s.length()<6)
{
l3.setText("Password length is less than 6 characters");
t2.setText("");
}else
{
l3.setText("User = "+t1.getText()+" Accepted. WELCOME !!!");
}
}
public static void main(String[] args)
{
Exp12XIII_3 t=new Exp12XIII_3();
}
}
Output :-
Code:-
import java.awt.*;
import java.awt.event.*;
public class WindowDemo
{
Frame f;
WindowDemo()
{
f=new Frame("Window Adapter");
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
f.dispose();
}
});
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args)
{
new WindowDemo();
}
}
Output:-
1.
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.WindowListener;
import java.awt.FlowLayout;
public class WindowAdapterDemo extends WindowAdapter
{
JFrame f ;
JLabel l ;
WindowAdapterDemo()
{
f = new JFrame();
f.setVisible(true);
f.setSize(400,400);
f.setLayout(new FlowLayout());
f.addWindowListener(this);
f.addWindowFocusListener(this);
}
public void windowLostFocus(WindowEvent we)
{
l = new JLabel("Window Lost Focus");
f.remove(l);
f.add(l);
}
public void windowOpened(java.awt.event.WindowEvent we)
{
l = new JLabel("Window Opened");
f.remove(l);
f.add(l);
}
public void windowActivated(java.awt.event.WindowEvent we)
{
l = new JLabel("Window Activated");
f.remove(l);
f.add(l);
}
public void windowDeactivated(java.awt.event.WindowEvent we)
{
l = new JLabel("Window Deactivated");
f.remove(l);
f.add(l);
}
public void windowGainedFocus(java.awt.event.WindowEvent we)
{
l = new JLabel("Window Gained Focus");
f.remove(l);
f.add(l);
}
public static void main(String[] args)
{
WindowAdapterDemo wa = new WindowAdapterDemo();
}
}
Output :-
2.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class AnonymousInner extends JFrame
{
public static void main(String[] args) {
AnonymousInner ai = new AnonymousInner();
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
System.out.println("Action Event:"+ae);
}
});
ai.add(jb);
ai.pack();
ai.setSize(300,350);
ai.setVisible(true);
}
}
Output :-
3.
Code :-
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);}
}
}
Output :-
Code :-
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Scanner;
public class RetriveIP
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Host Name: ");
String host = sc.nextLine();
try
{
InetAddress ip = InetAddress.getByName(host);
Output :-
Code :-
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());
Output :-
1.
import java.net.*;
class URLRetrive
{
public static void main(String args[]) throws MalformedURLException
{
URL hp = new URL("http://www.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());
System.out.println("Ext:" + hp.toExternalForm());
}
}
Output :-
2.
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Date;
}
}
Output :-
Code:-
//ValidateServer.java import
java.net.ServerSocket; import
java.net.Socket; import
java.io.BufferedReader; import
java.io.IOException; import
java.io.OutputStream; import
java.io.PrintStream; import
java.io.InputStreamReader;
//ValidateClient.java
import java.net.Socket; import
java.io.BufferedReader; import
java.io.IOException; import
java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
Output :-
1. Code :-
//ServerSide.java import
java.net.ServerSocket; import
java.net.Socket; import
java.io.BufferedReader; import
java.io.IOException; import
java.io.OutputStream; import
java.io.PrintStream; import
java.io.InputStreamReader;
//ClientSide.java import
java.net.Socket; import
java.io.BufferedReader; import
java.io.IOException; import
java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
public class ClientSide
{
public static void main(String[] args) throws IOException {
Socket s = new Socket("localhost",0605);
System.out.println("Client Started, waiting for server response..");
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in)
);
OutputStream os = s.getOutputStream();
BufferedReader br1 = new BufferedReader( new
InputStreamReader(s.getInputStream())
);
PrintStream ps = new PrintStream(os);
do{
System.out.print("Client: ");
String msg = br.readLine();
ps.println(msg);
String res = br1.readLine();
System.out.println("Server Send:"+res+"\n\n");
}
while(true);
}
}
Output:-
2. Code :-
//MyServer.java import
java.io. * ; import
java.net. * ;
Output :-
Code :-
//DgramRec.java
import java.net.*;
//DGramSender.java
import java.net.*;
Code :-
//ServerSideData.java
import java.net.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
do
{
DatagramPacket receiveDP = new
DatagramPacket(receiveData,receiveData.length);
ds.receive(receiveDP);
String clientMessage = new
String(receiveDP.getData(),0,receiveDP.getLength());
System.out.println("Client Message:"+clientMessage);
InetAddress ip = receiveDP.getAddress();
System.out.print("\n\nEnter Server Message:");
String serverMessage = br.readLine();
sendData = serverMessage.getBytes();
DatagramPacket sendDP = new DatagramPacket(sendData,
sendData.length, ip, receiveDP.getPort());
ds.send(sendDP);
receiveData = new byte[512];
}while(true);
}
}
//ClientSideData.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
public class ClientSideData
{
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket();
byte[] receiveData = new byte[512];
byte[] sendData = new byte[512];
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in)
);
System.out.println(" UDP Client Socket is created, waiting for
server ");
InetAddress ip = InetAddress.getLocalHost();
do
{
System.out.print("\nEnter Client Message:");
String clientMessage = br.readLine();
sendData = clientMessage.getBytes();
DatagramPacket sendDP = new DatagramPacket(sendData,
sendData.length, ip, 2019);
ds.send(sendDP);
DatagramPacket receiveDP = new
DatagramPacket(receiveData,receiveData.length);
ds.receive(receiveDP);
String serverMessage = new
String(receiveDP.getData(),0,receiveDP.getLength());
System.out.println("\n\nServer Message:"+serverMessage);
}while(true);
}}
Output :-
2.
Code :-
//ServerFile.java
import java.net.*;
import java.io.*;
public class ServerFile
{
public static void main(String args[])throws IOException
{
byte b[]=new byte[3072];
DatagramSocket dsoc=new DatagramSocket(2019);
FileOutputStream f=new
FileOutputStream("D:\\Hasnain.Java17\\EXPERIMENTS\\17\\ServerFile.txt")
;
}
}
//ClientFile.java
import java.net.*;
import java.io.*;
Output :-
3.
Code :-
import java.sql*;
class JdbcDemo
try
DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
ResultSet rs=st.executeQuery(str);
while(rs.next())
text= text+rs.getInt(1)+"\t"+rs.getString(2)+"\n";
System.out.print(text);
St.close();
cn.close();
catch (SQLExccpt.ion s)
{System.out.println("sql error");}
Output :-
1.
Code :-
import java.sql.*;
import java.net.InetAddress;
import java.util.*;
import java.util.Date;
// Database credentials
try{
Class.forName("com.mysql.jdbc.Driver");
stmt = conn.createStatement();
String sql = "CREATE TABLE EMPLOYEE " +
stmt.executeUpdate(sql);
(InetAddress.getLocalHost()).getHostAddress());
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
} }}}
Output:-
2.
Code:
import java.sql.*;
import java.net.InetAddress;
import java.util.*;
import java.util.Date;
// Database credentials
try{
Class.forName("com.mysql.jdbc.Driver");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
rs = stmt.executeQuery(sql);
while(rs.next()){
//Display values
System.out.println(" ");
rs.close();
System.out.println("\n");
(InetAddress.getLocalHost()).getHostAddress());
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}}}}
Output:-
Code:-
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Scanner;
try {
Class.forName("oracle.jdbc.OracleDriver");
connection = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:XE", "System",
"Atharva007");
psmt = connection.prepareStatement(SqlUtils.DYNC_UPDATE_QUERY);
System.out.println("Enter Address:");
System.out.println("Enter Name:");
psmt.setString(2, name);
psmt.setString(1, address);
catch(SQLException e){
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (psmt != null)
psmt.close();
if (connection != null)
connection.close();
} catch (Exception e) {
e.printStackTrace();
}}
Output:-
1. Code:
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection(url,uname,pass); Statement
st = con.createStatement(); st.executeUpdate(query);
st.close(); con.close();
Output :
2. Code:
java.sql.DriverManager; import
java.sql.PreparedStatement; public
class UpdateQuery
{ try
Class.forName("com.mysql.jdbc.Driver"); Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:33
06/Ddemodatabase","root","root");
PreparedStatement st = con
name='Abhishek'"); st.executeUpdate();
catch(Exception ex)
System.out.println(ex);
}
Output :
1. Code:
String query = "update student set Name = 'John' where Roll_No = 1";
Class.forName("com.mysql.jdbc.Driver");
st.close(); con.close();
Output:
2. Code :
String query = "delete from sales where P_Price > 500 AND P_Id =
'P1234'";
Class.forName("com.mysql.jdbc.Driver");
st.close();
con.close();
Output:-
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
response.setContentType("text/html");
out.println("<html>");
out.println("<head>");
out.println("<title>Hello MSBTE!</title>");
out.println("</head>");
out.println("<body>");
out.println("</body>");
out.println("</html>");
}
Output:-
1.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.InetAddress;
response.setContentType("text/html");
InetAddress IP = InetAddress.getLocalHost();
out.println("<html>");
out.println("<head>");
out.println("<title>Hello MSBTE!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello MSBTE!</h1>");
out.println("</body>");
out.println("</html>");
Output:-
2.
Form1.html
<html>
<body>
<center>
<form name="Form1"
method="post"action="http://localhost:8080/examples/servlets/servlet/PostParamet
ersServlet">
<table>
<tr><td><B>Employee</td>
<tr><td><B>Phone</td>
</table>
</body>
</html>
PostParametersServlet.java
import java.io.*;
import java.util.*;
import javax.servlet.*;
Enumeration e = request.getParameterNames();
while(e.hasMoreElements()){
pw.println(pvalue);
pw.close();
}}
Output:-
1.
HTML CODE:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<select name="subject">
<option value="AJP">AJP</option>
<option value="STE">STE</option>
<option value="OSY">OSY</option>
<option value="CSS">CSS</option>
</select>
</form>
</body>
</html>
JAVA CODE:
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.util.*;
ServletException,IOException{
resp.setContentType("text/html");
PrintWriter out=resp.getWriter();
String gen=req.getParameter("sex");
String sub=req.getParameter("subject");
out.println("<br>");
XML CODE:
<servlet>
<servlet-name>ServletCode</servlet-name>
<servlet-class>ServletCode</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletCode</servlet-name>
<url-pattern>/servlets/servlet/ServletCode</url-pattern>
</servlet-mapping>
Output:-
2.
HTML CODE:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form action="http://localhost:8080/examples/servlets/servlet//ServletCode2"
method="post">
</form>
</body>
</html>
JAVA CODE:
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.util.*;
ServletException,IOException{
resp.setContentType("text/html");
PrintWriter out=resp.getWriter();
int s1=Integer.parseInt(req.getParameter("sub1"));
int s2=Integer.parseInt(req.getParameter("sub2"));
int s3=Integer.parseInt(req.getParameter("sub3"));
int s4=Integer.parseInt(req.getParameter("sub4"));
float sum=(s1+s2+s3+s4);
out.println("<h2>Total is "+sum+"</h2>");
out.println("<br>");
}else {
XML CODE:
<servlet>
<servlet-name>ServletCode2</servlet-name>
<servlet-class>ServletCode2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletCode2</servlet-name>
<url-pattern>/servlets/servlet/ServletCode2</url-pattern>
</servlet-mapping>
Output:-
1.
HTML CODE:
<html>
<body>
<form action="http://localhost:8080/examples/servlets/servlet//SessionServlet">
</form>
</body>
</html>
JAVA CODE:
//SessionServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
try{
response.setContentType("text/html");
String n=request.getParameter("userName");
out.print("Welcome "+n);
HttpSession session=request.getSession();
out.print("<br>Session Id:"+session.getId());
session.setAttribute("uname",n);
out.print("<a href='servlet2'>visit</a>");
out.close();
XML CODE:
<servlet>
<servlet-name>SessionServlet</servlet-name>
<servlet-class>SessionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SessionServlet</servlet-name>
<url-pattern>/servlets/servlet/SessionServlet</url-pattern>
</servlet-mapping>
Output:-
2.
HTML CODE:
<html>
<body>
<form action="
http://localhost:8080/examples/servlets/servlet/SessionServlet1">
</form>
</body>
</html>
JAVA CODE:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.InetAddress;
try{
response.setContentType("text/html");
String n = request.getParameter("userName");
session.setMaxInactiveInterval(300);
session.getLastAccessedTime());
session.setAttribute("uname", n);
out.println();
InetAddress IP = InetAddress.getLocalHost();
catch (Exception e) {
System.out.println(e);
XML CODE:
<servlet>
<servlet-name>SessionServlet1</servlet-name>
<servlet-class>SessionServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SessionServlet1</servlet-name>
<url-pattern>/servlets/servlet/SessionServlet1</url-pattern>
</servlet-mapping>
Output :-
Code:
HTML CODE:
<!DOCTYPE html><html>
<body>
<h3>Cookies Example</h3>
<input type=submit></form>
</body>
</html>
JAVA CODE:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
Cookie c = cookies[i];
response.addCookie(c);
}}}
XML CODE:
<servlet>
<servlet-name>CookieExample</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>CookieExample</servlet-name>
</servlet-mapping>
Output:-
Code:-
HTML CODE:
<html>
<title>Cookie Program</title>
<body>
</form>
</body></html>
JAVA CODE:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
try{
response.setContentType("text/html");
String n=request.getParameter("userName");
out.print("Welcome "+n);
out.print("<form action='SecondServlet'>");
out.print("</form>");
out.close();
}catch(Exception e){System.out.println(e);}
//SecondServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
try{
response.setContentType("text/html");
Cookie ck[]=request.getCookies();
out.print("Hello "+ck[0].getValue());
out.close();
}catch(Exception e){System.out.println(e);}
}}
XML CODE:
<servlet>
<servlet-name>FirstServlet</servlet-name>
</servlet>
<servlet>
<servlet-name>SecondServlet</servlet-name>
</servlet>
<servlet-mapping>
</servlet-mapping>
<servlet-mapping>
<servlet-name> SecondServlet </servlet-name>
</servlet-mapping>
Output:-