0% found this document useful (0 votes)
18 views57 pages

Advanced Java Swing Components Guide

The document contains a series of Java Swing programming examples demonstrating the use of various components such as JTextArea, JTextField, JButton, JCheckBox, JComboBox, JLabel, JList, JMenu, JPopupMenu, JRadioButton, JTabbedPane, and JTable. Each example includes code snippets that illustrate how to create and manipulate these components within a JFrame. The document serves as a practical guide for students learning advanced Java programming concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views57 pages

Advanced Java Swing Components Guide

The document contains a series of Java Swing programming examples demonstrating the use of various components such as JTextArea, JTextField, JButton, JCheckBox, JComboBox, JLabel, JList, JMenu, JPopupMenu, JRadioButton, JTabbedPane, and JTable. Each example includes code snippets that illustrate how to create and manipulate these components within a JFrame. The document serves as a practical guide for students learning advanced Java programming concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SYCS – Sem IV – Advanced Java Practical

1 Use of JTextArea()
import [Link].*;
public class JTextAreaExample
{
JTextAreaExample()
{
JFrame f= new JFrame();
JTextArea area=new JTextArea("Welcome to javatpoint");
[Link](10,30, 200,200);
[Link](area);
[Link](300,300);
[Link](null);
[Link](true);
}
public static void main(String args[])
{
new JTextAreaExample();
}
}

Prof. Jyoti Samel Page 1


SYCS – Sem IV – Advanced Java Practical

Prof. Jyoti Samel Page 2


SYCS – Sem IV – Advanced Java Practical

2 Swing program to understand use of JTextField(), JLabel(), JTextArea(), JButton()


components.
import [Link];
import [Link].*;
public class FirstSwingExample
{
public static void main(String[] args)
{
JFrame f=new JFrame();//creating instance of JFrame
JLabel nameLbl=new JLabel();
[Link]("Enter your name :");
[Link](10,100,120,30);
[Link](nameLbl);

JTextField jtf=new JTextField(20);


[Link](150,100,120,30);
[Link](jtf);

JLabel pwdLbl=new JLabel("Enter password :",[Link]);


[Link](10,150,120,30);
[Link](pwdLbl);

JPasswordField pwdJp=new JPasswordField();


[Link]('*');
[Link](150,150,120,30);
[Link](pwdJp);

JLabel addressLbl=new JLabel("Enter Address :",[Link]);


[Link](10,190,120,30);
[Link](addressLbl);

JTextArea addressjta=new JTextArea(3,40);


[Link](150,190,120,60);
[Link](addressjta);

JButton button1=new JButton("submit");//creating instance of JButton


[Link](KeyEvent.VK_S);
[Link](150,250,100, 40);//x axis, y axis, width, height
[Link](button1);//adding button in JFrame

JButton button2=new JButton("reset");//creating instance of JButton


[Link]("Reset Button");
[Link](150,300,100, 40);//x axis, y axis, width, height
[Link](button2);

[Link](400,500);//400 width and 500 height


[Link](null);//using no layout managers
[Link](true);//making the frame visible
}

Prof. Jyoti Samel Page 3


SYCS – Sem IV – Advanced Java Practical

Prof. Jyoti Samel Page 4


SYCS – Sem IV – Advanced Java Practical

3 Swing Program to Display use of JButton(). When pressed OK button it will display “You
pressed OK button” or when pressed cancel button display “You pressed Cancel Button”
message in the JLabel.
import [Link].*;
import [Link].*;
import [Link].*;
class JButtonDemo extends JFrame implements ActionListener
{ JButton ok, cancel;
JLabel jlbl;
JButtonDemo()
{ setLayout(new FlowLayout());
ok=new JButton("OK"); [Link](KeyEvent.VK_O);
cancel=new JButton("CANCEL"); [Link](KeyEvent.VK_C);
jlbl=new JLabel();
add(ok); add(cancel); add(jlbl);
[Link](this);
[Link](this);
setSize(500,500); setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{ String s=[Link]();
String msg="";
if([Link]("OK"))
msg="You pressed OK button";
else if([Link]("CANCEL"))
msg="You pressed Cancel Button";
[Link](msg);
}
public static void main(String args[])
{
new JButtonDemo();
}
}

Prof. Jyoti Samel Page 5


SYCS – Sem IV – Advanced Java Practical

Prof. Jyoti Samel Page 6


SYCS – Sem IV – Advanced Java Practical

4 Use of JCheckbox()
import [Link].*;
public class JCheckBoxEx
{
JCheckBoxEx(){
JFrame f= new JFrame("CheckBox Example");
JCheckBox checkBox1 = new JCheckBox("C++");
[Link](100,100, 50,50);
JCheckBox checkBox2 = new JCheckBox("Java", true);
[Link](100,150, 80,50);
[Link](checkBox1);
[Link](checkBox2);
[Link](400,400);
[Link](null);
[Link](true);
}
public static void main(String args[])
{
new JCheckBoxEx();
}}

Prof. Jyoti Samel Page 7


SYCS – Sem IV – Advanced Java Practical

5 Swing Program to display use of JCheckbox. Design stationary app


import [Link].*;
import [Link].*;
import [Link].*;
class JCheckBoxDemo extends JFrame implements ItemListener
{
JCheckBox ch1, ch2, ch3;
JLabel jl;
JTextField jtf;
JCheckBoxDemo()
{
setLayout(new FlowLayout());
ch1=new JCheckBox("Pen Rs 20");
ch2=new JCheckBox("Pencil Rs 10");
ch3=new JCheckBox("Scale Rs 50");
jl=new JLabel("Total Rupees :");
jtf=new JTextField(20);
add(ch1); add(ch2); add(ch3); add(jl); add(jtf);
[Link](this); [Link](this); [Link](this);
setSize(300,300);
setVisible(true);
}
public void itemStateChanged(ItemEvent e)
{
int pen=0 , pencil=0,scale=0, total=0;
if([Link]())
pen=20;
else
pen=0;
if([Link]())
pencil=10;
else
pencil=0;
if([Link]())
scale=50;
else
scale=0;

total=pen+pencil+scale;
[Link]("Cost Rs:"+total);
}
public static void main(String args[])
{
new JCheckBoxDemo();
}
}

Prof. Jyoti Samel Page 8


SYCS – Sem IV – Advanced Java Practical

Prof. Jyoti Samel Page 9


SYCS – Sem IV – Advanced Java Practical

6 Swing program to display JCombobox()


import [Link].*;
public class JComboBoxExample {
JFrame f;
JComboBoxExample(){
f=new JFrame("ComboBox Example");
String country[]={"India","Aus","U.S.A","England","Newzealand"};
JComboBox cb=new JComboBox(country);

[Link](true);
[Link]("Japan");
[Link]("India");
[Link](50, 50,90,20);
[Link](cb);
[Link](null);
[Link](400,500);
[Link](true);
}
public static void main(String[] args) {
new JComboBoxExample();
}
}

Prof. Jyoti Samel Page 10


SYCS – Sem IV – Advanced Java Practical

7 Use of JLabel() with setBackground and setForeground methods


import [Link];
import [Link].*;
class JLabelExample
{
public static void main(String args[])
{
JFrame f= new JFrame("Label Example");
JLabel l1,l2;
l1=new JLabel("First Label.");
[Link](50,50, 100,30);
[Link](true);
[Link]([Link]);
[Link]([Link]);
l2=new JLabel("Second Label.");
[Link](50,100, 100,30);
[Link](l1); [Link](l2);
[Link](300,300);
[Link](null);
[Link](true);
}
}

Prof. Jyoti Samel Page 11


SYCS – Sem IV – Advanced Java Practical

8 Use of JList()
import [Link].*;
class JListDemo
{
public static void main(String args[])
{
JFrame f = new JFrame("JList Example");
JPanel p=new JPanel();
String s[]={"Core Java","Advanced Java", "Enterprise Java", "Web Services"};
JList jL=new JList(s);
[Link](jL);
[Link](p);
[Link](300,300);
[Link](true);

}
}

Prof. Jyoti Samel Page 12


SYCS – Sem IV – Advanced Java Practical

9 Use of JMenu()
import [Link].*;
class JMenuExample
{
JMenu menu1,menu2, submenu1,submenu2;
JMenuItem i1,i2,i3,i4,i5;
JMenuExample()
{
JFrame f=new JFrame();
JMenuBar mb=new JMenuBar();
menu1 = new JMenu("SYCS-IV");
menu2=new JMenu("SYCS -III");
submenu1 =new JMenu("Programming :");
submenu2=new JMenu("MAths :");
i1=new JMenuItem("Advanced Java");
i2=new JMenuItem("FOA");
i3=new JMenuItem("CN");
i4=new JMenuItem("SE");
i5=new JMenuItem("LAUP");
[Link](i1); [Link](i2); [Link](i3); [Link](i4);
[Link](i5);
[Link](submenu1); [Link](submenu2);
[Link](menu1); [Link](menu2);
[Link](mb);
[Link](400,400);
[Link](null);
[Link](true);
}
public static void main(String args[])
{ new JMenuExample();
}

Prof. Jyoti Samel Page 13


SYCS – Sem IV – Advanced Java Practical

Prof. Jyoti Samel Page 14


SYCS – Sem IV – Advanced Java Practical

10 Use of JPopupMenu()
import [Link].*;
import [Link].*;
class JPopupExample
{
JPopupExample()
{
JFrame f=new JFrame();
JPopupMenu popupmenu=new JPopupMenu("EDIT");
JMenuItem cut = new JMenuItem("CUT");
JMenuItem copy = new JMenuItem("COPY");
JMenuItem paste = new JMenuItem("PASTE");
JLabel label=new JLabel();
[Link](400,100);
[Link](cut); [Link](copy); [Link](paste);
[Link](new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
[Link](f,[Link](),[Link]());
}
});
[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link]("Cut is slected");
}
});
[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link]("Copy is slected");
}
});
[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link]("PAste is slected");
}
});
[Link](label);
[Link](popupmenu);
[Link](500,500);
[Link](null);
[Link](true);

Prof. Jyoti Samel Page 15


SYCS – Sem IV – Advanced Java Practical

public static void main(String args[])


{
new JPopupExample();
}
}

Prof. Jyoti Samel Page 16


SYCS – Sem IV – Advanced Java Practical

11 Use of JRadioButton()- stationary app where user can select only one item due to
ButtonGroup() control
import [Link].*;
import [Link].*;
import [Link].*;
class JRadioButtonDemo extends JFrame implements ActionListener
{
JRadioButton ch1, ch2, ch3;
JLabel jl;
JTextField jtf;
JRadioButtonDemo()
{
setLayout(new FlowLayout());
ch1=new JRadioButton("Pen Rs 20");
ch2=new JRadioButton("Pencil Rs 10");
ch3=new JRadioButton("Scale Rs 50");
ButtonGroup bg=new ButtonGroup();
[Link](ch1); [Link](ch2); [Link](ch3);
jl=new JLabel("Total Rupees :");
jtf=new JTextField(20);
add(ch1); add(ch2); add(ch3); add(jl); add(jtf);
[Link](this); [Link](this);
[Link](this);
setSize(300,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int pen=0 , pencil=0,scale=0, total=0;
if([Link]())
pen=20;
else
pen=0;
if([Link]())
pencil=10;
else
pencil=0;
if([Link]())
scale=50;
else
scale=0;

total=pen+pencil+scale;
[Link]("Cost Rs:"+total);
}
public static void main(String args[])
{
new JRadioButtonDemo();
}
}

Prof. Jyoti Samel Page 17


SYCS – Sem IV – Advanced Java Practical

Prof. Jyoti Samel Page 18


SYCS – Sem IV – Advanced Java Practical

12 Use of JRadioButton()
import [Link].*;
public class JRadiobuttonEx
{
JFrame f;
JRadiobuttonEx()
{
f=new JFrame("Radio Button Example");
JRadioButton r1=new JRadioButton("Male");
JRadioButton r2=new JRadioButton("Female");
[Link](75,50,100,30);
[Link](75,100,100,30);
ButtonGroup bg=new ButtonGroup();
[Link](r1);[Link](r2);
[Link](r1);[Link](r2);
[Link](300,300);
[Link](null);
[Link](true);
}
public static void main(String[] args)
{
new JRadiobuttonEx();
}
}

Prof. Jyoti Samel Page 19


SYCS – Sem IV – Advanced Java Practical

13 Use of JTabbedPane()
import [Link].*;
class JTabbedPaneExample
{
public static void main(String args[])
{
JFrame f = new JFrame("JList Example");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JTabbedPane jTab=new JTabbedPane();
JLabel jL1=new JLabel("This is tab onq");
JLabel jL2=new JLabel("This is tab two");
JTextField jT1=new JTextField("welcome");
[Link](jL1); [Link](jT1);
final Object[][] info={ {"1","jeet"} , {"2","hari"}, {"3","kavi"}};
final String[] cHead={"Roll no", "Name"};
JTable jT=new JTable(info, cHead);
[Link](jT);
[Link](jL2);
[Link]("Tab1", p1);
[Link]("Tab2", p2);
[Link](jTab);
[Link](500,400);
[Link](true);

}
}

Prof. Jyoti Samel Page 20


SYCS – Sem IV – Advanced Java Practical

14 Use of JTable()
import [Link].*;
class JTableExample
{
public static void main(String args[])
{
JFrame f = new JFrame("JList Example");
JPanel p=new JPanel();
final Object[][] info={ {"1","jeet"} , {"2","hari"}, {"3","kavi"}};
final String[] cHead={"Roll no", "Name"};
JTable jT=new JTable(info, cHead);
[Link](jT);
[Link](p);
[Link](400,400);
[Link](true);
}

Prof. Jyoti Samel Page 21


SYCS – Sem IV – Advanced Java Practical

15 Swing Program to create Login demo


import [Link].*;
import [Link].*;
import [Link].*;
class LoginDemo extends JFrame implements ActionListener
{
JLabel unameLbl, pwdLbl , msgLbl;
JTextField unameTxt;
JPasswordField pwdPTxt;
JButton loginBtn, resetBtn;
LoginDemo()
{
super("Practical1");
//setLayout(new FlowLayout());
Container cp=getContentPane();
[Link](new GridLayout(4,2));
unameLbl= new JLabel("Enter your User name :");
pwdLbl= new JLabel("Enter your Password :");
msgLbl= new JLabel(" ");
unameTxt=new JTextField(20);
pwdPTxt=new JPasswordField(20);
[Link]('*');
loginBtn=new JButton("Login");
resetBtn=new JButton("Reset");
[Link](unameLbl); [Link](unameTxt);
[Link](pwdLbl); [Link](pwdPTxt);
[Link](loginBtn); [Link](resetBtn);
[Link](msgLbl);
[Link](this);
[Link](this);
setSize(400,400);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
String un=[Link]();
String pwd=[Link]().toString();
if([Link]()==loginBtn)
{
if([Link]("SYCS") && [Link]("1234"))
[Link]("Login Successful");
else
[Link]("Login Failed");
}
if([Link]()==resetBtn)
{
[Link](null);
[Link]("");
[Link]("");
}

Prof. Jyoti Samel Page 22


SYCS – Sem IV – Advanced Java Practical

}
public static void main(String args[])
{
new LoginDemo();
}
}

Prof. Jyoti Samel Page 23


SYCS – Sem IV – Advanced Java Practical

Prof. Jyoti Samel Page 24


SYCS – Sem IV – Advanced Java Practical

16 Swign program to create marksheet app


import [Link].*;
import [Link].*;
import [Link].*;
class MarksheetDemo extends JFrame implements ActionListener
{
JLabel lblname,lblm1,lblm2,lblm3,lbltotal,lblremark,lbldisptotal,lbldispremark;
JTextField jtfname, jtfm1,jtfm2,jtfm3;
JButton btnCalculate, btnReset;
MarksheetDemo()
{
Container cp=getContentPane();
[Link](new GridLayout(7,2));
lblname=new JLabel("Student name : ");
jtfname=new JTextField();
lblm1=new JLabel("Marks obtained is subject 1: ");
jtfm1=new JTextField();
lblm2=new JLabel("Marks obtained is subject 2: ");
jtfm2=new JTextField();
lblm3=new JLabel("Marks obtained is subject 3: ");
jtfm3=new JTextField();
lbltotal=new JLabel("Total Marks :");
lbldisptotal=new JLabel();
lblremark=new JLabel("REMARK :");
lbldispremark=new JLabel();
btnCalculate=new JButton("RESULT");
btnReset=new JButton("RESET");

[Link](lblname); [Link](jtfname);
[Link](lblm1); [Link](jtfm1);
[Link](lblm2); [Link](jtfm2);
[Link](lblm3); [Link](jtfm3);
[Link](lbltotal); [Link](lbldisptotal);
[Link](lblremark); [Link](lbldispremark);
[Link](btnCalculate); [Link](btnReset);

[Link](this);
[Link](this);

setSize(500,400);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if([Link]()==btnCalculate)
{ double s1,s2,s3,total;
s1=[Link]([Link]());
s2=[Link]([Link]());
s3=[Link]([Link]());
total=s1+s2+s3;

Prof. Jyoti Samel Page 25


SYCS – Sem IV – Advanced Java Practical

[Link](total+"");
if(s1>=40 && s2>=40 && s3>=40)
[Link]("PASS");
else
[Link]("FAIL");
}
if([Link]()==btnReset)
{ [Link](null);
[Link](null);
[Link](null);
[Link](null);
[Link](null);
[Link](null);
}
}
public static void main(String args[])
{
new MarksheetDemo();
}
}

Prof. Jyoti Samel Page 26


SYCS – Sem IV – Advanced Java Practical

Prof. Jyoti Samel Page 27


SYCS – Sem IV – Advanced Java Practical

17 Use of Simple Interest Demo


import [Link].*;
import [Link].*;
import [Link].*;
class SimpleInterestDemo extends JFrame implements ActionListener
{
JLabel PLbl, ILbl , TLbl, msgLbl;
JTextField PTxt, ITxt, TTxt;
JButton calculateBtn, resetBtn;
SimpleInterestDemo()
{
Container cp=getContentPane();
[Link](new GridLayout(5,2));
// setLayout(new FlowLayout());
PLbl=new JLabel("Enter Principal Amt : ");
ILbl=new JLabel("Enter Rate of Interest : ");
TLbl=new JLabel("Enter Time in years : ");
msgLbl=new JLabel(" ");
PTxt=new JTextField(20);
ITxt=new JTextField(20);
TTxt=new JTextField(20);
calculateBtn=new JButton("Calculate");
resetBtn=new JButton("reset");
[Link](PLbl); [Link](PTxt);
[Link](ILbl); [Link](ITxt);
[Link](TLbl); [Link](TTxt);
[Link](calculateBtn); [Link](resetBtn);
[Link](msgLbl);
[Link](this);
[Link](this);

setSize(500,400);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
double p, i, t,si;
p=[Link]([Link]());
i=[Link]([Link]());
t=[Link]([Link]());
if([Link]()==calculateBtn)
{
si=(p*i*t)/100;
[Link]("Simple Interest "+si);
}
if([Link]()==resetBtn)
{
[Link](null); [Link](null); [Link](null);
[Link](null);
}

Prof. Jyoti Samel Page 28


SYCS – Sem IV – Advanced Java Practical

}
public static void main(String args[])
{
new SimpleInterestDemo();
}
}

Prof. Jyoti Samel Page 29


SYCS – Sem IV – Advanced Java Practical

JDBC
1 Steps :
File  New Project 

Press Next

Press Next Next  Finish

Prof. Jyoti Samel Page 30


SYCS – Sem IV – Advanced Java Practical

Open Services Tab


Right click on “Java DB” create database 

Right click on SYCS database and select “Connect”.

To create table within SYCS database


Expand SYCS database
Right click on table  create table

Prof. Jyoti Samel Page 31


SYCS – Sem IV – Advanced Java Practical

Select Add Column

Prof. Jyoti Samel Page 32


SYCS – Sem IV – Advanced Java Practical

Select Project tab


And in java class file type code

import [Link].*; // step 1


class selectData
{ public static void main(String args[])throws Exception
{ try{[Link]("[Link]"); //step2
Connection con=[Link]("jdbc:derby://localhost:1527/SYCS" ,
"SYCS","SYCS"); // step 3
[Link]("Connection okay");
Statement stmt=[Link](); // step 4
ResultSet rs=[Link]("Select * from SYCSDATA"); // step 5
[Link]("Rollno \t Name \t CJ marks");
while([Link]())
{ [Link]([Link](1) + "\t" + [Link](2) +"\t"+ [Link](3));
}
while([Link]())
{ [Link]([Link](1) + "\t" + [Link](2) +"\t"+ [Link](3));
}

[Link]();
[Link]();
[Link]();
}
catch(Exception e)
{
}
}

Prof. Jyoti Samel Page 33


SYCS – Sem IV – Advanced Java Practical

Prof. Jyoti Samel Page 34


SYCS – Sem IV – Advanced Java Practical

2 import [Link].*; // step 1


class insertData
{
public static void main(String args[])throws Exception
{
try{
[Link]("[Link]"); //step2
Connection con=[Link]("jdbc:derby://localhost:1527/SYCS_AJ" ,
"SYCS","SYCS1"); // step 3
[Link]("Connection okay");
Statement stmt=[Link](); // step 4
[Link]("insert into SYCSDATA values (25,'Geetu',5)"); // step 5
[Link]("RECORD INSERTED");
[Link]();
[Link]();

}
catch(Exception e)
{
}
}
}

Prof. Jyoti Samel Page 35


SYCS – Sem IV – Advanced Java Practical

3 import [Link].*; // step 1


class deleteData
{ public static void main(String args[])throws Exception
{ try{[Link]("[Link]"); //step2
Connection con=[Link]("jdbc:derby://localhost:1527/SYCS_AJ" ,
"SYCS","SYCS1"); // step 3
[Link](false);
[Link]("Connection okay");
Statement stmt=[Link](); // step 4
[Link]("delete from SYCSDATA where name='Geetu' "); // step 5
[Link]("Record Deleted");
[Link]();
ResultSet rs=[Link]("Select * from SYCSDATA");
[Link]("Rollno \t Name \t CJ marks");
while([Link]())
{ [Link]([Link](1) + "\t" + [Link](2) +"\t"+ [Link](3));
}
[Link]();
[Link]();
[Link]();
}
catch(Exception e)
{
}
}
}

Prof. Jyoti Samel Page 36


SYCS – Sem IV – Advanced Java Practical

Servlet
1 Design a servlet program to do the arithmetic operations
<form action="AritServlet" method="get">
Enter First Number :
<Input type="text" name="n1">
<br>
Enter Second number:
<Input type="text" name="n2">
<br>
<select name="op">
<option value="add">Additon</option>
<option value="sub">subtraction</option>
<option value="mul">multiplication</option>
<option value="div">division</option>

</select>
<br>
<input type="submit" value="Click">

</form>

Prof. Jyoti Samel Page 37


SYCS – Sem IV – Advanced Java Practical

2 Write a servlet to accept Employee details and insert the data into database
<body>
<form action="EmpServlet" method="post">
Enter Employee name:
<Input type="text" name="ename"> <br>
Enter Employee salary:
<Input type="text" name="esal"> <br>
<input type="submit" value="save"> <br>
<input type="reset" value="clear"> <br>
</form>
</body>
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

@WebServlet(urlPatterns = {"/EmpServlet"})
public class EmpServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

try{
[Link]("Connection okay");
[Link]("[Link]"); //step2
Connection con=[Link]("jdbc:derby://localhost:1527/EmpDB" ,
"EmpDB","EmpDB"); // step 3
[Link]("Connection okay");

String empname=[Link]("ename");
int salary = [Link]([Link]("esal"));

PreparedStatement st=[Link]("insert into EMP values(?,?)");


[Link](1, empname);
[Link](2, salary);

[Link]("RECORD INSERTED");
[Link]();
[Link]();
}
catch(Exception e)
{ [Link]("Error");
}

Prof. Jyoti Samel Page 38


SYCS – Sem IV – Advanced Java Practical

Prof. Jyoti Samel Page 39


SYCS – Sem IV – Advanced Java Practical

3 Write a servlet application –registration for Marathi Bhasha Din


Enter students form [Link] and page and store the details.
<html>
<body>
<form name="reg" method="get" action="regServlet">
<h1>Registration For Marathi Bhasha Divas</h1>
<table border="1">
<tr>
<td>Enter Your name :</td>
<td><Input type="text" name="name"></td>
</tr>
<tr>
<td>Select your Stream :</td>
<td><select name="stream">
<option name="cs">BSc CS</option>
<option name="it">BSc IT</option>
<option name="com">BCOM</option>
</select>
</td>
</tr>
<tr>
<td>Enter WhatsApp Number:</td>
<td><input type="text" name="wno">
</tr>
<tr>
<td><input type="submit"></td>
<td><input type="reset"></td>
</tr>

</table>
</form>
</body>
</html>
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

@WebServlet(urlPatterns = {"/regServlet"})
public class regServlet extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
try (PrintWriter out = [Link]()) {
String st1 = "";
String nm=[Link]("name");

Prof. Jyoti Samel Page 40


SYCS – Sem IV – Advanced Java Practical

String st=[Link]("stream");
String wn=[Link]("wno");
[Link]("Welcome :" +nm);

if([Link]("BScCS"))
{ [Link]("Stream: BSc Computer Science");
}
if([Link]("BScIT"))
{ [Link]("BSc Inforamtion Technology");
}
if([Link]("BCOM"))
{ [Link]("B Com");

}
}

Prof. Jyoti Samel Page 41


SYCS – Sem IV – Advanced Java Practical

4 Create a servlet for a login page. If the username and password are correct then it says
message “Hello <username>” else a message “login failed”
<html>
<head><title>Login Form</title></head>
<form action="LoginServlet" >
Enter User ID<input type="text" name="txtId"><br>
Enter Password<input type="password" name="txtPass"><br>
<input type="reset">
<input type="submit" value="<< Click to Login >>" >
</form>
</html>
package mypack;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
[Link]("<html><head>");
[Link]("<title>Servlet LoginServlet</title></head>");
String uname = [Link]("txtId");
String upass = [Link]("txtPass");
if([Link]("admin") && [Link]("12345")){
[Link]("<body bgcolor=blue >");
[Link]("<h1> Welcome !!! "+uname+"</h1>");
}
else{
[Link]("<body bgcolor=red >");
[Link]("<h1> Login Fail !!! </h1>");
}
[Link]("</body>");
[Link]("</html>");
}
}

Prof. Jyoti Samel Page 42


SYCS – Sem IV – Advanced Java Practical

Prof. Jyoti Samel Page 43


SYCS – Sem IV – Advanced Java Practical

5 Using Request Dispatcher Interface create a Servlet which will validate the password
entered by the user, if the user has entered "Servlet" as password, then he will be
forwarded to Welcome Servlet else the user will stay on the [Link] page and an error
message will be displayed.
<html>
<head><title>Login Form</title></head>
<form action="LoginServlet" >
Enter User ID<input type="text" name="txtId"><br>
Enter Password<input type="password" name="txtPass"><br>
<input type="reset">
<input type="submit" value="<< Click to Login >>" >
</form>
</html>
package mypack;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
[Link]("<html><head>");
[Link]("<title>Servlet LoginServlet</title></head>");
String uname = [Link]("txtId");
String upass = [Link]("txtPass");
if([Link]("admin") && [Link]("servlet")){
RequestDispatcher rd = [Link]("WelcomeServlet");
[Link](request, response);
}
else{
[Link]();
[Link]("<h1> Login Fail !!! </h1>");
RequestDispatcher rd = [Link]("[Link]");
[Link](request, response);
}
[Link]("</body>");
[Link]("</html>");
}
}
package mypack;

Prof. Jyoti Samel Page 44


SYCS – Sem IV – Advanced Java Practical

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class WelcomeServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = [Link]();
[Link]("<html><head>");
[Link]("<title>Welcome Servlet</title></head>");
[Link]("");
[Link]("<h1>Welcome to the world of Servlet !!!!</h1>");
[Link]("</body>");
[Link]("</html>");
}
}

Prof. Jyoti Samel Page 45


SYCS – Sem IV – Advanced Java Practical

JSP
1 Page directive – include page
[Link]

<%--
Document : B
Created on : Mar 20, 2021, 8:55:18 AM
Author : ADMIN
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<%@include file="[Link]"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<a>This is the main file</a>
</body>
</html>

[Link]

<%--
Document : A
Created on : Mar 20, 2021, 8:54:16 AM
Author : ADMIN
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<a>Header file : </a>
<%
int count =1;
count++;
[Link](count);
%>

Prof. Jyoti Samel Page 46


SYCS – Sem IV – Advanced Java Practical

</body>
</html>

Prof. Jyoti Samel Page 47


SYCS – Sem IV – Advanced Java Practical

2. Display current date


<%@page contentType="text/html" pageEncoding="UTF-8" import="[Link].*;"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<% [Link]("todays date is : "+ new Date());
%>
</body>
</html>

Prof. Jyoti Samel Page 48


SYCS – Sem IV – Advanced Java Practical

3 Jsp Login
[Link] or [Link]

<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="[Link]" method="post">
Ente your name:
<input type="text" name="t1">
Enter Password:
<input type="password" name="p1">
<br>
<input type="submit">
</form>
</body>
</html>
[Link]

<%@page contentType="text/html" pageEncoding="UTF-8" language="java"


import="[Link].*;" info="this is the welcome page" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="[Link]" method="get">
<%
String u=[Link]("t1");
String p=[Link]("p1");
if([Link]("admin") && [Link]("123"))
[Link]("Welcome " + u);
else
[Link]("Login Unsuccessful");

%>

Prof. Jyoti Samel Page 49


SYCS – Sem IV – Advanced Java Practical

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

Prof. Jyoti Samel Page 50


SYCS – Sem IV – Advanced Java Practical

4 Develop a simple JSP application to pass values from one page to another with validations.
(Name-txt, age-txt, hobbies-checkbox, email-txt, gender-radio button).
[Link]

<html><head><title>User Information Paage</title>


</head>
<body>
<form action="[Link]">
Enter Your Name<input type="text" name="name" ><br>
Enter Your Age<input type="text" name="age" ><br>
Select Hobbies
<input type="checkbox" name="hob" value="Singing">Singing
<input type="checkbox" name="hob" value="Reading">Reading Books
<input type="checkbox" name="hob" value="Football">Playing Football<br>
Enter E-mail<input type="text" name="email" ><br>
Select Gender
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="other">Other<br>

