0% found this document useful (0 votes)
477 views61 pages

Advanced Java Lab Guide

This document appears to be a lab manual for an Advanced Java Programming course. It contains instructions for 13 programming assignments involving applets, GUI applications, databases, servlets, JSP, and more. The first assignment asks students to create an applet that draws a circle of radius 100 pixels in the center of a 500x300 applet, displaying their name inside the circle. The second assignment instructs students to draw 10 red circles in a vertical column in the center of the applet. The document provides no solutions, only listing the assignment topics and sometimes brief descriptions of the required programs.

Uploaded by

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

Advanced Java Lab Guide

This document appears to be a lab manual for an Advanced Java Programming course. It contains instructions for 13 programming assignments involving applets, GUI applications, databases, servlets, JSP, and more. The first assignment asks students to create an applet that draws a circle of radius 100 pixels in the center of a 500x300 applet, displaying their name inside the circle. The second assignment instructs students to draw 10 red circles in a vertical column in the center of the applet. The document provides no solutions, only listing the assignment topics and sometimes brief descriptions of the required programs.

Uploaded by

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

Advanced JAVA-3360701

SIR BHAVSINHJI POLYTECHNIC INSTITUTE


BHAVNAGAR
DEPATMENT OF INFORMATION TECHNOLOGY
(AFFILIATED WITH GUJARAT TECHNOLOGICAL UNIVERSITY)

ajp LAB MANUAL


ADVANCED JAVA PROGRAMMING
Subject Code: 3360701
Semester: 6th

Guided by
A. S. Gohil

Name: Aasti Singh


Enrollment No: 166490316109
Div: IT 6B Batch: 2020-2021

1
Advanced JAVA-3360701

INDEX

Name: Aasti Singh


Enrollment No: 166490316109
Div: IT 6B Batch: 2020-2021

2
Advanced JAVA-3360701

No. P P D S R
r a a i e
a g t g m
c e e n a
t r
i n k
c o
a
l
s

1 D
e
v
e
l
o
p

a
n

a
p
p
l
e
t

t
h
a
t

d
r
a
w
s

c
i
r
c
l
e
.
T

3
Advanced JAVA-3360701

h
e

d
i
m
e
n
s
i
o
n

o
f

t
h
e

a
p
p
l
e
t

s
h
o
u
l
d

b
e

5
0
0

3
0
0

p
i

4
Advanced JAVA-3360701

x
e
l
s
.

T
h
e

c
i
r
c
l
e

s
h
o
u
l
d

b
e

c
e
n
t
e
r
e
d

i
n

t
h
e

a
p
p
l
e
t
a

5
Advanced JAVA-3360701

n
d

h
a
v
e

r
a
d
i
u
s

o
f

1
0
0

p
i
x
e
l
s
.

D
i
s
p
l
a
y

y
o
u
r

n
a
m
e
c

6
Advanced JAVA-3360701

e
n
t
e
r
e
d

i
n

c
i
r
c
l
e
.

u
s
i
n
g

d
r
a
w
O
v
a
l
(
)

m
e
t
h
o
d
)

7
Advanced JAVA-3360701

2 D
r
a
w

t
e
n

r
e
d

c
i
r
c
l
e
s

i
n

v
e
r
t
i
c
a
l

c
o
l
u
m
n

i
n

t
h
e

c
e
8
Advanced JAVA-3360701

6 D
e
v
e
l
o
p

a
n

a
p
p
l
e
t

t
h
a
t

u
s
e
s

t
h
e

m
o
u
s
e

l
i
s
t
e
n
e
r
,

w
h
i
9
Advanced JAVA-3360701

7 D
e
v
e
l
o
p

p
r
o
g
r
a
m

t
h
a
t

h
a
s

o
n
l
y

o
n
e

b
u
t
t
o
n

i
n

t
h
e
f

10
Advanced JAVA-3360701

r
a
m
e
,

c
l
i
c
k
i
n
g

o
n

t
h
e

b
u
t
t
o
n

c
y
c
l
e
s

t
h
r
o
u
g
h

t
h
e

c
o

11
Advanced JAVA-3360701

l
o
r
s
:

r
e
d
-
>
g
r
e
e
n
-
>
b
l
u
e

a
n
d

s
o

o
n
.

O
n
e

c
o
l
o
r

c
h
a
n
g

12
Advanced JAVA-3360701

p
e
r

