0% found this document useful (0 votes)
9 views87 pages

All Practicals Ajava

Asuhx

Uploaded by

roxosa6042
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)
9 views87 pages

All Practicals Ajava

Asuhx

Uploaded by

roxosa6042
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
You are on page 1/ 87

lOMoARcPSD|47642596

196170307083 All Practicals Ajava

Advance Java Programming (Gujarat Technological University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Dhshs Hshshs ([email protected])
lOMoARcPSD|47642596

LABORATORY MANUAL

Semester: VI

Course Name: Advanced Java Programming

Course Code: (3360701)

Student Name: Chaitanya M. Parmar

Enrollment No.:196170307083 Division: B

Batch: B1 Shift: Morning

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Government Polytechnic
Ahmedabad

This is to certify that Mr. /Miss

_________________________________________of

Diploma Computer Engineering Semester 6th Enrollment

No ____________________ has satisfactorily completed

his/her term work in Advanced Java Programming

(3360701) for the term ending in__________________.

Date of Submission: - ____/____/______

Signature Of Faculty Signature of H.O.D.

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Government Polytechnic, Ahmedabad


Computer Department
Practical Index

Name: __________________________________ Enrollment No.: ___________

Course Name: Advance Java Programming (3360701) Semester: 6 Batch: ____

Sr. Page
Practical Name Date Grade Sign
No. No
Unit-1 [CO1: Write Java Applet programs]
Write an applet that draws a circle. The
dimension of the applet should be 500 x 300
1. pixels. The circle should be centered in the
applet and have a radius of 100 pixels. Display
your name centered in a circle.
Draw ten red circles in a vertical column in the
2.
center of the applet
Built an applet that displays a horizontal
3. rectangle in its center. Let the rectangle fills
with color from left to right.
Built an applet that displays a sum of two
4. numbers which are passed as parameters from
a html file to an applet.
Unit-2 [CO2-Develop GUI based application using Abstract Window Toolkit (AWT)
& Swing to handle events]
Write an applet that displays the position of the
mouse when it is dragged or moved. Draw a
5.
10x10 pixel rectangle filed with black at the
current mouse position.
Write an applet that contains one button.
Initialize the label on the button to “start”,
when the user presses the button, the label on
6.
the button should change to “stop”. The label
on the button should change between these two
values each time the button is pressed.
Write a programs to demonstrate the use of
7.
mousePressed and mouseReleased methods.
Write a program that has only one button in the
frame, clicking on the button cycles through
8.
the colors: red->green->blue-> and so on. one
color change per click.
Write an applet that contains three check boxes
9.
and 30 x 30 pixel canvas. The three

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Sr. Page
Practical Name Date Grade Sign
No. No
checkboxes should be labeled “Red”, ”Green”,
”Blue”. The selection of the check boxes
determines the color of the canvas.
Create an application that displays a frame
10. with a Menu Bar and to display the selection
of menu item in the center of the frame
Develop a program that draws two sets of
11. ever-decreasing rectangles one in outline form
and one filled alternatively in black and white.
Write a program which makes simple student
12.
details form using AWT controls.
Write a program which makes use of Swing
13.
Control JButton and JTextField.
Write a program which makes use of Swing
14.
Control JCombobox.
Write a program which makes use of Swing
15.
Control JRadioButton.
Unit-3 [CO-3 Implement simple java programs with Java Database Connectivity
(JDBC)]
16. Create the table ‘employee’ in Oracle. Write a
program to display all employees whose salary
is greater than 2000 and less than 6000.
17. Write a program to present a set of choice for
user to select a product & display the price of
product from the database.
18. Develop a UI that performs the following SQL
operations:1) Insert 2) Update 3) Delete
Unit-4 [CO-4 Develop Server-side programs using Servlets ]
Develop a simple Servlet program which
maintains a counter for the number of times it
19. has been accessed since its loading; initialize
the counter using deployment descriptor and
print today’s date.
Develop a simple Servlet program which
20. present a set of choice for Colors and display
the selected color.

21. Create a web form which processes Servlet


and demonstrates use of cookies and sessions.
Unit-5 [CO-5 Develop dynamic web pages using JSP]
Develop a simple JSP program to display
22. Name, System date and grade of a student by
accepting the marks of five subjects.

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Sr. Page
Practical Name Date Grade Sign
No. No
Develop a simple JSP program for user login
23.
form with static and dynamic database.
Develop a JSP program for user registration
24. and then control will be transfer to second
page.

_____________________________

Faculty Signature

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

UNIT-1
Java Applet

1
Downloaded by Dhshs Hshshs ([email protected])
lOMoARcPSD|47642596

❖ Introduction
➢ Steps to developing and testing an applet:
1) Building an applet code(.java file)
2) Creating an executable applet (.class file)
3) Designing a web page using HTML tags
4) Preparing <APPLET> tag
5) Incorporating <APPLET> tag into the web page
6) Creating HTML file
7) Testing the applet code
General format of applet code:
import java.awt.*;
import java.applet.*;
.........
public classappletclassnameextendsApplet
{
............
public void paint(Graphics g)
{
//Applet operation code
}
............
}

//save file with name Hellojava.class in a java subdirectory

HTML file with <APPLET> tag Hellojava.html

<HTML>
<!This page includes a welcome title in title bar and also display a welcome
message>
<HEAD>
<TITLE>Welcome to Java Applets </TITLE>
</HEAD>
<BODY>
<CENTER>
<H1> WELCOME </H1>
</CENTER>
<APPLET CODE=Hellojava.class WIDTH=200 HEIGHT=400 >
</APPLET>
</BODY>
</HTML>

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Java applet code Hello.java

import java.awt.*;
import java.applet.*;
public class Hellojava extends Applet
{
public void paint(Graphics g)
{
g.drawString( “Hello java” ,10,100);
}
}
Output:

We must have following files in our current directory:


1) Hellojava.java
2) Hellojava.class
3) Hellojava.html

Compiling an applet :
javac Hellojava.java

Running an applet

To run an applet ,we require one of following tools:


1) Using Java enabled web browser :if we use it,we will be able to see the entire
web page containing the applet.
2) Using Java appletviewer:
Syntax: appletviewer Hellojava.html

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Practical No.: 1 Date : / /


AIM:
(A) Write an applet that draws a circle. The dimension of the applet should be 500 x 300
pixels. The circle should be centered in the applet and have a radius of 100 pixels. Display
your name centered in a circle.

Java Code: -

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

public class prac1 extends Applet


{
public void init()
{
super.init();
setSize(500,300);
}
public void start()
{

}
public void paint(Graphics g)
{
g.drawOval(200,100,100,100);
g.drawString("Chaitanya",222,155);
}
public void stop()
{

}
public void destroy()
{

}
}

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

HTML Code: -

<html>
<applet code="prac1.class" width="100" height="100"></applet>
</html>

Output:

Grade: ___________ Sign of Faculty: ___________________


5

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Practical No. :2 Date: / /


AIM:
Draw ten red circles in a vertical column in the center of the applet.

Java Code: -

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

public class prac2 extends Applet


{
public void init()
{
super.init();
setSize(500,550);
}
public void start(){}
public void paint(Graphics g)
{
int circley = 225;
int circlex= 0;
for (int i=0;i<10;i++)
{
g.setColor(Color.RED);
g.fillOval(circley,circlex,50,50);
circlex +=55;
}
}
public void stop(){}
public void destroy(){}
}
Html Code: -

<html>
<applet code="prac2.class" width="100" height="100"></applet>
</html>

Output:

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Grade: ___________ Sign of Faculty: ___________________


7

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Practical No.: 3 Date: / /

AIM:
Built an applet that displays a horizontal rectangle in its center. Let the rectangle fill with
color from left to right.

Java Code: -

