All Practicals Ajava
All Practicals Ajava
LABORATORY MANUAL
Semester: VI
Government Polytechnic
Ahmedabad
_________________________________________of
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
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.
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
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
}
............
}
<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>
import java.awt.*;
import java.applet.*;
public class Hellojava extends Applet
{
public void paint(Graphics g)
{
g.drawString( “Hello java” ,10,100);
}
}
Output:
Compiling an applet :
javac Hellojava.java
Running an applet
Java Code: -
import java.applet.*;
import java.awt.*;
}
public void paint(Graphics g)
{
g.drawOval(200,100,100,100);
g.drawString("Chaitanya",222,155);
}
public void stop()
{
}
public void destroy()
{
}
}
HTML Code: -
<html>
<applet code="prac1.class" width="100" height="100"></applet>
</html>
Output:
Java Code: -
import java.applet.*;
import java.awt.*;
<html>
<applet code="prac2.class" width="100" height="100"></applet>
</html>
Output:
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()
{
}
public void destroy()
{
}
}
Html Code: -
<html>
<applet code="prac3.class" width="100" height="100"></applet>
</html>
Output:
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
Output:
11
UNIT-2
AWT
12
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.*;
13
Html Code: -
<html>
<applet code="prac5.class" width="400" height="400"></applet>
</html>
Output:
14
15
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
Output:
17
18
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
HTML Code: -
<html>
<applet code="prac7.class" width="400" height="400">
</applet>
</html>
Output:
20
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
HTML Code: -
<html>
<applet code="prac8.class" width = "400" height = "400">
</applet>
</html>
Output:
22
Java Code: -
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet;
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
25
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.*;
}
Output:
27
Html Code: -
<html>
<applet code="prac11.class" width=500 height=500></applet>
28
</html>
29
Output:
30
Java Code: -
import java.awt.*;
public prac12(){
setBackground(Color.WHITE);
setForeground(Color.black);
setLayout(null);
add(l11);
add(l1);
add(l2);
add(l3);
add(l4);
31
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
Output: -
33
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
f.add(txtname);
f.add(lblno);
f.add(txtno);
f.add(btnsubmit);
}
public static void main(String [] args){
new prac13();
}
}
Output:
35
Java Code: -
import java.awt.*;
import javax.swing.*;
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
f.add(lblname);
f.add(branch);
f.add(btnsubmit);
}
public static void main(String [] args){
new prac14();
}
}
Output: -
37
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);
f.add(lbl);
f.add(rb1);
f.add(rb2);
f.add(rb3);
f.add(btnsubmit);
}
public static void main(String[] args) {
new prac15();
}
}
Output: -
39
Unit-3
JDBC
40
41
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
}
}
}
Output: -
Java Code: -
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
44
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
Output:
46
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
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
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
Output:
1) Insert
50
2) Update
51
3) Delete
• Table
52
53
UNIT-4
Servlet
54
55
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
Output:
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
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
60
Aim:
Create a web form which processes Servlet and demonstrates use of cookies and
sessions.
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")
UserName.setMaxAge(10);
Pass.setMaxAge(10);
response.addCookie(UserName);
response.addCookie(Pass);
HttpSession session=request.getSession();
session.setAttribute("uid",1);
response.setContentType("text/html");
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")
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
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
UNIT-5
JSP
65
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
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">
Prac22.jsp: -
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>");
68
Output:
69
Grade:_______________ Sign:_______________
70
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
<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: -
72
Output:
73
(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
Prac23_2.jsp: -
<%
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
Output:
77
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
<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: -
</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
Output: