You are on page 1of 24

1.

Design an applet to create form using Text Field, Text Area,


Button and Label.
import java.awt.*;
import java.applet.*;
/*<applet code=BasicAWT.class width=300 height=300>
</applet> */
public class BasicAWT
{
 public static void main(String args[])
 {
  Frame f = new Frame();
  f.setSize(400,400);
  f.setVisible(true);
  f.setLayout(new FlowLayout() );
 
  Label l1 = new Label();
  l1.setText("Enter Your Name ");
   
  TextField tf = new TextField("Atharva");
 
  Label l2 = new Label("Address");
  TextArea ta = new TextArea("",3,40);
  
  Button b = new Button("Submit");
   
  f.add(l1); f.add(tf); f.add(l2); f.add(ta); f.add(b);
 }
}

2. Develop an application using List components to add names of 10


different cities.
import java.awt.*;
import java.applet.*;
 
public class ListDemo extends Applet
{
    public void init()
    {
        List c = new List();
        c.setMultipleSelections(true);
        c.add("Thane");
        c.add("Mumbai");
        c.add("Nagpur");

1
        c.add("Navi Mumbai");  
  c.add("Kolhapur");  
        c.add("Ahemdabad");  
        c.add("Latur");  
        c.add("Kalyan");  
        c.add(“Dombivali");  
        c.add("Dadar");  

        add(c);
    }
}

3. Develop a program to display a grid of 5 x 5..


import java.awt.*;
class Exp_3_1 extends Frame
{
Exp_3_1()
{
for(Integer i=1;i<=25;i++)
add(new Button(i.toString()));
setTitle("GridLayout in Java Example");
setSize(400,150);
setVisible(true);
setLocation(200,200);
setLayout(newGridLayout(5,5));
}
public static void main(String args[])
{
Exp_3_1 g = new Exp_3_1();
}
}

4. Develop a program to give following output

import java.awt.Button;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Frame;
public class Griddemo extends Frame
{
public static void main(String args[])

2
{
Griddemo fr=new Griddemo();
fr.setTitle("gridbag demo");
fr.setSize(300,300);
fr.setVisible(true);
}
public Griddemo()
{
GridBagLayout grid=new GridBagLayout();
GridBagConstraints gbc =new GridBagConstraints();
setLayout(grid);
GridBagLayout layout = new GridBagLayout();
this.setLayout(layout);
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.gridx=0;
gbc.gridy=0;
this.add(new Button("Button One"),gbc);
gbc.gridx=1;
gbc.gridy=0;
this.add(new Button("Button Two"),gbc);
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.ipady=20;
gbc.gridx=0;
gbc.gridy=1;
this.add(new Button("Button Three"),gbc);
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;
this.add(new Button("Button Five"),gbc);
}
}

5. Develop program to give following output for CardLayout.

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

3
import javax.swing.JFrame;
import javax.swing.*;
   
public class CardLayoutDemo extends JFrame implements ActionListener {
   
    CardLayout card;
    JButton b1, b2, b3;  
    Container c;
   
    CardLayoutDemo()
    {
        c = getContentPane();
        card = new CardLayout(40, 30);
        c.setLayout(card);
   
        b1 = new JButton("First Level");
        b2 = new JButton("Second Level");
   
        b1.addActionListener(this);
        b2.addActionListener(this);
   
        c.add("a", b1);
        c.add("b", b2);
    }
       
    public void actionPerformed(ActionEvent e)
    {
        card.next(c);
    }
 
    public static void main(String[] args)
    {
        CardLayoutDemo cl = new CardLayoutDemo();
   
        cl.setSize(400, 400);
        cl.setVisible(true);
        cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

6. Develop a program to create checkable menu item ‘Picture’ under


‘Insert’ menu and ‘Paste’ menu item under menu ‘Home’
import java.awt.*;
class MenuExample
{ MenuExample()
{ Frame f= new Frame("Menu and MenuItem Example");

4
MenuBar mb=new MenuBar();
Menu menu1=new Menu("Home");
Menu menu2=new Menu("Insert");
CheckboxMenuItem c=new CheckboxMenuItem("picture",true);
MenuItem m=new MenuItem("paste");
menu1.add(c); menu2.add(m); mb.add(menu1); mb.add(menu2);
f.setMenuBar(mb);
f.setSize(200,200);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{ new MenuExample(); }
}
7. Develop a program to create three menus such as PageLayout,
References and Mailing. Disable the Mailing menu.
import java.awt.*;
class MenuExample1
{
MenuExample1()
{
Frame f= new Frame("Menu and MenuItem Example");
MenuBar mb=new MenuBar();
Menu menu1=new Menu("Page Layout");
Menu menu2=new Menu("References ");
Menu menu3=new Menu("mailing ");
menu3.setEnabled(false);
mb.add(menu1);
mb.add(menu2);
mb.add(menu3);
f.setMenuBar(mb);
f.setSize(200,200);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{ new MenuExample1(); }
}

8. Develop a frame to select different states in India using


JCombobox
import javax.swing.*;
import java.awt.*;
 
public class DifferentState
{

5
    public static void main(String args[])
    {
        JFrame JFrameMain = new JFrame();
         
        JFrameMain.setVisible(true);
        JFrameMain.setSize(400,400);
        JFrameMain.setLayout(new GridLayout(3,3));
        String states[] = {"Solapur","Pune","Mumbai","Banglore"};
        JComboBox JComboBoxStates = new JComboBox(states);
 
        JFrameMain.add(JComboBoxStates);
    }
 
}
9. Develop a JTree program to shoe root directory and int
subfolders of your system.
import javax.swing.*;
import java.awt.*;
import javax.swing.tree.DefaultMutableTreeNode;
/**/ public class TreeNodeEx extends JApplet
{ JTree tree;
Container c= getContentPane();
public void init()
{ c.setLayout(new BorderLayout());
DefaultMutableTreeNode top = new DefaultMutableTreeNode ("Options");
DefaultMutableTreeNode a1 = new DefaultMutableTreeNode ("A1");
DefaultMutableTreeNode b1 = new DefaultMutableTreeNode ("B1");
DefaultMutableTreeNode a= new DefaultMutableTreeNode ("a");
DefaultMutableTreeNode b= new DefaultMutableTreeNode ("b");
a1.add(a); b1.add(b); top.add(a1); top.add(b1);
tree=new JTree(top);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp=new JScrollPane(tree,v,h);
c.add(jsp,BorderLayout.CENTER);
}
}
10. Develop a program to create a table of ‘Name’, ‘Percentage’ and
‘Grade’ for 10 students using JTable
// Program to create a table of Name of Student, Percentage and Grade of 10Student
import java.awt.*;
import javax.swing.*; 
 
public class JTableStudents  extends JApplet
{
    public void init()
    {
        setVisible(true);

6
        setSize(400,400);
        //setLayout( new BorderLayout() );
         
        String collumnHeading[] = {"Name","Percentage","Grade"};
         
        Object data[][]={
            {"A1",98,"A"},
            {"A2",90,"C"},
            {"A3",88,"A"},
            {"A4",99,"A"},
            {"A5",59,"A"},
            {"A6",94,"D"},
            {"A7",92,"A"},
            {"A8",42,"C"},
            {"A9",85,"A"},
            {"A10",98,"B"}
        };
         
        JTable JTableObj = new JTable(data,collumnHeading);
 
        int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
        int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
        JScrollPane jsp = new JScrollPane(JTableObj,v,h);
 
        add(jsp,BorderLayout.CENTER);
    }
}
 
/*
    <applet code="JTableStudents" height="400" width="400">
    </applet>
*/
11. Develop a program using JProgressBar to show the
progress of ProgressBar when user clicks on JButton
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
public class JProgressBarApplet extends JApplet implements ActionListener
{
    JProgressBar JProgressBarObj;
    JButton JButtonObj;
    int i=0;
 
    public void init()

7
    {
        setSize(400,400);
        setVisible(true);
        setLayout(new FlowLayout());
 
        JButtonObj = new JButton("Click Me");
        JButtonObj.addActionListener(this);
 
        JProgressBarObj = new JProgressBar();
        JProgressBarObj.setStringPainted(true);
        JProgressBarObj.setValue(0);
 
        add(JButtonObj);
        add(JProgressBarObj);
    }
 
    public void actionPerformed(ActionEvent ie)
    {
        this.iterate();
    }
 
    public void iterate()
    {
        while(i<=2000)
        {
            JProgressBarObj.setValue(i);
            i=i+20;
            try
            {
                Thread.sleep(150);
            }
            catch(Exception e)
            {}
        }
    }
}
12. Develop a program for given output.

8
13. Develop program to generate KeyEvent when a key
is pressed and display ‘KeyPressed’ message.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class KeyEventDemo extends Applet implements KeyListener
{
    String msg = "";
 
    public void init()
    {
        addKeyListener(this);
    }
 
    public void keyReleased(KeyEvent k)
    {
        showStatus("Key Released");
        repaint();
    }
 
    public void keyTyped(KeyEvent k)
    {
        showStatus("Key Typed");
        repaint();
    }
 
    public void keyPressed(KeyEvent k)
    {
        showStatus("Key Pressed");
        repaint();
    }
 
    public void paint(Graphics g)

9
    {
        g.drawString(msg, 10, 10);
    }
}
/*
   <applet code="KeyEventDemo" height="400" width="400">
   </applet>
*/

14. Develop a program to accept two numbers and display product


of two numbers when pressed “Multiply” button.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/**/
public class Multiplyex extends Applet implements ActionListener
{
TextField t1,t2,t3;
Label l1,l2,l3;
Button b1;
public void init()
{
l1=new Label("Enter first no");
t1=new TextField();
l2=new Label("Enter second no");
t2=new TextField();
b1=new Button("MULTIPLY");
l3=new Label("Result");
t3=new TextField();
add(l1); add(t1); add(l2); add(t2); add(b1); add(l3); add(t3);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{ int i=Integer.parseInt(t1.getText());
int j=Integer.parseInt(t2.getText());
int a=i*j;
String s1=""+a;
t3.setText(s1);
}
}
15. Develop a program to change the background color of applet
when user performs events using mouse.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;  
public class MouseColor1 extends Applet implements MouseListener
{

10
  public void init()
   {
       addMouseMotionListener(this);
   }
   public void mousePressed(MouseEvent me)
   {
       setBackground(Color.red);
       repaint();
   }
  public void mouseReleased(MouseEvent me)
   {
       setBackground(Color.green);
     repaint();
   } 
 public void mouseClicked(MouseEvent me){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}

16. Develop a program to count number of clicks performed by user


in a Frame window
import java.awt.*;
import java.applet.*;
import java.awt.event.*;  
public class Mouse1 extends Frame implements MouseListener
{
  Label l;
Mouse1()
{
setLayout(null);
setSize(300,300);
setVisible(true);
l=new Label(“Count: “);
l.setBounds(50,150,200,100);
add(l);
addMouseListener(this);
}
   public void mousePressed(MouseEvent me)
   {
       l.setText(“mouse clicked # of clicks:”+e.getClickCount());
   }
  public void mouseReleased(MouseEvent me){ } 
 public void mouseClicked(MouseEvent me){}
public void mouseEntered(MouseEvent e) {}

11
public void mouseExited(MouseEvent e) {}
public static void main(String args[])
{ new Mouse1();
}
}

17. Develop a program to demonstrate the use of mouseDragged


and mouseMoved methods of MouseMotion Listener.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test {

public static void main(String[] args) {


new Test();
}

public Test()
{
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
System.out.println("Moved");
}

@Override
public void mouseDragged(MouseEvent e) {
System.out.println("Mouse Dragged");
}
});
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);

12
}

18. Develop a program using JPasswordField to set the password


character as ‘#’ instead of ‘*’
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);
    }
 
}

19. Develop a program to 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”

20. Develop a program to demonstrate the use of WindowAdapter


class.

13
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);
    }
 

14
    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();
    }
}

21. Develop a program using InetAddress class to retrieve IP


address of computer when hostname is entered by user.
import java.net.*;
import java.util.*;

public class IPDemo


{
public static void main(String[] args){
String host;
Scanner input = new Scanner(System.in);
System.out.print("\n Enter host name: ");
host = input.nextLine();
try {
InetAddress address = InetAddress.getByName(host);
System.out.println("IP address: " + address.getHostAddress());
System.out.println("Host name : " + address.getHostName());
System.out.println("Host name and IP address: " + address.toString());

}
catch (UnknownHostException ex) {
System.out.println("Could not find " + host);
}
}
}

22. Develop a program using URLClass to retrieve the


host, protocol, port an file of URL http://www.msbte.org.in

15
import java.io.*;
import java.net.*;
public class exp121
{ public static void main(String[] args)
{
try {
URL url=new URL("http://www.msbte.com");
System.out.println("Protocol: "+url.getProtocol());
System.out.println("Host Name: "+url.getHost());
System.out.println("Port Number: "+url.getPort());
System.out.println("File Name: "+url.getFile());
}catch(Exception e)
{System.out.println(e); }
}
}

23. Develop a program to check credentials of user


(Client will send user id and password to the server and server
will authenticate the client using equals())
//clientside
import java.net.*;
import java.io.*;
public class Client1
{
public static void main( String args[]) throws Exception
{ Socket cs = new Socket("localhost",1234);
BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br = new BufferedReader(new InputStreamReader(cs.getInputStream()));
DataOutputStream dos = new DataOutputStream(cs.getOutputStream());
System.out.println(" Enter password");
System.out.println(" if client 'quit' type exit");
System.out.println(" Enter password");
String s1,s4=null;
while(!(s1=kb.readLine()).equals("exit"))
{
System.out.println(" data send to server machine");
dos.writeBytes(s1+"\n");
} System.out.println("Terminated..");
cs.close();
dos.close();
kb.close();
}
}

16
//serverside
import java.net.*;
import java.io.*;
public class Server1
{
public static void main( String args[]) throws Exception
{
ServerSocket srs = new ServerSocket(1234);
System.out.println("Server is running...");
Socket ss=srs.accept();
System.out.println("connection establised");
BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br = new BufferedReader(new InputStreamReader(ss.getInputStream()));
DataOutputStream dos = new DataOutputStream(ss.getOutputStream());
while(true)
{
String s2,s1="password";
while((s2=br.readLine())!=null)
{
System.out.println("Client Entered Password: "+s2);
if(s2.equals(s1))
{
System.out.println("your password is correct");
}
else
{
System.out.println("your password is incorrect");
}
}
System.out.println("Terminated..");
ss.close();
srs.close();
dos.close();
kb.close();
System.exit(0);
}}}
24. Develop a program using DatagramPacket and
DatagramSocket to create chat application.
//ServerSideData.java:
import java.net.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class ServerSideData
{
    public static void main(String[] args) throws IOException {

17
        DatagramSocket ds = new DatagramSocket(2019);
 
        byte[] receiveData = new byte[512];
        byte[] sendData = new byte[512];
 
        BufferedReader br = new BufferedReader(
            new InputStreamReader(System.in)
        );
 
        System.out.println(" UDP Server Socket is created, waiting for client ");
 
        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)
        );
 

18
        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);
 
         
    }
}

25. Develop a program to name and rollno of students from


“studenttable” having parecentage>70

import java.sql.*;
class Select
{
public static void main(String ar[])
{
try{
String url="jdbc:odbc:abc";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("dr");
Connection c=DriverManager.getConnection(url);
System.out.println("cn");
Statement st=c.createStatement();
System.out.println("s");
ResultSet rs=st.executeQuery("Select name,rollno from student where per>70");
while(rs.next())
{

19
System.out.println("name is"+rs.getString(1));
System.out.println("rollno is"+rs.getInt(2));
//System.out.println("per is"+rs.getInt(3));
}
}
catch(Exception ee)
{System.out.println(ee);}
}
}

26. Develop a program to retrieve data using Resultset.

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


import java.sql.SQLException;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.Connection;
 
public class UpdateStaticOracle
{
 public static void main(String args[] ) throws java.sql.SQLException
 {
 oracle.jdbc.OracleDriver obj = new oracle.jdbc.OracleDriver();
 System.out.println("1 Driver loaded successfully");
 
 Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","System","pass");
 if( con != null)
  System.out.println("Connection established successfully");
 else
  System.out.println("Connection not established successfully");
  
 Statement st = con.createStatement();
 System.out.println("Statement established successful");
  
 String qry = "Update Student set Name='A' where id= 104";
 int count = st.executeUpdate(qry);
  
 System.out.println(count+"Statement Executed Successfully");
 
 st.close();

20
 con.close();
 }
}

28. Develop a program to update name of student from ‘Jack’ to


“John’
import java.sql.*;
import java.sql.Statement.*;
import java.io.*;
public class UpdateDb
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver Loaded");
String url="jdbc:odbc:TYCM";
Connection con=DriverManager.getConnection(url);
System.out.println("connected");
Statement s=con.createStatement();
Statement si = con.createStatement();
Statement st=con.createStatement();
Statement su=con.createStatement();
Statement su1=con.createStatement();
s.executeUpdate("create table record(Name varchar(20), ID integer)");
System.out.println("Table created");
si.executeUpdate("insert into record values('Sejal',1)");
si.executeUpdate("insert into record values('Jack',2)");
si.executeUpdate("insert into record values('Amar',3)");
System.out.println("Data inserted");
String s1;
s1="select * from record";
ResultSet rs=st.executeQuery(s1);
rs=st.getResultSet();
while(rs.next())
{
System.out.println(rs.getString("Name")+" "+rs.getInt("ID"));
}
su.executeUpdate("UPDATE record SET Name='John' WHERE Name='Jack'");
System.out.println("records updated");
con.close();
}
catch(Exception e)
{
System.out.println(e);
}

21
}
}

29. Develop a program to delete all records for a product whose


“price is greater than 500” and “Id is P1234”
import java.sql.*;
import java.sql.Statement.*;
import java.io.*;
public class DeleteDb
{
public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver Loaded");
String url="jdbc:odbc:TYCM";
Connection con=DriverManager.getConnection(url);
System.out.println("connected");
Statement s=con.createStatement();
Statement si = con.createStatement();
Statement st=con.createStatement();
Statement su=con.createStatement();
Statement su1=con.createStatement();
s.executeUpdate("create table product(PrName varchar(20), ID integer,Price integer)");
System.out.println("Table created");
si.executeUpdate("insert into product values('Wheat',121,300)");
si.executeUpdate("insert into product values('Rice',122,349)");
si.executeUpdate("insert into product values('Sugar',1234,500)");
System.out.println("Data inserted");
String s1;
s1="select * from product";
ResultSet rs=st.executeQuery(s1);
rs=st.getResultSet();
while(rs.next())
{
System.out.println(rs.getString("PrName")+" "+rs.getInt("ID")+" "+rs.getInt("Price"));
}
su.executeUpdate("DELETE from product WHERE price=500 and ID=1234");
System.out.println("records deleted");
con.close();
}
catch(Exception e)
{
System.out.println(e);
}

22
}
}

30. Develop a servlet to print “Hello MSBTE” in browser


window
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.PrintWriter;
import java.io.IOException;
 
public class HelloHttp extends HttpServlet
{
 public void service(HttpServletRequest request, HttpServletResponse response) throws
ServletException,IOException
 {
  System.out.println("Http Servlet Program under Execution");
   
  response.setContentType("text/html");
  PrintWriter pw = response.getWriter();
 
  pw.println("Website shifted to codingatharva.blogspot.com");
  pw.close();
 }
}

31. Develop a servlet to receive the parameter through


HTML forms and send back received parameter to browser

32. Develop a servlet to receive student subject marks through


HTML forms Text Field and send the response s passed or Failed
in Examination

23
33. Develop a servlet program to display various details about
session using HttpSession methods.

34. Develop a servlet to collect user information using cookies


import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class CookieEx extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws IOException, ServletException

response.setContentType(“text/html”);

PrintWriter out = response.getWriter();

out.println(“”);

out.println(“”);

out.println(“”);

out.println(“”);

out.println(“
“);
String name=request.getParameter(“name”);

String value=request.getParameter(“age”);

Cookie c=new Cookie(name,value);

response.addCookie(c);

String p1= c.getName();

String p2= c.getValue();

out.println(“Name : “+ p1);

out.println(“
Age : “+ p2);
out.println(“”);

24

You might also like