Advanced Java Practical File
Advanced Java Practical File
CERTIFICATE
This is to certify that Miss SIDDHI A. NIVALE of S.Y B.Sc. C.S. Semester IV
has completed the practical work in the Subject of Advanced Java during the
academic year 2021-22 under the guidance of Prof. Abhijeet Salvi being the
INDEX
Practical No: 1
Calculator.java file
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Calculator extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n1 = request.getParameter("n1");
String n2 = request.getParameter("n2");
String op = request.getParameter("op");
switch (op) {
case "Addition":
out.println("Answer = "+(Integer.parseInt(n1) + Integer.parseInt(n2)));
break;
case "Subtraction":
out.println("Answer = "+(Integer.parseInt(n1) - Integer.parseInt(n2)));
break;
case "multiplication":
out.println("Answer = "+(Integer.parseInt(n1) * Integer.parseInt(n2)));
break;
default:
out.println("Answer = "+(Integer.parseInt(n1) / Integer.parseInt(n2)));
break;
}
}
}
:
Output:
b. Design a simple form using HTML which takes a number as an input & perform following
operations
i. Calculate Factorial
ii. Determine Prime Number or Not.
iii. Determine Number is Armstrong or Not.
Code :
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>Practical 1A</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div><h1> PRACTICAL NO : 1 </h1></br>
<hr>
<h3>b.) Design a simple form using HTML which takes a number as an input & perform following operations</h3>
<p>
i.Calculate Factorial<br>
ii. Determine Prime Number or Not.<br>
iii. Determine Number is Armstrong or Not.
</p>
</div>
<div>
<form method="GET" action="prac_1A">
ENTER NUMBER : <input type="number" name="n">
<input type="submit" value="calculate">
</form>
</div>
</body>
</html>
prac_1A.java file
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class prac_1A extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
int n = Integer.parseInt(req.getParameter("n"));
out.println("<h2>The factorial of the given number "+ n + " is : </h2>");
int i=1,fact=1;
if(n==0)
{
out.println("0");
}
else
{
while(i<=n)
{
fact=fact*i;
i=i+1;
}
out.print(fact);
}
out.println("<br><h2>The given number is : " + n + "</h2>");
int flag=0;
for(i=2;i<=(n-1);i++)
{
if(n%2==0)
{
flag=1;
break;
}
}
if(flag==0)
{
out.print("It is a Prime number");
}
else
{
out.print("It is not a prime number,<br><br>");
}
out.println("<h2>The given no. " + n + " is Armstrong or not ?</h2><br>");
int remainder, sum = 0, temp = 0;
temp = n;
while (n > 0) {
remainder = n % 10;
sum = sum + (remainder * remainder * remainder);
n = n / 10;
}
if (sum == temp) {
out.println("It is a Armstrong number");
} else {
out.println("It is Not a Armstrong number");
}
}
}
Output :
PRACTICAL-02
Aim:
a) Using Request Dispatcher Interface create a servlet
which will validate the password entered by the user, if the
user has entered “Servlet” as password, then he will be
forwarded to Welcome servlet else the user will stay on the
index.html page and an error message will be displayed.
Code:
index.html:
login.java:
welcome.java:
Output:
visits.java:
Output:
session.java:
Output:
PRACTICAL NO : 3
Aim : a. Create a registration servlet in Java using JDBC. Accept the details such as
Username, Password, Email, and Country from the user using HTML Form and store the
registration details in the database.
b. Design database for student administration. Develop servlet(s) to perform CRUD
operations.
Index . Html
<!DOCTYPE html>
<html>
<head>
<title>Practical 3</title>
<meta charset="UTF-8">
<body>
</form>
</body>
</html>
addData.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
try{
String username,password,email,country;
req.getParameter("psd"); email=
req.getParameter("email"); country=
req.getParameter("country");
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","")
;
out.print("<h1>Connected</h1>");
ps.setString(1,username);
ps.setString(2,password); ps.setString(3,email);
ps.setString(4,country); ps.executeUpdate();
Successfully</h3>");
}
catch(Exception e){ out.println(e);
Outout
b. Design database for student administration. Develop servlet(s)
to perform CRUD operations.
CODE
studenthome.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project
Properties.
To change this template file, choose Tools | Templates and open
the template in the editor.
-->
<html>
<head>
<title>Student Administration</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
</head>
<body>
<center>
<h1>Student Administration</h1> <br><br>
<h2> <a href="StudentDetails.html">Add Stduent</a>
</h2><br>
<h2> <a href="viewStudent">View Stduent</a> </h2><br>
<h2> <a href="viewStudent">Update/Delete Stduent</a>
</h2><br>
</center>
</body>
</html>
Studentdetails.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 3.B</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
</head>
<body>
<h1>Practical 3.B</h1> <br>
<form action="addStudent" method="post">
Roll : <input type='number' name='rollno'> <br><br>
First Name: <input type='text' name='fname'> <br><br>
Last Name : <input type='text' name='lname'> <br><br>
Age : <input type='number' name='age'> <br><br>
Course : <select name="course">
<br><br>
</form>
</body>
</html>
Addstudent.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
try{
String fname,lname,course;
int rollno=Integer.parseInt(req.getParameter("rollno"));
fname= req.getParameter("fname"); lname=
req.getParameter("lname");
int age = Integer.parseInt(req.getParameter("age"));
course= req.getParameter("course");
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/use
r","root","");
out.print("<h1>Connected</h1>");
String query = "insert into studentdetails values(?,?,?,?,?)";
PreparedStatement ps = con.prepareStatement(query);
ps.setInt(1,rollno);
ps.setString(2,fname); ps.setString(3,lname);
ps.setInt(4,age); ps.setString(5,course);
ps.executeUpdate(); con.close();
out.println("<h3>data Inserted
Successfully</h3>");
}
catch(Exception e){ out.println(e);
}
}
Viewstudent.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
try{
Class.forName("com.mysql.jdbc.Driver"); Connection
con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/use
r","root","");
Statement st = con.createStatement();
String query= "select * from studentdetails order by roll_no";
out.print("<center>");
out.print("<h1>View Stdents</h1>");
out.print("<table border='1'>"
+ "<thead>"
+ "<tr>"
+ "<td>Roll NO</td>"
+ "<td>First Name</td>"
+ "<td>Last Name</td>"
+ "<td>Age</td>"
+ "<td>Course</td>"
+ "<td>Update</td>"
+ "<td>Delete</td>"
+ "</thead>"
);
ResultSet rs = st.executeQuery(query); while(rs.next()){
out.print("<tr>"
+ "<td>"+rs.getString("roll_no")+"</td>"
+ "<td>"+rs.getString("firstName")+"</td>"
+ "<td>"+rs.getString("lastName")+"</td>"
+ "<td>"+rs.getString("age")+"</td>"
+ "<td>"+rs.getString("course")+"</td>"
+ "<td>"+ "<a href='update?id=" + rs.getInt("roll_no")
+ "'> Edit </a>" +"</td>"
+ "<td>"+ "<a href='delete?id=" + rs.getInt("roll_no")
+ "'> Delete </a>" +"</td>"
+ "</form>"
);
}
}
catch(Exception e){ out.println(e);
}
}
Update.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
int id = Integer.parseInt(req.getParameter("id"));
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/use
r","root","");
Statement st = con.createStatement();
String query= "select * from studentdetails where roll_no=?";
PreparedStatement ps = con.prepareStatement(query);
ps.setInt(1,id);
// out.print("<h2>"+id+"</h2>"); out.print("<h1>Update
Details</h1>");
while(rs.next()){
out.print(""
+ "<form action='updatestudent' method='post'>\n" +
" Roll : <input type='number'
value='"+rs.getString("roll_no")+"' name='rollno'> <br><br>" +
" First Name: <input type='text'
value='"+rs.getString("firstName")+"' name='fname'> <br><br>"
+
" Last Name : <input type='text'
value='"+rs.getString("lastName")+"' name='lname'> <br><br>\n"
+
" Age : <input type='number'
value='"+rs.getString("age")+"' name='age'> <br><br>\n" +
" Course : <select name=\"course\">\n" +
" \n" +
}
catch(Exception e){
out.println(e);
}
}
Updatestudent.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
int rollno=Integer.parseInt(req.getParameter("rollno"));
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/use
r","root","");
out.print("<h1>Connected</h1>");
String query = "update studentdetails set
firstName=?,lastname=?, age=?,course=? where roll_no=?" ;
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1,fname);
ps.setString(2,lname); ps.setInt(3,age);
ps.setString(4,course); ps.setInt(5,rollno);
ps.executeUpdate();
con.close();
out.println("<h3>data data updated Successfully</h3>");
}
catch(Exception e){
out.println(e);
}
}
Delete.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
int id = Integer.parseInt(req.getParameter("id"));
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/use
r","root",""); out.print("<h1>Connected</h1>");
Statement st = con.createStatement();
String query= "delete from studentdetails where roll_no=?";
PreparedStatement ps = con.prepareStatement(query);
ps.setInt(1,id);
ps.executeUpdate();
out.print("data deleted successfully");
// RequestDispatcher rd=
req.getRequestDispatcher("viewStudent");
// rd.forward(req, res);
}
catch(Exception e){
out.println(e);
}
}
Output :
Adding Details to database
Deleting data
Output:
http://java.io
Practical 4
javax.servlet.*; import
javax.servlet.http.*; import
java.sql.*;
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/user", "root", "");
Statement st = con.createStatement();
rs = st.executeQuery(query);
rsmd.getColumnTypeName(i));
out.print("<br>");
} catch (Exception e) {
}
}
Output:
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/user",
"root", "");
out.print("<br>Product Name" +
dbmd.getDatabaseProductName());
} catch (Exception e) {
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods.
Click on the + sign on the left to edit the code.">
Output :
PRACTICAL NO : 5
Aim : a. Develop a simple JSP application to display values obtained from the use
of intrinsic objects[Implicit Objects] of various types.
b. Develop a simple JSP application to pass values from one page to another with
validations. (Name-txt, age-txt, hobbies-checkbox, email-txt, gender-radio button)
a. Develop a simple JSP application to display values obtained from the use of
intrinsic objects[Implicit Objects] of various types // out object
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<%
// out object
out.println("<br>Nu
m1 is " + num1);
out.println("<br>Nu
m2 is " + num2);
%>
</body>
</html>
Output:
// request object
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<br>
</form>
</body>
</html>
Result.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<%
age =Integer.parseInt(request.getParameter("age"));
out.print("Name : "+name);
out.print("Age : "+age);
%>
</body>
</html>
Output :
// response
Response.jsp
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<%
response.sendRedirect("https://www.google.com/");
%>
</body>
</html>
b. Develop a simple JSP application to pass values from one page to
another with validations. (Name-txt, age-txt, hobbies-checkbox, email-txt,
genderradio button)
formresult.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<br>
<br>
Hobbies : <br>
<br>
<br>
</form>
</body>
</html>
Result.jsp
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<%
Integer.parseInt(request.getParameter("age"));
{ out.println(str + ",");
%>
</body>
</html>
Output :
Practical no: 6
Code :
index.html
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
</head>
<body>
<div class="signup-form">
<h1>Registration form</h1>
<div class="input-boxes">
<div class="input-box">
</div>
<div class="input-box">
<i class="fas fa-envelope"></i>
</div>
<div class="input-box">
</div>
<center>
</center>
</div>
</form>
</div>
</body>
</html>
Reg.jsp
<%--
Document : Reg
Author : Siddhi
--%>
<%@page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<%
String email;
email = request.getParameter("email");
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/login","root","");
Statement st = con.createStatement();
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1,
Name);
ps.setString(2, email);
ps.setString(3, password);
ps.executeUpdate();
con.close();
}catch(Exception e)
{ out.println(e);
}
%>
</body>
</html>
Logform.html
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
</head>
<body>
<div class="signup-form">
<h1>Login form</h1>
<div class="input-boxes">
<div class="input-box">
</div>
<div class="input-box">
</div>
<center>
</center>
</div>
</form>
</div>
</body>
</html>
Logform.jsp
<%--
Document : Loginform
Author : Siddhi
--%>
<%@page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<%
String email =
request.getParameter("email"); String
password; password =
request.getParameter("psw");
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/login","root","");
Statement st = con.createStatement();
if(rs.next()) {
}else{ o
ut.print("login Fails");
con.close();
}catch(Exception e)
{ out.println(e);
%>
</body>
</html>
Output :
b. Create Employees table in EMP database. Perform select, insert,
update, and delete operations on the Employee table using JSP.
Code :
Emphome.html
<!DOCTYPE html>
<!--
-->
<html>
<head>
<title>Employee Administration</title>
<meta charset="UTF-8">
</head>
<body>
<center>
</center>
</body>
</html>
Addemp.html
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
</head>
<body>
<div class="signup-form">
<h1>Employee form</h1>
<div class="input-boxes">
<div class="input-box">
<option value="executive">Executive</option>
<option value="manager">Manager</option>
<option value="director">Director</option>
<option value="accountant">Accountant</option>
<option value="chief">Chief</option>
</select></div>
<div class="input-box">
<option value="female">Female</option>
<option value="male">Male</option>
</select></div>
</div>
</div>
</form>
</div>
</body>
</html>
Addemp.jsp
<%--
Document : addemp
Author : Siddhi
--%>
<%@page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<%
int empid =
Integer.parseInt(request.getParameter("empid")); String
java.sql.Date.valueOf(request.getParameter("dob"));
java.sql.Date job =
java.sql.Date.valueOf(request.getParameter("sdate")); String
Integer.parseInt(request.getParameter("bacc"));
Integer.parseInt(request.getParameter("depid"));
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/emp","root","");
Statement st = con.createStatement();
PreparedStatement ps = con.prepareStatement(query);
ps.setInt(1, empid);
ps.setString(2, ename);
ps.setDate(3, dob);
ps.setDate(4, job);
ps.setString(5, email);
ps.setInt(6, sal);
ps.setInt(7, bankac);
ps.setString(8, addr);
ps.setString(9, designation);
ps.setInt(10, depid);
ps.setString(11, gender);
ps.executeUpdate();
con.close();
}catch(Exception e)
{ out.println(e);
%>
</body>
</html>
Viewwmp.jsp
<%--
Document : viewwmp
Author : Siddhi
--%>
<%@page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<h1>Employee’s Detail</h1>
<thead>
<tr>
<th>Employee id</th>
<th>Name</th>
<th>D.O.B</th>
<th>Join date</th>
<th>Email id </th>
<th>Salary</th>
<th>Bank A/C</th>
<th>Address</th>
<th>Designation</th>
<th>Dept id</th>
<th>Gender</th>
<th colspan=\"4\">Action</th>
</tr>
</thead>
<%
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/emp","root","");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);
while(rs.next())
String email =
rs.getString("email"); int
salary = rs.getInt("salary");
String designation =
rs.getInt("depid");
);
out.println("</tbody></table><br><br>");
}catch(Exception e)
{ out.println(e);
%>
</body>
</html>
Update.jsp
<%--
Document : update
Author : Siddhi
--%>
<%@page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-
</head>
<body>
<div class="signup-form">
<%
try{
int id = Integer.parseInt(request.getParameter("eid"));
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/emp","root","");
Statement st = con.createStatement();
//
PreparedStatement ps = con.prepareStatement(query);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
<div class=\"input-boxes\">\n" +
"\n"+
"\n" +
" <div class=\"input-box\"><input type='text' value='"+rs.getString("ename")+"'
name='name'></div>\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
"\n" +
" </select></div>\n" +
"\n" +
" </select></div>\n" +
" </div>\n" +
" </div>\n" +
" </form>");
catch(Exception e){
out.println(e);
%>
</div>
</body>
</html>
Updateemp.jsp
<%--
Document : updateemp
Author : Siddhi
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<%
try{
int empid =
Integer.parseInt(request.getParameter("empid")); String
java.sql.Date.valueOf(request.getParameter("dob"));
java.sql.Date job =
java.sql.Date.valueOf(request.getParameter("sdate")); String
= Integer.parseInt(request.getParameter("bacc"));
int depid =
Integer.parseInt(request.getParameter("depid"));
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/emp","root","");
String query = "update employ set eid=?,ename=?, dob=?,job=?,email=?,
salary=?,bankac=?,addr=?, designation=?,depid=?,gender=? where eid=?" ;
PreparedStatement ps = con.prepareStatement(query);
ps.setInt(1, empid);
ps.setString(2, ename);
ps.setDate(3, dob);
ps.setDate(4, job);
ps.setString(5, email);
ps.setInt(6, sal);
ps.setInt(7, bankac);
ps.setString(8, addr);
ps.setString(9, designation);
ps.setInt(10, depid);
ps.setString(11, gender);
ps.setInt(12, empid);
ps.executeUpdate();
con.close();
catch(Exception e){
out.println(e);
%>
</body>
</html>
Delete.jsp
<%--
Document : Delete
Author : Siddhi
--%>
<%@page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<%
int id = Integer.parseInt(request.getParameter("eid"));
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/emp","root","");
Statement st = con.createStatement();
PreparedStatement ps = con.prepareStatement(query);
ps.setInt(1,id);
ps.executeUpdate();
out.print("<h3>data deleted
successfully</h3>");
catch(Exception e){
out.println(e);
%>
</body>
</html>
Output :