<input type="submit" value="Submit Form">


</form>
</body>
</html>
[Link]

<%@page contentType="text/html" pageEncoding="UTF-8" import="mypack.*" %>


<html><head><title>JSP Page</title></head>
<body>
<h1>Validation Page</h1>
<jsp:useBean id="obj" scope="request" class="[Link]" >
<jsp:setProperty name="obj" property="*"/>
</jsp:useBean>
<%if ([Link]())
{ %>
<jsp:forward page="[Link]"/>
<% }
else {%>
<jsp:include page="[Link]"/>
<%}%>
<%=[Link]() %>
</body></html>
[Link]

package mypack;
public class CheckerBean {
private String name, age, hob, email, gender, error;
public CheckerBean(){error="";}

Prof. Jyoti Samel Page 51


SYCS – Sem IV – Advanced Java Practical

public void setName(String n){name=n;}


public void setAge(String a){age=a;}
public void setHob(String h){hob=h;}
public void setEmail(String e){email=e;}
public void setGender(String g){gender=g;}
public void setError(String e){error=e;}
public String getName(){return name;}
public String getAge(){return age;}
public String getHob(){return hob;}
public String getEmail(){return email;}
public String getGender(){return gender;}
public String getError(){return error;}
public boolean validate(){
boolean res=true;
if([Link]().equals("")) {error+="<br>Enter First Name";res=false;}
if([Link]() > 2 )
{error+="<br>Age Invalid";res=false;}
return res;
}
}
[Link]