c
l
i
c
k
.
(
u
s
e

g
e
t
B
a
c
k
G
r
o
u
n
d
(
)

m
e
t
h
o
d

t
o

g
e
t

t
h

13
Advanced JAVA-3360701

c
u
r
r
e
n
t

c
o
l
o
r
)

14
Advanced JAVA-3360701

8 Develop an program that contains three


check boxes and 30 x 30 pixel canvas.The
three checkboxes should be labeled “Red”,
“Green”,”Blue”. The selection of the
check boxes determine the color of the
canvas. For example, if the user selects
both “Red” and “Blue”, the canvas should
be purple.

9 Create an application that displays a frame


with a menu bar. When a user selects any
menu or menu item, display that selection
on a text area in the center of the frame

10 Develop a program that draws two sets of


ever-decreasing rectangles one in outline
form and one filled alternately in black
and white

11 Develop a database application that uses


any JDBC driver

12 Develop a Graphical User Interface that


performs the following SQL operations: a)
Insert b) Delete c) Update.

13 Develop a program to present a set of


choice for user to select a product and
display the price of product.

14 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.

15 Create a web form which processes servlet


and demonstrates use of cookies and
sessions

16 Develop a simple JSP program for user


registration and then control will be
transfer it into second page.

17 Develop a simple JSP program for user


login form with static and dynamic
database

18 Develop a JSP program to display the


grade of a student by accepting the marks
of five subjects

15
Advanced JAVA-3360701

Practical – 1
Aim: Develop 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. ( using drawOval() method).

Ans:

import java.awt.*; import


java.applet.*;
/*<applet code= "Applet_Example_2.class" width = "500" height= "500" >
</applet>*/
public class Applet_Example_2 extends Applet
{ public void paint(Graphics g)
{
g1.drawOval(150,50,200,200);
g.drawString("Singh Aasti",215,150);
}
}

Output:

16
Advanced JAVA-3360701

Practical – 2

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

Import java.awt.*;
import java. Applet.*;
/*<applet code= "SimpleApplet.class" width = "500" height= "500" >
</applet>*/

Public class Applet_Example_3 extends Applet


{ Public void paint (Graphics
g)
{
g.setColor (Color.RED); int
x=230; int y=0; for (int i=1;
i<=10; i++)
{
g.fillOval(x,y,40,40); y=y+50;
}
}
}
Output:

17
Advanced JAVA-3360701

Practical – 3

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

Ans:

import java.applet.*; import


java.awt.*;

/*<applet code= "SimpleApplet.class" width = "500" height= "500" >


</applet>*/

public class Applet_Example_4 extends Applet


{ public void paint(Graphics g)
{ int width=40; int
height=70;
g.setColor(Color.BLUE);
g.drawRect(30,125,240,70); for(int
i=1;i<24;i++)
{
try
{
Thread.sleep(1000);
} catch(InterruptedException e)
{
}
g.fillRect(30,125,width,height); width =
width +40;
}
}
}
Output:

18
Advanced JAVA-3360701

19
Advanced JAVA-3360701

Practical – 4

Aim: Develop 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.

Ans:

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


import java.awt.event.MouseEvent; import
java.awt.event.MouseMotionListener;

public class Applet_Example_5 extends Applet implements MouseMotionListener


{
static int x,y; public
void init()
{ addMouseMotionListener(this);
} public void paint(Graphics g)
{
g.drawString("X = " + x + ", Y = " + y , 15,15);
g.drawRect( x, y, 10, 10 );
}

@Override public void mouseDragged(MouseEvent


arg0) { // TODO Auto-generated method stub
x=getX(); y=getY(); repaint();
} @Override public void
mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub

x=arg0.getX();
y=arg0.getY(); repaint();
}
}

Output:

20
Advanced JAVA-3360701

P
r
a
c
t
i
c
a
l

Aim: Develop an applet that contains one button. Initialize the label on the button to
“start”, when the user presses the button, which changes the label between these two
values each time the button is pressed.

21
Advanced JAVA-3360701

Ans:

import java.applet.*; import java.awt.*; import java.awt.event.*; public class


Applet_Example_6 extends Applet implements ActionListener
{
Button b; public void
init()
{ b = new Button("Start");
b.addActionListener(this); add(b);
} public void paint(Graphics g)
{ } public void actionPerformed(ActionEvent ae)
{
String label = b.getLabel(); if(label.compareTo("Start")==0)
{
b.setLabel("Stop"); }

else
{
b.setLabel("Start");
}
}
}
Output:

22
Advanced JAVA-3360701

Practical – 6
Aim: Develop an applet that uses the mouse listener, which overrides only two
methods, which are mousePressed and mouseReleased.

Ans:

import java.applet.*; import


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

/*<applet code = "Applet_Example_7.class" width = "500" height= "300" >


</applet> */ public class Applet_Example_7 extends Applet implements

MouseListener{

static String str;


Button b; public void
init()
{ b = new Button("Click here");
b.addMouseListener(this); add(b);
} public void paint(Graphics g)
{
g.drawString(str, 40, 50);
}
@Override public void mouseClicked(MouseEvent
arg0) { }
@Override public void mouseEntered(MouseEvent
arg0) { }

23
Advanced JAVA-3360701

@Override public void mouseExited(MouseEvent


arg0) { }
@Override public void mousePressed(MouseEvent
arg0) {
str = "Button is Pressed"; repaint();
}

@Override public void mouseReleased(MouseEvent


arg0) { // TODO Auto-generated method stub

str = "Button is Released"; repaint();


}

Output:

24
Advanced JAVA-3360701

Practical – 7
Aim: Develop 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 color changes per
click.(use getBackGround() method to get the current color) Ans:

import java.awt.*; import


java.awt.event.*;

public class JAVA_Practical_7 implements ActionListener


{
Frame f; Button b; public
JAVA_Practical_7()
{ f = new Frame("Frame_Example") ; b= new
Button("Change color");
b.setBackground(Color.RED);
b.addActionListener(this);
f.setLayout(new FlowLayout());
f.add(b);
f.setSize(300,300);
f.setVisible(true); }
public static void main(String[] args) { // TODO
Auto-generated method stub new
JAVA_Practical_7();
}

25
Advanced JAVA-3360701

@Override public void actionPerformed(ActionEvent


arg0) { // TODO Auto-generated method stub
if(b.getBackground() == Color.RED)
{
b.setBackground(Color.GREEN);
}
else if(b.getBackground() == Color.GREEN)
{
b.setBackground(Color.BLUE); }
else
{
b.setBackground(Color.RED);
}
}

Output:

26
Advanced JAVA-3360701

27
Advanced JAVA-3360701

Practical – 8
Aim: Develop an program that contains three check boxes and 30 x 30 pixel
canvas.The three checkboxes should be labeled “Red”, “Green”,”Blue”. The selection
of the check boxes determine the color of the canvas. For example, if the user selects
both “Red” and “Blue”, the canvas should be purple.

Ans:

import java.awt.*; import


java.awt.event.*;
public class JAVA_Practical_8 implements ItemListener{

Frame f;
Checkbox c1;
Checkbox c2;
Checkbox c3; Canvas c;
int red = 0,blue=0,green=0;

JAVA_Practical_8()
{
f = new Frame("Practical - 8") ; c1
= new Checkbox("Red"); c2 = new
Checkbox("Green"); c3 = new
Checkbox("Blue");
c1.addItemListener(this);
c2.addItemListener(this);
c3.addItemListener(this);

c = new Canvas(); c.setSize(30,30);


c.setBackground(Color.GRAY);
f.setLayout(new FlowLayout());

f.add(c1);
f.add(c2);
f.add(c3);
f.add(c);
f.setSize(300,300);
f.setVisible(true);
}

28
Advanced JAVA-3360701

public static void main(String[] args) { // TODO Auto-


generated method stub new
JAVA_Practical_8();
}

@Override public void itemStateChanged(ItemEvent


arg0) { // TODO Auto-generated method stub
if(c1.getState()) red = 255;
else red = 0;
if(c2.getState()) green =
255;
else green = 0;
if(c3.getState()) blue = 255;
else blue = 0;
Color clr = new Color(red,green,blue); c.setBackground(clr);
}
}

Output:

29
Advanced JAVA-3360701

30
Advanced JAVA-3360701

Practical – 9

Aim: Create an application that displays a frame with a menu bar. When a user
selects any menu or menu item, display that selection on a text area in the center of the
frame Ans:

import java.awt.*; import java.awt.event.*; public class

JAVA_Practical_9 implements ActionListener{

Frame f; Label l; public

JAVA_Practical_9()

{ f = new Frame("Frame 2") ; l= new

Label("Select Menu.");

MenuBar mb = new MenuBar();

Menu mn = new Menu("File");

MenuItem mi1 = new MenuItem("New");


mi1.addActionListener(this); MenuItem mi2 =

new MenuItem("Open");

mi2.addActionListener(this); MenuItem mi3 =

new MenuItem("Save");

mi3.addActionListener(this);

MenuItem mi4 = new MenuItem("Exit");

Mi4.addActionListener(this); mn.add(mi1);

mn.add(mi2); mn.add(mi3);

mn.add(mi4); mb.add(mn);

f.setMenuBar(mb);

f.setLayout(new FlowLayout());

31
Advanced JAVA-3360701

f.add(l);

f.setSize(300,300);

f.setVisible(true); }

public static void main(String[] args) { // TODO

Auto-generated method stub new

JAVA_Practical_9();

@Override public void

actionPerformed(ActionEvent ae) { // TODO Auto-

generated method stub if(ae.getSource() instanceof

MenuItem)

{
l.setText(ae.getActionCommand());

Output:

32
Advanced JAVA-3360701

Sign:

Date:

33
Advanced JAVA-3360701

Practical – 10
Aim: Develop a program that draws two sets of
ever-decreasing rectangles one in outline form and one filled alternately in black and
white.

Ans:
import java.awt.*; public class JAVA_Practical_10

extends Canvas{

Frame f;
JAVA_Practical_10()
{ f = new Frame("Practical - 10");

f.setSize(500,300);
f.setVisible(true);
f.setLayout(new FlowLayout());
setSize(400,200); setVisible(true);

f.add(this); }

34
Advanced JAVA-3360701

public static void main(String[] args) { // TODO

Auto-generated method stub new

JAVA_Practical_10();

} public void paint(Graphics g)

{
g.setColor(Color.RED);
g.fillRect(0,0,400,200); int

x=0; int y=0;

int width=400; int

height=200; int i=0;

while(i<19)

{ width=width-10;

height=height-10;

x=(400-width)/2; y=(200-height)/2;

if(i%2==1)
g.setColor(Color.WHITE);
else
g.setColor(Color.BLACK);
g.fillRect(x,y,width,height); i+

+; try{

Thread.sleep(1000);
} catch(InterruptedException e){

}
}
}
}
Output:

35
Advanced JAVA-3360701

36
Advanced JAVA-3360701

Practical – 11

Aim: Develop a database application that uses any JDBC driver.


Ans:

import java.sql.*;
public class DbDemo
{
public static void main(String[] args)
{
Connection conn; Statement
stmt;
try
{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Trying to connect with Database Server..");
conn=DriverManager.getConnection("jdbc:mysql://localhost/",
"root", "");
System.out.println("ConnectionEstablished Successfully...");
System.out.println("Trying to create Database..");
stmt = conn.createStatement();
String sql = "CREATE DATABASE STUDENTDB";
stmt.executeUpdate(sql);
System.out.println("Database Created Successfully...");
System.out.println("Trying to create Table..");

sql = "CREATE TABLE STUDINFO(rollno int, name


varchar(50),mobile int)"; stmt.executeUpdate(sql);
System.out.println("Table Created Successfully...");

conn.close(); stmt.close();
}
catch(Exception e)
{
System.out.println("Exception Caught." + e); }
}
}

37
Advanced JAVA-3360701

Output:

Practical – 12
Aim: Develop a Graphical User Interface that performs the following SQL
operations: a) Insert b) Delete c) Update.

Ans:
import java.sql.*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class DatabaseDemo extends Frame implements WindowListener,ActionListener {

JTextField nm,enno,spi,delEno;
JLabel lblnm,lbleno,lblspi,lbldelno;
JButton sbmt,rtrv,dltRcrd,dlt;
JPanel pn1,pn2,pn3,pn4,pn5;
DatabaseDemo()
{ setSize(500,500); setLayout(new

FlowLayout()); nm = new

JTextField(10); enno = new

JTextField(10); spi = new

38
Advanced JAVA-3360701

JTextField(10); delEno = new

JTextField(10); lblnm = new

JLabel("Name : "); lblspi = new

JLabel("SPI : ");

lbleno = new JLabel("Enrollment No. : ");

sbmt = new JButton("Insert"); rtrv = new

JButton("Retrive"); dltRcrd = new

JButton("Delete Records"); dlt = new

JButton("Delete"); pn1 = new JPanel();

pn1.setLayout(new FlowLayout());

pn1.add(lblnm); pn1.add(nm);

pn1.setVisible(true); pn2 = new JPanel();

pn2.setLayout(new FlowLayout());

pn2.add(lbleno); pn2.add(enno);

pn2.setVisible(true); pn3 = new JPanel();

pn3.setLayout(new FlowLayout());

pn3.add(lblspi); pn3.add(spi);

pn3.setVisible(true); pn4 = new JPanel();

pn4.setLayout(new FlowLayout());

pn4.add(sbmt); pn4.add(rtrv);

pn4.add(dltRcrd); pn4.setVisible(true); pn5 =

new JPanel();

pn5.setLayout(new FlowLayout()); pn5.add(new

Label("Enter En. No. To Delete : ")); pn5.add(delEno);

pn5.add(dlt); pn5.setVisible(false); add(pn1);

add(pn2); add(pn3); add(pn4); add(pn5);

39
Advanced JAVA-3360701

setVisible(true); dlt.addActionListener(this);

dltRcrd.addActionListener(this);

sbmt.addActionListener(this);

rtrv.addActionListener(this);

addWindowListener(this);

} public void windowActivated(WindowEvent we){} public void

windowDeactivated(WindowEvent we){} public void

windowIconified(WindowEvent we){} public void

windowDeiconified(WindowEvent we){} public void

windowOpened(WindowEvent we){} public void

windowClosed(WindowEvent we){} public void

windowClosing(WindowEvent we){System.exit(0);} public void

actionPerformed(ActionEvent ae)

{
String acmd = ae.getActionCommand();

DbConnect dbc = new DbConnect(); if(acmd ==

"Insert")

{
String arg[] = new String[3]; arg[0] =

nm.getText().toString(); arg[1] =

enno.getText().toString(); arg[2] =

spi.getText().toString(); dbc.insert("insert into

stud_info values(?,?,?)",arg);

System.out.println("Records Inserted Successfully....");

} if(acmd == "Retrive")

{ dbc.retriveRecords();

} if(acmd == "Delete")

40
Advanced JAVA-3360701

String delno = delEno.getText().toString(); dbc.deleteRecords(delno);

System.out.println("Selected Record Sucessfully Deleted.....");

pn5.setVisible(false);

} if(acmd == "Delete Records")

pn5.setVisible(true);

}
public static void main(String s[])

{ new DatabaseDemo();

} } class

DbConnect

Connection cnn;

PreparedStatement pstmt;

Statement stmt; ResultSet rs; static

final String USER = "root"; static

final String PASS = ""; static final

String DB_URL =

"jdbc:mysql://localhost:3306/Students

";

DbConnect()

try

41
Advanced JAVA-3360701

Class.forName("com.mysql.jdbc.Driver"); cnn

=DriverManager.getConnection(DB_URL,USER,PASS);

} catch(SQLException se)

{ se.printStackTrace();

} catch(ClassNotFoundException cnfe)

cnfe.printStackTrace();

public void insert(String qry,String arg[])

try

{ pstmt = cnn.prepareStatement(qry);

pstmt.setString(1,arg[0]);

pstmt.setInt(2,Integer.parseInt(arg[1]));

pstmt.setString(3,arg[2]);

pstmt.executeUpdate();

} catch(SQLException se)

{ se.printStackTrace();

} } public void