import java.applet.*;
import java.awt.*;
public class prac3 extends Applet
{
public void init()
{
super.init();
setSize(350,350);
}
public void start()
{

}
public void paint(Graphics s)
{
int x1=100,y1=100,y2=50;
s.setColor(Color.green);
s.drawRect(100,100,100,50);
for(x1=100;x1<300;x1=x1+5)
{
try
{
Thread.sleep(1000);
s.fillRect(x1,y1,5,y2);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
public void stop()

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

{
}
public void destroy()
{

}
}

Html Code: -
<html>
<applet code="prac3.class" width="100" height="100"></applet>
</html>

Output:

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Grade: ___________ Sign of Faculty: ___________________

Practical No.: 4 Date: / /


AIM:
Built an applet that displays a sum of two numbers which are passed as parameters from
a html file to an applet.

Java Code: -

import java.applet.*;
import java.awt.*;
public class prac4 extends Applet
{
public void init()
{
super.init();
setSize(500,500);
}
public void start(){}
public void paint(Graphics g)
{
int a = Integer.parseInt(getParameter("a"));
int b= Integer.parseInt(getParameter("b"));
int sum=a+b;
g.drawString(String.valueOf(sum),250,250);
}
public void stop(){}
public void destroy(){}
}

Html Code: -

<html>
<applet code="prac4.class" width=300 height=300>
<param name="a" value="10">
<param name="b" value="20">
</applet>
</html>

10

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Output:

11

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Grade: ___________ Sign of Faculty: ___________________

UNIT-2
AWT

12

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Practical No. : 5 Date: / /

AIM:
Write an applet that displays the position of the mouse at the upper left corner of the applet
when it is dragged or moved. Draw a 10x10 pixel rectangle filed with black at the current
mouse position.

Java Code: -

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

public class prac5 extends Applet implements MouseListener,MouseMotionListener{


String str = "";
int x,y;
public void init(){
super.init();
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g){
g.drawString(str,160,150);
g.fillRect(x,y,10,10);
}
public void mouseDragged(MouseEvent e){
x = e.getX();
y = e.getY();
str = "X-axis: " + x +" Y-axis: " +y;
repaint();
}
public void mouseMoved(MouseEvent e){
x = e.getX();
y = e.getY();
str = "X-axis: " +x +" Y-axis: " +y;
repaint();
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}

13

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Html Code: -
<html>
<applet code="prac5.class" width="400" height="400"></applet>
</html>

Output:

14

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Grade: ___________ Sign of Faculty: ___________________

15

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Practical No. : 6 Date: / /


AIM:
Write an applet that contains one button. Initialize the label on the button to “start”, when
the user presses the button change the label should be change to stop .between these two
values each time the button is pressed.

Java Code: -
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class prac6 extends Applet implements ActionListener{
String msg;
Button btn;
public void init(){
btn = new Button();
btn.setLabel("START");
add(btn);
btn.addActionListener(this);
}
public void paint(Graphics g){
if(msg=="START"){
btn.setLabel("STOP");
}
else{
btn.setLabel("START");
}
}
public void actionPerformed(ActionEvent e){
msg = btn.getLabel();
repaint();
}
}

Html Code: -

<html>
<applet code="prac6.class" width = "400" height = "400">
</applet>
</html>

16

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Output:

17

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Grade: ___________ Sign of Faculty: __________________

18

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Practical No. : 7 Date: / /


AIM:
Write an applet that uses the mouse listener, which overrides only two methods which are
mousePressed and mouseReleased methods.

Java Code: -
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class prac7 extends Applet implements MouseListener,MouseMotionListener{
String msg="";
int x, y;
public void init(){
super.init();
setSize(400,400);
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics s){
showStatus(msg);
s.drawString(msg,50,50);
}
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseClicked(MouseEvent arg0) {}
public void mouseEntered(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
public void mousePressed(MouseEvent arg0) {
msg="Mouse Pressed";
repaint();
}
public void mouseReleased(MouseEvent arg0) {
msg="Mouse Released";
repaint();
}
}

19

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

HTML Code: -
<html>
<applet code="prac7.class" width="400" height="400">
</applet>
</html>

Output:

20

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Grade: ___________ Sign of Faculty: __________________

Practical No.:8 Date: / /


AIM:
Write a program that has only one button in the frame, clicking on the button cycles through
the colors: red->green->blue-> and so on. One colour change per click.(use
getBackground() method to get the current colour)

Java Code: -

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class prac8 extends Applet implements ActionListener{
String msg;
Button btn;
public void init(){
btn = new Button();
btn.setLabel("START");
add(btn);
btn.addActionListener(this);
}
public void paint(Graphics g){
if(msg=="RED"){
setBackground(Color.RED);
btn.setLabel("GREEN");
}
else if(msg=="GREEN"){
setBackground(Color.GREEN);
btn.setLabel("BLUE");
}
else{
setBackground(Color.BLUE);
btn.setLabel("RED");
}
}
public void actionPerformed(ActionEvent e){
msg = btn.getLabel();
repaint();
}
21

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

HTML Code: -
<html>
<applet code="prac8.class" width = "400" height = "400">
</applet>
</html>

Output:

22

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Grade: ___________ Sign of Faculty: ___________________

Practical No.:9 Date: / /


AIM:
Write an applet that contains three check boxes and 30 x 30-pixel canvas. The three
checkboxes should be labelled “Red”,” Green”,” Blue”. The selection of the check boxes
determines the colour of the canvas. For example, if the user selects both “Red” and
“Blue”, the canvas should be purple.

Java Code: -

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet;

public class prac9 extends Applet implements ItemListener{


JCheckBox j1,j2,j3;
Canvas c;
public void init(){
j1=new JCheckBox("Red");
j1.addItemListener(this);
j2=new JCheckBox("Green");
j2.addItemListener(this);
j3=new JCheckBox("Blue");
j3.addItemListener(this);
add(j1);
add(j2);
add(j3);
c=new Canvas();
c.setSize(30,30);
c.setBackground(Color.CYAN);
add(c);
}
public void itemStateChanged(ItemEvent e){
if(j1.isSelected()){
c.setBackground(Color.RED);
}
if(j2.isSelected()){
c.setBackground(Color.GREEN);
}
23

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

if(j3.isSelected()){
c.setBackground(Color.BLUE);
}
}
public void paint(Graphics g){
g.drawString("canvas",20,20);
}
}

Html Code: -

<html>
<applet code="prac9.class" width="400" height="400">
</applet>
</html>

Output:

24

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Grade: ___________ Sign of Faculty: ___________________

25

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Practical No.: 10 Date: / /

AIM:
Create an application that displays a frame with a menubar. When a user
selects any menu or menu item, display that selection on a text area in the
center of the frame
Java Code: -

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

public class prac10{


prac10(){
Frame f=new Frame("Menubar");
MenuBar m = new MenuBar();
Menu file = new Menu("File");
Menu home = new Menu("Home");
Menu edit = new Menu("Edit");
Menu help = new Menu("Help");
MenuItem n = new MenuItem("New");
MenuItem s = new MenuItem("Save");
MenuItem sa = new MenuItem("Save As");
MenuItem c = new MenuItem("Close");
file.add(s);
file.add(sa);
file.add(n);
file.add(c);
m.add(file);
m.add(home);
m.add(edit);
m.add(help);
f.setMenuBar(m);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
f.addWindowListener (new WindowAdapter() {
public void windowClosing (WindowEvent e) {
f.dispose();
}
});
}
public static void main(final String[] ar){
new prac10();
}
26

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

}
Output:

27

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Grade: ___________ Sign of Faculty: ___________________

Practical No.: 11 Date: / /


AIM:
Write an applet that draws two sets of ever-decreasing rectangles one in
outline form and one filled alternately in black and white.
Java Code: -
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class prac11 extends Applet {
int x, y, h, w;
public void init() {
super.init();
setSize(500, 500);
x = 10;
y = 100;
h = 300;
w = 200;
}
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i< 20; i++) {
x = x + 5;
y = y + 5;
h = h - 10;
w = w - 10;
if (i % 2 == 0) {
g.setColor(Color.black);
g.fillRect(x, y, h, w);
}
else {
g.setColor(Color.white);
g.fillRect(x, y, h, w);
}
}
}
}

Html Code: -

<html>
<applet code="prac11.class" width=500 height=500></applet>
28

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

</html>

29

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Output:

30

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Grade: ___________ Sign of Faculty: ___________________

Practical No.: 12 Date: / /


AIM:
Write a program which makes simple student details form using AWT controls.

Java Code: -

import java.awt.*;

public class prac12 extends Frame{

Button btn_save=new Button("save");


Label l11=new Label("STUDENT DETAILS FORM:",Label.CENTER);
Label l1=new Label("Name:",Label.LEFT);
Label l2=new Label("Enrollment No:",Label.LEFT);
Label l3=new Label("Gender:",Label.LEFT);
Label l4=new Label("Course:",Label.LEFT);
Label l5=new Label("Semester:",Label.LEFT);
Label l6=new Label("",Label.RIGHT);
TextField t1=new TextField();
TextField age=new TextField();
Choice c1=new Choice();
CheckboxGroup cbg=new CheckboxGroup();
Checkbox ck1=new Checkbox("Male",false,cbg);
Checkbox ck2=new Checkbox("Female",false,cbg);
Choice course=new Choice();
Choice sem=new Choice();

public prac12(){
setBackground(Color.WHITE);
setForeground(Color.black);
setLayout(null);

add(l11);
add(l1);
add(l2);
add(l3);
add(l4);

31

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

add(l5);
add(l6);
add(t1);
add(age);
add(ck1);
add(ck2);
add(course);
add(sem);
add(age);
add(btn_save);

course.add("Computer");
course.add("Civil");
course.add("Mechanical");
course.add("IT");

sem.add("1");
sem.add("2");
sem.add("3");
sem.add("4");
sem.add("5");
sem.add("6");

l1.setBounds(25,65,90,20);
l2.setBounds(25,90,90,20);
l3.setBounds(25,120,90,20);
l4.setBounds(25,165,90,20);
l5.setBounds(25,220,90,20);
l6.setBounds(25,290,90,20);
l11.setBounds(10,40,280,20);
t1.setBounds(120,65,170,20);
course.setBounds(120,165,170,60);
ck1.setBounds(120,120,50,20);
ck2.setBounds(170,120,60,20);
sem.setBounds(120,220,100,20);
age.setBounds(120,90,170,20);
btn_save.setBounds(120,320,50,30);

32

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

public static void main(String g[]){


prac12 a = new prac12();
a.setVisible(true);
a.setSize(400,400);
}
}

Output: -

33

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Grade: ___________ Sign of Faculty: ___________________

Practical No. : 13 Date: / /


AIM:
Write a program which makes use of Swing Control JButton and JTextField.

Java Code: -

import java.awt.*;
import javax.swing.*;
public class prac13{
JFrame f = new JFrame("Practical 13");
JLabel lblname = new JLabel("Full Name: ");
JLabel lblno = new JLabel("Enrollment No: ");
JTextField txtname = new JTextField();
JTextField txtno = new JTextField();
JButton btnsubmit = new JButton("Submit");

public prac13(){
f.setLayout(null);
f.setSize(400,400);
f.setVisible(true);

lblname.setBounds(50,50,200,30);
txtname.setBounds(150,50,200,30);
lblno.setBounds(50,80,200,30);
txtno.setBounds(150,80,200,30);
btnsubmit.setBounds(150,130,95,30);

f.add(lblname);
34

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

f.add(txtname);
f.add(lblno);
f.add(txtno);
f.add(btnsubmit);
}
public static void main(String [] args){
new prac13();
}
}

Output:

35

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Grade: ___________ Sign of Faculty: ___________________

Practical No. : 14 Date: / /


AIM:
Write a program which makes use of Swing Control JCombobox.

Java Code: -

import java.awt.*;
import javax.swing.*;

public class prac14{

JFrame f = new JFrame("Practical 14");


JLabel lblname = new JLabel("Branch");
String branches[] =
{"Computer","Electrical","Mechanical","Plastic","Civil"};
JComboBox branch = new JComboBox(branches);
JButton btnsubmit = new JButton("Submit");

public prac14(){

f.setLayout(null);
f.setSize(400,400);
f.setVisible(true);

lblname.setBounds(50,50,200,30);
branch.setBounds(100,50, 200,30);
btnsubmit.setBounds(150,130,95,30);

36

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

f.add(lblname);
f.add(branch);
f.add(btnsubmit);
}
public static void main(String [] args){
new prac14();
}
}

Output: -

37

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Grade: ___________ Sign of Faculty: ___________________

Practical No.: 15 Date: / /


AIM:
Write a program which makes use of Swing Control JRadioButton.

Java Code: -

import javax.swing.*;
public class prac15 {
JFrame f;
prac15(){
f=new JFrame("Practical 15");
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);

JLabel lbl = new JLabel("Gender:");


lbl.setBounds(30,50,200,40);
JRadioButton rb1=new JRadioButton("Male");
JRadioButton rb2=new JRadioButton("Female");
JRadioButton rb3=new JRadioButton("Others");
rb1.setBounds(100,50,200,40);
rb2.setBounds(170,50,200,40);
rb3.setBounds(240,50,200,40);
ButtonGroup b1=new ButtonGroup();
b1.add(rb1);
b1.add(rb2);
b1.add(rb3);

f.add(lbl);
f.add(rb1);
f.add(rb2);
f.add(rb3);

JButton btnsubmit = new JButton("Submit");


btnsubmit.setBounds(150,130,95,30);
38

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

f.add(btnsubmit);
}
public static void main(String[] args) {
new prac15();
}
}
Output: -

39

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Grade: ___________ Sign of Faculty: ___________________

Unit-3
JDBC

40

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

41

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Practical No.: 16 Date: / /


AIM:
Create the table ‘employee’ in Oracle. Write a program to display all employees whose
salary is greater than 2000 and less than 6000.

Java Code: -

import java.sql.*;
public class prac16 {
static final String JDBC_driver = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3307/dbdemo";
static final String URNAME = "root";
static final String PASS = "admin";
public static void main(String[] args) {
try {
Class.forName(JDBC_driver);
Connection conn = DriverManager.getConnection(DB_URL,URNAME,
PASS);
Statement stat = conn.createStatement();
String query = "Select * from employee where emp_sal>2000 and
emp_sal<6000;";
ResultSet result = stat.executeQuery(query);
while (result.next()) {
System.out.println("Employee Name:" +
result.getString("EMP_NAME"));
System.out.println("Employee Salary:"+
result.getString("EMP_SAL"));
System.out.println();
}
}
catch (Exception e) {
System.out.println("Something Went Wrong!! "+e);
42

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

}
}
}

Output: -

Grade: ___________ Sign of Faculty: ___________________


43

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Practical No. : 17 Date: / /


AIM:
Write a program to present a set of choice for user to select a product & display the price
of product from the database.

Java Code: -

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

public class prac17 {


static final String JDBC_driver = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3307/dbdemo";
static final String URNAME = "root";
static final String PASS = "admin";

public static void main(String[] args) throws SQLException,


ClassNotFoundException {
Class.forName(JDBC_driver);
Connection conn = DriverManager.getConnection(DB_URL, URNAME,
PASS);
System.out.println("Connected");
java.sql.Statement stat = conn.createStatement();
Frame f = new Frame("Practical 17");
Button bprice = new Button("Show Price");
Label lname = new Label("Product Name:");
Choice pname = new Choice();
Label lprice = new Label("Product Price:");
final TextField tprice = new TextField();
pname.add("Select Product");
pname.add("T-Shirt");
pname.add("Shirt");
pname.add("Cap");
pname.add("Hoodie");
pname.add("Jeans");
pname.add("Shorts");
bprice.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {

44

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

String s = "select price from product where name='" +


pname.getSelectedItem () + "';";
ResultSet result = stat.executeQuery(s);
if (result.next()) {
tprice.setText(result.getString(1));
}
else {
tprice.setText("No data Found");
}
} catch (Exception ae) {
System.out.println("Something Went Wrong!! "+e);
}
}
});
lname.setBounds(100, 50, 100, 20);
pname.setBounds(200, 50, 200, 20);
lprice.setBounds(100, 80, 100, 20);
tprice.setBounds(200, 80, 200, 20);
bprice.setBounds(230, 130, 70, 30);
f.add(lname);
f.add(pname);
f.add(lprice);

f.add(tprice);
f.add(bprice);
f.setTitle("Practical 17");
f.setSize(500, 500);
f.setLayout(null);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
f.dispose();
}
});
}
}