<%--
Document : successful
Created on : Apr 5, 2021, 2:02:06 AM
Author : ADMIN
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Data Validated Successfully !!!</h1>
</body>
</html>

Prof. Jyoti Samel Page 52


SYCS – Sem IV – Advanced Java Practical

Prof. Jyoti Samel Page 53


SYCS – Sem IV – Advanced Java Practical

JavaBean
1. Write a simple javabean code to access employee id and name
[Link]

package person;

public class employee {


private int id;
public String name;

public int getId() {


return id;
}

public void setId(int id) {


[Link] = id;
}

public String getName() {


return name;
}

public void setName(String name) {


[Link] = name;
}

}
[Link]

package person;

public class person {


public static void main(String args[])
{
employee e=new employee();
[Link](101);
[Link]("Ajay");
[Link]("Employee id:"+[Link]());
[Link]("Employee Name:" +[Link]());
}
}

Prof. Jyoti Samel Page 54


SYCS – Sem IV – Advanced Java Practical

Prof. Jyoti Samel Page 55


SYCS – Sem IV – Advanced Java Practical

2 Write a simple java bean code to access student details <jsp:useBean> tag
[Link]

package Bean;

public class studentBean {


private String fname, lname;
private int age;

public String getFname() {


return fname;
}

public void setFname(String fname) {


[Link] = fname;
}

public String getLname() {


return lname;
}

public void setLname(String lname) {


[Link] = lname;
}

public int getAge() {


return age;
}

public void setAge(int age) {


[Link] = age;
}

Prof. Jyoti Samel Page 56


SYCS – Sem IV – Advanced Java Practical

[Link]

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<jsp:useBean id="students" class="[Link]">
<jsp:setProperty name="students" property="fname" value="John"/>
<jsp:setProperty name="students" property="lname" value="Fernandese"/>
<jsp:setProperty name="students" property="age" value="18"/>
</jsp:useBean>
<p>
First Name : <jsp:getProperty name="students" property="fname"/>
Last Name : <jsp:getProperty name="students" property="lname"/>
Age : <jsp:getProperty name="students" property="age"/>
</p>
</body>
</html>

Prof. Jyoti Samel Page 57

You might also like