retriveRecords()

try

{ stmt = cnn.createStatement(); rs = stmt.executeQuery("select

* from stud_info");

42
Advanced JAVA-3360701

System.out.println("----DATABASE RECORDS----");
System.out.println("Name | En_No | SPI");

System.out.println("---------------");

while(rs.next())
{
System.out.println(rs.getString(1)+" | "+rs.getInt(2)+" | "+rs.getString(3));
}}

catch(SQLException se)

{ se.printStackTrace();

} } public void deleteRecords(String

delno)

{
try
{ pstmt = cnn.prepareStatement("delete from stud_info where en_no=?");

pstmt.setInt(1,Integer.parseInt(delno)); pstmt.executeUpdate();

} catch(SQLException se)

{ se.printStackTrace();

}
}
}
Output:

43
Advanced JAVA-3360701

44
Advanced JAVA-3360701

45
Advanced JAVA-3360701

Practical – 13

Aim: Develop a program to present a set of choice for user to select a product and
display the price of product.

Ans:

import java.awt.*; import java.awt.event.*; class Demo


extends Frame implements ItemListener
{
Choice c;
Label lbl;
Demo()
{ setSize(300,300); setLayout(new
FlowLayout()); c = new Choice();
lbl = new Label("Price : ");
c.add("Samsung TV");
c.add("MI Mobile");
c.add("IPhone");
c.addItemListener(this);
add(c); add(lbl);
setVisible(true);
}

public void itemStateChanged(ItemEvent ae)


{
String s = c.getSelectedItem(); if(s.equals("Samsung
TV"))
{ lbl.setText("Price : 30000");
} if(s.equals("MI Mobile"))
{ lbl.setText("Price : 10000");
} if(s.equals("IPhone"))
{
lbl.setText("Price : 100000");
}
}

public static void main(String s[])


{ new Demo();
}

46
Advanced JAVA-3360701

Output:

Practical – 14
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. Ans:

 HitCounter.java:
import java.io.IOException; import
java.io.PrintWriter; import
javax.servlet.*;
import javax.servlet.http.HttpServlet; import
javax.servlet.http.HttpServletRequest; import
javax.servlet.http.HttpServletResponse;

public class HitCounter extends HttpServlet


{ int c; public void
init()
{
ServletConfig s=getServletConfig();
c=Integer.parseInt(s.getInitParameter("counter"));
}

47
Advanced JAVA-3360701

public void doGet(HttpServletRequest req, HttpServletResponse res) throws


ServletException, IOException
{ c++;
PrintWriter out = res.getWriter();
out.println("Total Hit: " + c);
}
}

 Web.xml:
<servlet>
<servlet-name>pagecounter</servlet-name>
<servlet-class>HitCounter</servlet-class>
<init-param>
<param-name>counter</param-name>
<param-value>0</param-value>
</init-param>
</servlet>
Output:

48
Advanced JAVA-3360701

Practical – 15
Aim :- Create a web form which processes servlet and demonstrates use of sessions.

Ans:

 login.html:
<html>
<head> <title> Login Page </title>
</head>
<body>
<form action="loginservlet" method="post" >
<table>
<tr>
<td>User Name: </td>
<td> <input type = "text" name = "name"> </td>
</tr>
<tr>
<td>Password: </td>
<td> <input type = "password" name = "pass"> </td>
</tr>
<tr>
<td> </td>
<td> <input type = "submit" name = "submit" value = "SUBMIT"> </td> </tr>
</form>
</body>
</html>
 loginservlet.java:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class loginservlet extends HttpServlet
{
protected void doPost (HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException
{
res.setContentType("text/html");

49
Advanced JAVA-3360701

PrintWriter out=res.getWriter();
String name=req.getParameter("name"); String
password=req.getParameter("pass");
if(name.equals("Aasti") && password.equals("Singh"))
{
HttpSession session=req.getSession(); session.setAttribute("Name", name);
out.println("<br/><h1>Welcome :"+ name + "</h1> "); out.println("<br/> <a
href=profile> Click here </a>");
}

else
{ out.println("<h1>You Have entered Wrong Password </h1>");
out.println("<br/> <a href=login.html> Click here </a> to Login"); }
}
}
 Profile.java:
import java.io.IOException; import
java.io.PrintWriter; import
javax.servlet.ServletException; import
javax.servlet.http.HttpServlet; import
javax.servlet.http.HttpServletRequest; import
javax.servlet.http.HttpServletResponse; import
javax.servlet.http.HttpSession; public class profile
extends HttpServlet
{
protected void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{ res.setContentType("text/html");
PrintWriter out=res.getWriter();
HttpSession session=req.getSession(false); if(session!=null)
{
String name=(String) session.getAttribute("Name"); out.println("Welcome," +
name);
out.println("<h1>This is Session Program. </h1> "); out.println("<a

href=logoutservlet> Click here to Logout </a>");

}
else
{ out.println("<br> <h1> Plz Login First</h1>"); out.println("<br/> <a
href=login.html> Click here </a> to Login");
}
}

50
Advanced JAVA-3360701

 Logoutservlet.java :
import java.io.IOException; import
java.io.PrintWriter; import
javax.servlet.ServletException; import
javax.servlet.http.HttpServlet; import
javax.servlet.http.HttpServletRequest; import
javax.servlet.http.HttpServletResponse; import
javax.servlet.http.HttpSession;

public class logoutServlet extends HttpServlet


{
public void doGet (HttpServletRequest req,HttpServletResponse res) throws
IOException, ServletException
{ res.setContentType("text/html"); PrintWriter
out=res.getWriter(); HttpSession
session=req.getSession();
session.invalidate();
out.println("<br> <h1>You have successfully Logout </h1>");
out.println("<br/> <a href=login.html> Click here </a> to Login"); }
}

 Web.xml:
<servlet>
<servlet-name>loginservlet</servlet-name>
<servlet-class>loginservlet</servlet-class> </servlet>

<servlet>
<servlet-name>logoutservlet</servlet-name>
<servlet-class>logoutServlet</servlet-class> </servlet>

<servlet>
<servlet-name>profile</servlet-name>
<servlet-class>profile</servlet-class> </servlet>

<servlet-mapping>
<servlet-name>profile</servlet-name>
<url-pattern>/profile</url-pattern>
</servlet-mapping>

51
Advanced JAVA-3360701

<servlet-mapping>
<servlet-name>logoutservlet</servlet-name>
<url-pattern>/logoutservlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>loginservlet</servlet-name>
<url-pattern>/loginservlet</url-pattern> </servlet-mapping>

Output:

52
Advanced JAVA-3360701

53
Advanced JAVA-3360701

Aim: Develop a

Practical – 16
Develop a simple JSP program for user registration and then control will be
transfer it into second page.

Ans:

 firstpage.jsp:

<html>

<head>

<title>Registration Page</title>

</head>

<body>

<form action="secondpage.jsp" method="post">

Enter your UserName:<input type="text" name="name"><br>

Enter your Password:<input type="password" name="pas"><br>

Enter your email:<input type="email" name="email"><br>

<input type="submit">

</form>

</body>

</html>

 Secondpage.jsp:

<html>

<head>

<title>Welcome Page</title>

</head>

54
Advanced JAVA-3360701

<body>

<% String username=request.getParameter("name");

String password=request.getParameter("pas"); String email=request.getParameter("email");

out.print(username + ", registered successfully!!! ");

%>

</body>

</html>

Output:

55
Advanced JAVA-3360701

Aim: Develop a

Practical – 17
simple JSP program for user login form with static and dynamic
database.
Ans:

login.jsp:
<html>

<head>

<title> Log in </title>

</head>

<body>

<form action="welcome.jsp" method="post"> Username:<input

type="text" name="uname"><br/> password:<input

type="password" name="pass"><br/>

<input type="submit"> </form> </body>

</html>

 welcome.jsp:
<% String username=request.getParameter("uname");

Stringpassword=request.getParameter("pass");

if(username.equals("mayur") && password.equals("thakkar"))

out.print("Welcome, " + username + "!");

} else

{ out.print("Invalid Username or Password. ")

%>

56
Advanced JAVA-3360701

Output:

57
Advanced JAVA-3360701

Aim: Develop a

Practical – 18
JSP program to display the grade of a student by accepting the
marks of five subjects.

Ans:

 input.jsp:
<html>

<head>

<title>Subject Marks</title>

</head>

<body>

<h1>Marks Entry of Semester - 6</h1>

<form action="result.jsp" method="POST">

<p>Enter AJP Marks : <input type="text" name="ajp"></p>

<p>Enter PHP Marks : <input type="text" name="php"></p>

<p>Enter WNS Marks :<input type="text" name="wns"></p>

<p>Enter ANDROID Marks :<input type="text" name="android"></p>

<p>Enter Project Marks :<input type="text" name="pro"></p>

<p><input type="submit"></p>

</form>

</body>

</html>
 result.jsp:
<%

58
Advanced JAVA-3360701

int AJP=Integer.parseInt(request.getParameter("ajp")); int

PHP=Integer.parseInt(request.getParameter("php")); int

WNS=Integer.parseInt(request.getParameter("wns")); int

ANDROID=Integer.parseInt(request.getParameter("android")); int

PROJECT=Integer.parseInt(request.getParameter("pro"));

int Total = AJP+PHP+WNS+ANDROID+PROJECT;

double avg = Total/5.0;

if(avg > =90 )

{ out.println(" your grade is: A");

} else if (avg >= 80)

out.println("your grade is: B");

} else if (avg >= 70)

{ out.println("your grade is: C");

} else if (avg >= 60)

{ out.println("your grade is: D");

59
Advanced JAVA-3360701

} else { out.println("your grade is: E");

%>

Output:

60
Advanced JAVA-3360701

Sign:

Date:

61

You might also like