45

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Output:

46

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Grade: ___________ Sign of Faculty: ___________________

Practical No.: 18 Date: / /


AIM:
Develop a UI that performs the following SQL operations:1) Insert 2) Delete
3) Update.
Java Code: -

import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.awt.event.*;
public class prac18 extends JFrame implements ActionListener
{
JTextField jtf1=new JTextField(10);
JTextField jtf2=new JTextField(10);
JTextField jtf3=new JTextField(10);
JLabel jlb1=new JLabel("Employee No :",JLabel.CENTER);
JLabel jlb2=new JLabel("Employee Name:",JLabel.CENTER);
JLabel jlb3=new JLabel("Employee Salary:",JLabel.CENTER);
JLabel jlb4=new JLabel(" ");
JLabel jlb5=new JLabel(" ");
JLabel jlb6=new JLabel(" ");
JLabel jlb7=new JLabel(" ");
JButton jbinst=new JButton("Insert");
JButton jbupdt=new JButton("Update");
JButton jbdlt=new JButton("Delete");
Statement stmt;
ResultSet rs;
prac18()
{
jbinst.addActionListener(this);
jbdlt.addActionListener(this);
jbupdt.addActionListener(this);
JPanel jp=new JPanel();
jp.setLayout(new GridLayout(0,2));
jp.add(jlb1);
jp.add(jtf1);
jp.add(jlb2);
jp.add(jtf2);
47

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

jp.add(jlb3);
jp.add(jtf3);
jp.add(jbinst);
jp.add(jbdlt);
jp.add(jbupdt);
jp.add(jlb4);
jp.add(jlb5);
jp.add(jlb6);
jp.add(jlb7);
add(jp);
initializeDB();
}
private void initializeDB(){
try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection cn=DriverManager. getConnection ("jdbc:mysql: //localhost:3307/
dbdemo", "root", "admin");

stmt=cn.createStatement();
}
catch(Exception e){
System.out.println(e);
}
}
public void actionPerformed(ActionEvent e)
{

String s2;
int s1, s3;
if(e.getActionCommand().equals("Insert"))
{
s1= Integer.parseInt(jtf1.getText());
s2= jtf2.getText();
s3= Integer.parseInt(jtf3.getText());
try{
int in=stmt.executeUpdate("INSERT INTO EMPLOYEE (EMP_NO,
EMP_NAME,
EMP_SAL) VALUES("+s1+",'"+s2+"',"+s3+")");

48

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

if(in>0)
JOptionPane.showMessageDialog(null, "Record Inserted
Successfully!!!");
String qs="SELECT DISTINCT * FROM EMPLOYEE";
rs=stmt.executeQuery(qs);
if(rs.next())
{
int st1=s1;
String st2=s2;
int st3=s3;
jlb5.setText("Employee No : "+st1);
jlb6.setText("Employee Name : "+st2);
jlb7.setText("Employee Salary : "+st3);
}
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
if(e.getActionCommand().equals("Delete"))
{
try
{
String sql = "DELETE FROM EMPLOYEE " + "WHERE
EMP_NO="+jtf1.getText();
stmt.execute(sql);
JOptionPane.showMessageDialog(null, "Record Deleted
Successfully!!!");
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
if(e.getActionCommand().equals("Update"))
{
try
{

49

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

String sql="UPDATE EMPLOYEE SET EMP_NAME='"+jtf2.getText()+"',


EMP_SAL='"+jtf3.getText()+"' WHERE EMP_NO = "+jtf1.getText()+"";
stmt.execute(sql);
JOptionPane.showMessageDialog(null, "Record Updated Successfully!!!");
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception{
prac18 frame = new prac18();
frame.setTitle("Practial 18");
frame.setSize(500,500);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
frame.setLayout(new FlowLayout());
}
}

Output:

1) Insert

50

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

2) Update

51

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

3) Delete

• Table

52

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Grade: ___________ Sign of Faculty: ___________________

53

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

UNIT-4
Servlet

54

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Follow this step to develop and run servlet


1. Create demo folder in webapps directory of Apache tomcat folder.
2. Add WEB-INF folder and index.html file in demo directory
3. Add classes folder and web.xml file in WEB_INF folder.
4. Create First.java file in classes folder, add code and compile that file to create class
file
5. Edit web.xml file and add necessary elements.
6. Start tomcat server.
7. Open browser and type
http:\\localhost:8000\demo\First

55

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Practical No: 19 Date: / /

Aim:

Develop a simple Servlet program which maintains a counter for the number of times it
has been accessed since its loading; initialize the counter using deployment descriptor
and print today’s date.

Java Code: -

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
@WebServlet("/Practical19")
public class Practical19 extends HttpServlet {
private static final long serialVersionUID = 1L;
int count;
public void init() {
count=0;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Date date = new Date();
response.setContentType("text/html");
count++;
PrintWriter out = response.getWriter();
out.println("<html><title>"+"Access
Counter"+"</title><body><center><h1 style='padding-top: 300px;'>
You have reloaded this page <span style='color: blue;'>"+count+"
</span> times.</h1></center> <center> <h2>Date and Time : "+
date.toString()+ "</h2> </center></body></html>");
}

56

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Output:

Grade: ____________ Sign:_____________


57

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Practical No.:20 Date: / /

Aim:

Develop a simple Servlet program which present a set of choice for Colors and display
the selected color.

Java Code: -

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Practical20")
public class Practical20 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String color = request.getParameter("color");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<hmtl><body bgcolor=\""+color+"\">
<center><h1 style='padding-top: 300px;'>Selected Color
:"+color+"</h1></center></body></html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}

58

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

HTML Code: -

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Practical 20</title>
</head>
<body>
<center>
<h2 style='padding-top: 250px;'>Choose a Color :</h2>
<form action="Practical20" method="POST">
<input type="radio" name="color" value="Red"> RED<br>
<input type="radio" name="color" value="Green">
GREEN<br>
<input type="radio" name="color" value="Blue"> BLUE<br>
<input type="radio" name="color" value="Yellow">
YELLOW<br>
<input type="radio" name="color" value="Pink">
PINK<br><br>
<input type="submit" style="background: white;"
value="PROCEED">
</form>
</center>
</body>
</html>

Output:

59

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Grade: ______________ Sign:______________

60

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Practical No.:21 Date: / /

Aim:

Create a web form which processes Servlet and demonstrates use of cookies and
sessions.

Java Code (Practical_21): -

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/Practical21")

public class Practical21 extends HttpServlet {


private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

Cookie UserName = new Cookie("Uname",request.getParameter("uname"));


Cookie Pass = new Cookie("Pass",request.getParameter("pass"));

UserName.setMaxAge(10);
Pass.setMaxAge(10);

response.addCookie(UserName);
response.addCookie(Pass);

HttpSession session=request.getSession();

session.setAttribute("uid",1);
response.setContentType("text/html");

PrintWriter out = response.getWriter();


out.println("<html><body> <center><h2>Hello User : <p style='color:red'>
"+request.getParameter("uname")
+"</p></h2><form action='Practical21_1'><input type='submit' "
+ "value='Next'></form></center> </body></html>");
}
61

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
doGet(request, response);
} }
Java Code (Practical_21_1): -

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

@WebServlet("/Practical21_1")

public class Practical21_1 extends HttpServlet {


private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

HttpSession session=request.getSession();
Cookie[] cookie = request.getCookies();
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<body>");
if(cookie!=null) {
out.println("<center><h2>Welcome to the Next Page : <p
style='color:red'>"+cookie[0].getValue()
+"</p><br> Your Pass: <p
style='color:red'>"+cookie[1].getValue()
+"</p></h2><br><h3>Your ID is
:"+session.getAttribute("uid")+"</h3></center>");
}
else {
out.println("<h2>Cookie Not Found</h2>");
}
out.println("</body>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}

62

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

HTML Code: -

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Practical 21</title>
</head>
<body>
<center>
<form action="Practical21" style="padding-top:
300px;" method="POST">
Username : <input type="text"
name="uname"><br><br>
Password : <input type="text"
name="pass"><br><br>
<input type="submit" style="background:
white;" value="Submit">
</form>
</center>
</body>
</html>

Output:

63

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Grade: __________________ Sign:_______________


64

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

UNIT-5
JSP

65

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Practical No.:22 Date: / /

Aim:

Develop a simple JSP program to display Name, System date and grade of a student by
accepting the marks of five subjects. Write a simple JSP program to display Name, System
date and grade of a student by accepting the marks of five subjects.

HTML Code: -

<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Practical 22</title>
</head>
<style>
body{
min-height: 100vh;
overflow-y: hidden;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
form{
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
gap: .4rem;
}
input{
padding: 10px;
width: 30rem;
font-size: 1rem;
}
input[type="submit"]{
66

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

margin-top: 10px;
}
</style>
<body>
<h1><u>Student Information</u></h1>
<div class="container">
<form action="practical22.jsp">
<input type="text" name="name" placeholder="Enter Your Name">
<input type="number" name="sub1" placeholder="Subject 1's Marks">

<input type="number" name="sub2" placeholder="Subject 2's Marks">


<input type="number" name="sub3" placeholder="Subject 3's Marks">
<input type="number" name="sub4" placeholder="Subject 4's Marks">
<input type="number" name="sub5" placeholder="Subject 5's Marks">
<input type="submit" value="Proceed">
</form>
</div>
</body>
</html>

Prac22.jsp: -

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"


pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Practical 22</title>
</head>
<body style='padding-top: 250px; font-size: 20px;'>
<%
String name = request.getParameter("name");
int sub1 = Integer.parseInt(request.getParameter("sub1"));
int sub2 = Integer.parseInt(request.getParameter("sub2"));
int sub3 = Integer.parseInt(request.getParameter("sub3"));
int sub4 = Integer.parseInt(request.getParameter("sub4"));
int sub5 = Integer.parseInt(request.getParameter("sub4"));

float avg = (sub1 + sub2 + sub3 + sub4 + sub5 )/ 5;


java.util.Date date = new java.util.Date();
if(avg >= 90){
67

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

out.println("<center >");
out.println("Your Grade: AA");
out.println("</br>Current Date and Time " + date);
out.println("</center>");
}else if (avg >= 80 && avg < 90){
out.println("<center>");
out.println("Your Grade: AB");
out.println("</br>Current Date and Time " + date);
out.println("</center>");
}else if (avg >= 70 && avg < 80){
out.println("<center>");
out.println("Your Grade: BB");
out.println("</br>Current Date and Time " + date);
out.println("</center>");

}else if (avg >= 50 && avg < 70){


out.println("<center>");
out.println("Your Grade: CC");
out.println("</br>Current Date and Time " + date);
out.println("</center>");
}else if (avg >= 33 && avg < 50){
out.println("<center>");
out.println("Your Grade: DD");
out.println("</br>Current Date and Time " + date);
out.println("</center>");
}else{
out.println("<center>");
out.println("Your Grade: FF");
out.println("</br>Result showing on " + date);
out.println("</center>");
}
%>
</body>
</html>

68

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Output:

69

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Grade:_______________ Sign:_______________

70

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Practical No.:23 Date: / /

Aim:

Develop a simple JSP program for user login form with static and dynamic database.

(Static)

HTML Code: -

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="preconnect" href="https://fonts.googleapis.com">
<title>Login Page</title>

</head>

<body>
<div class="container" style="align-items: center;">
<center><table>
<form action="Practical23_Static.jsp" method="POST">
<tr>
<td>
<label for="username">Username: </label>
</td>
<td>
<input type="text" name="username" placeholder="Enter
Username" required>
</td>
</tr><br>
<tr>
<td>
<label for=" password">Password: </label>
</td>
<td>
<input type="password" required name="password"
placeholder="Enter Password">
</td>
</tr>
<br>
71

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

<tr>
<td colspan='2' style="text-align: center;">
<input type="submit" value="Submit" class='btn'>
</td>

</tr>
</form>
</table></center>
</div>
</body>

Prac23_1.jsp Code: -

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"


pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%! String username,password; %>
<%
username = request.getParameter("username");
password = request.getParameter("password");
if(username.equals("Chaitanya") &&
password.equals("Abcd1234")){
out.println("<h1 style='text-align: center;'>Welcome " +
username + "</h1>");
}else{
out.println("<h1 style='text-align: center;'>Wrong Username
or Password</h1>");
}
%>
</body>
</html>

72

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Output:

73

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

(Dynamic): -

HTML Code: -

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="preconnect" href="https://fonts.googleapis.com">
<title>Login Page</title>
</head>

<body>
<div class="container">
<table>
<form action="Practical23_Dynamic.jsp" method="POST">
<tr>
<td>
<label for="username">Username: </label>
</td>
<td>
<input type="text" name="username" placeholder="Enter
Username" required>
</td>
</tr>
<tr>
<td>

74

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

<label for=" password">Password: </label>


</td>
<td>
<input type="password" required name="password"
placeholder="Enter Password">
</td>
</tr>
<tr>
<td colspan='2' style="text-align: center;">
<input type="submit" value="Submit" class='btn'>
</td>
</tr>
</form>
</table>
</div>
</body>
</html>

Prac23_2.jsp: -

<%@ page language="java" import="java.sql., java.io., java.util.*"


contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%! String DB_URL,USERNAME,PASSWORD,userName,password,fetchedPass; %>
75

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

<%
DB_URL = "jdbc:mysql://localhost:3307/jdbcdemo";
USERNAME = "root";
PASSWORD = "admin";
userName = request.getParameter("username");
password = request.getParameter("password");
try{
Connection cn = null;
Statement stmt = null;
Class.forName("com.mysql.cj.jdbc.Driver");
cn = DriverManager.getConnection (DB_URL,USERNAME, PASSWORD);
stmt= cn.createStatement();
String query = "Select password from userdata where name = '" + userName +
"'";

ResultSet rs = stmt.executeQuery(query);
rs.next();
fetchedPass = rs.getString("password");
if(password.equals(fetchedPass)){
out.println("<h1>Welcome " + userName + "</h1>");
}else{
out.println("<h1>Wrong Username or Password</h1>");
}
}
catch(Exception e){
out.println(e);
}
%>
</body>
</html>

76

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Output:

Grade: ________________ Sign: _______________

77

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Practical No.:24 Date: / /

Aim:

Develop a JSP program for user registration and then control will be transfer to second
page.

HTML Code: -

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css2?family=Baloo+Bhai+2&display=swap"
rel="stylesheet">
<title>Registration Form</title>
</head>

<body>
<header>
</header>
<div class="body">
<div class="container">
<form action="Practical24.jsp" method="GET">
<table width="600px" align="center">
<tr>
<td class="label">Full Name: </td>
<td><input type="text" name="name" placeholder="Full Name"
autocomplete="on" required
autofocus></td>
</tr>

<tr>
<td class="label">Password: </td>
78

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

<td><input type="password" name="password"


placeholder="Password" required></td>
</tr>

<tr>
<td class="label">Confirm Password: </td>
<td><input type="password" name="cpassword"
placeholder="Confirm Password" required></td>
</tr>

<tr>
<td colspan="2" align="center">
<input type="submit" name="submit" value="Submit"
class="btn">
</td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>

Prac24.jsp: -

<%@ page language="java" import="java.sql., java.io., java.util.*"


contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
79

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

</head>
<body>
<%! String name,password; %>
<%
Connection cn;
PreparedStatement pstmt;

try{
Class.forName("com.mysql.cj.jdbc.Driver");
cn = DriverManager.getConnection("jdbc:mysql://localhost:3307/
jdbcdemo","root","admin");
name = request.getParameter("name");
password = request.getParameter("password");
String query = "insert into student values(?,?)";
pstmt = cn.prepareStatement(query);
pstmt.setString(1,id);
pstmt.setString(2,name);
pstmt.setString(3,password);

pstmt.execute();
out.println("<h1>You have been registered successfully</h1>");
}
catch(Exception e){
out.println(e);
}
%>
</body>
</html>

80

Downloaded by Dhshs Hshshs ([email protected])


lOMoARcPSD|47642596

Output:

Grade: ________________ Sign: _______________


81

Downloaded by Dhshs Hshshs ([email protected])

You might also like