0% found this document useful (0 votes)
96 views

8423 Tejas Java Practical

This document is a certificate certifying that Mr./Miss. Tejas Dhulugade has successfully completed all the practicals of the subject Enterprise Java in partial fulfillment for the degree of B.Sc. (Information Technology) for the academic year 2022-2023 from the University of Mumbai. It lists the 10 practicals completed along with the dates and signatures of the internal and external examiners.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

8423 Tejas Java Practical

This document is a certificate certifying that Mr./Miss. Tejas Dhulugade has successfully completed all the practicals of the subject Enterprise Java in partial fulfillment for the degree of B.Sc. (Information Technology) for the academic year 2022-2023 from the University of Mumbai. It lists the 10 practicals completed along with the dates and signatures of the internal and external examiners.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 95

Certificate

This is to certify that Mr. /Miss. _ Tejas Dhulugade Examination

Seat no. 8427 has successfully completed all the practicals of the subject Enterprise Java

in partial fulfillment for the degree

of B.Sc. (Information Technology) SEM V, affiliated to university of Mumbai for the

academic year 2022-2023.

Internal Examiner

Head of the Department External Examiner


INDEX

List of Practical Date Signature


1 Implement the following Simple Servlet applications. 25-06-
2022
.
a Create a simple calculator application using servlet.
.
b Create a servlet for a login page. If the username and password
. are correct then it says message “Hello <username>” else a
message
“login failed”
c 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.

2 Implement the following Servlet applications with Cookies and 02/07/20


. 22
Sessions.
a Using RequestDispatcher 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.
b Create a servlet that uses Cookies to store the number of
. times a user has visited servlet.
c Create a servlet demonstrating the use of session creation and
. destruction. Also check whether the user has visited this page first
time or has visited earlier also using sessions.

3 Implement the Servlet IO and File applications. 16/07/20


22
.
a Create a Servlet application to upload and download a file.
.
b Develop a Simple Servlet Question Answer Application using
. Database.
c Create a simple Servlet application to demonstrate Non-Blocking
. Read Operation.
4 Implement the following JSP applications. 30/07/20
22
.
a Develop a simple JSP application to display values obtained from
. the
use of intrinsic (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).
c Create a registration and login JSP application to register and
. authenticate the user based on username and password using JDBC.

5 Implement the following JSP JSTL and EL Applications. 13/08/20


. 22

a Create an html page with fields, eno, name, age, desg, salary. Now
. submit this data to a JSP page which will update the employee table
of the database with matching eno.
b Create a JSP page to demonstrate the use of Expression language.
.
c Create a JSP application to demonstrate the use of JSTL.
.

6 Implement the following EJB Applications. 20/08/20


22
.
a Create a Currency Converter application using EJB [Stateless
. Session
Bean].
b Develop a simple Bank application using EJB [Stateful Session
. Bean].

7 Implement the following EJB Applications with interceptors.


.
a Develop a simple EJB application to demonstrate Servlet Hit count
. using Singleton Session Beans.
b Develop a simple EJB application, which work with Interceptors.
.

8 Implement the following JPA applications. 27/08/20


. 22

a Create a simple JPA application to perform CRUD operations on


. Book details. Book details like Book_Id, Book_Name, Book_Price,
Book_Price, Book_Author, Book_Publication

9 Implement the following JPA applications with ORM and 03/09/20


22
. Hibernate.
a Develop a Hibernate application to store Feedback of
. Website Visitors in MySQL Database.
b Develop a Hibernate application to perform CRUD operation on
. employee details in MySQL Database.

1 Implement the following applications. 10/09/20


22
0
.
a Develop a five page web application site using any two or three
. Java
EE Technologies.
PRACTICAL .1

A. Create a simple calculator application using servlet.

Index.html:

<html>

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>

<form action="calculator_servlet" method="post">

Enter 1<sup>st</sup> no:<input type="number" name="t1"><br>

Enter 2<sup>nd</sup> no:<input type="number" name="t2"><br>

<input type="submit" value="+" name="add">

<input type="submit" value="-" name="sub">

<input type="submit" value="*" name="mult">

<input type="submit" value="/" name="div">

</form>

</body>

</html>
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 java.io.*;

import javax.servlet.*;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.*;

public class calculator_servlet extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

try {

int i = Integer.parseInt(request.getParameter("t1"));

int j= Integer.parseInt(request.getParameter("t2"));

if(request.getParameter("add")!=null){

out.println("Addition is :"+(i+j));

if(request.getParameter("sub")!=null){

out.println("Subtraction is :"+(i-j));

if(request.getParameter("mult")!=null){

out.println("Multiplication is :"+(i*j));

}
if(request.getParameter("div")!=null){

out.println("division is :"+(i/j));

}catch(Exception e){

out.println("error :"+e);

OUTPUT-:
b. Create a servlet for a login page. If the username and password are correct then it says
message “Hello <username>” else a message “login failed”

index.html

<html>
<head>
<title>Login Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="login" method="get">
<input type="text" name="user" placeholder="Username"><br>
<input type="password" name="pass" placeholder="Password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>

login.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

@WebServlet(urlPatterns={"/login"})
public class login extends HttpServlet{
public void doGet(HttpServletRequest req , HttpServletResponse res) throws ServletException ,
IOException {
res.setContentType("Text/Html");
PrintWriter out = res.getWriter();
String name = req.getParameter("user");
String pas = req.getParameter("pass");
if(name.equals("shivendra") && pas.equals("8637"))
{
out.println("Welcome " + name );
}
else{
res.sendRedirect("index.html");
}
} }

Output:
c. 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.

index.html:

<html>
<head>
<title>Registration using JDBC</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="signup" method="get">
<input type="text" name="user" placeholder="Enter your name"><br>
<input type="email" name="email" placeholder="Enter your email"><br>
<input type="password" name="pass" placeholder="Enter your password"><br>
<input type="text" name="cn" placeholder="Enter your country"><br>
<input type="submit" value="Login">
</form>
</body>
</html>

signup.java:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.sql.*;
@WebServlet(urlPatterns={"/signup"})
public class signup extends HttpServlet{
public void doGet(HttpServletRequest request , HttpServletResponse response)
throws ServletException , IOException{
response.setContentType("Text/Html");
PrintWriter out = response.getWriter();
try{
String name = request.getParameter("user");
String pas = request.getParameter("pass");
String email = request.getParameter("email");
String country = request.getParameter("cn");
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection cn =
DriverManager.getConnection("jdbc:derby://localhost:1527/record","app","app");
PreparedStatement ps = cn.prepareStatement("insert into record values(?,?,?,?)");
ps.setString(1, name);
ps.setString(2, pas);
ps.setString(3, email);
ps.setString(4, country);
int i = ps.executeUpdate();
if(i>0){
out.println("Data saved successfully");
}
else{
out.println("Data save failed");
}}
catch(Exception e){
out.println("error :"+e);
}
}

Output-
Practical 2

Implement the following Servlet applications with Cookies and Sessions:

a. Using Request dispatcher Interface create a Servlet which will validate the password
entered by the user, if the user has entered &quot;Servlet&quot; 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.

index.html:

<html>
<head>
</head>
<body>
<form action="login" method="post">
Password: <input type="password" name="t2"><br>
<input type="submit" value="Login">
</form>
</body>
</html>

login.java:

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(urlPatterns={"/login"})
public class login extends HttpServlet {
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException{
response.setContentType("Text/Html");
PrintWriter out = response.getWriter();
try{
String pa = request.getParameter("t2");
if(pa.equals("servlet")){
RequestDispatcher rd = request.getRequestDispatcher("owner");
rd.forward(request,response);
}
else{
out.println("Please enter valid password");
response.sendRedirect("index.html");
}
}
catch(Exception e){
out.println("Error : "+e);
}
} }

owner.java:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
@WebServlet(urlPatterns={"/owner"})
public class owner extends HttpServlet {
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("Text/Html");
PrintWriter out=response.getWriter();
try{
String u = request.getParameter("t2");
out.println("Welcome "+u);
}
catch(Exception e){
out.println("error "+e);
}
}}

Output:
b. Create a servlet that uses Cookies to store the number of
times a user has visited servlet

logic.java:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
@WebServlet(urlPatterns={"/logic"})
public class logic extends HttpServlet{
static int i=1;
public void service(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException{
response.setContentType("Text/Html");
PrintWriter out= response.getWriter();
try{
Cookie c= new Cookie("user_name",String.valueOf(i));
response.addCookie(c);
int j =Integer.parseInt(c.getValue());
if(j==1){
out.println("Welcome user");
}
else{
out.println("You have visited this website "+j+" time");
}
i++;
}
catch(Exception e){
out.println("Error "+e);
}
}
}

Output:
c. Create a servlet demonstrating the use of session creation and destruction. Also check
whether the user has visited this page first time or has visited earlier also using sessions.

login.java:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
@WebServlet(urlPatterns={"/login"})
public class login extends HttpServlet{
static int i;
public void service(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException{
response.setContentType("Text/Html");
PrintWriter out= response.getWriter();
try{
HttpSession session = request.getSession(true);
if(session.isNew()){
out.println("Welcome , Shiv");
i=0;
}
else{
out.println("Welcome Again");
i++;
}
out.println("<br>Number of previous Accesses :"+i);
}
catch(Exception e){
out.println("Error "+e);
} } }

Output:
Practical 3

3.Implement the Servlet IO and File applications.


a. Create a Servlet application to upload and download a file.
CODE:
OUTPUT:
b. Develop Simple Servlet Question Answer Application using Database.
CODE:
Quiz table
create table quiz(question char(10),ans char(20));
insert into quiz values('q1','a');
insert into quiz values('q2','b');
insert into quiz values('q3','c');

index.html
<!DOCTYPE html>
<html>
<head>
<title>Quiz Application</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Quiz</h1>
<form action="QuizServlet" method="get">
<label>Java Enterprise Edition, is also known as?</label><br>
<input type="radio" name="q1" value="a">J2EE<br>
<input type="radio" name="q1" value="b">J2SE<br>
<input type="radio" name="q1" value="c">J2ME<br>

<label>The life cycle of a servlet is managed by?</label><br>


<input type="radio" name="q2" value="a">Servlet Itself<br>
<input type="radio" name="q2" value="b">Servlet Container<br>
<input type="radio" name="q2" value="c">http & https<br>

<label>Which of the following is not a valid type of statement in JDBC?</label><br>


<input type="radio" name="q3" value="a">PreparedStatement<br>
<input type="radio" name="q3" value="b">CallableStatement<br>
<input type="radio" name="q3" value="c">QueryStatement<br>

<br><br>
<input type="submit" value="Submit"/>
<input type="reset" value="Reset"/>

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

QuizServlet.java
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.sql.*;
import java.util.*;

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

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String paramName, paramValue[];
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
int cnt=0;
String ans="";

Enumeration paramNames = request.getParameterNames();


try {
Class.forName("org.apache.derby.jdbc.ClientDriver");

con=DriverManager.getConnection("jdbc:derby://localhost:1527/College","app","app");
stmt=con.createStatement();
rs=stmt.executeQuery("select ans from quiz");

while(rs.next()&& paramNames.hasMoreElements())
{
String un=rs.getString(1);
paramName = (String)paramNames.nextElement();
paramValue = request.getParameterValues(paramName);
for(int i=0;i<paramValue.length;i++)
{
ans = paramValue[i];
}

if(un.equals(ans))
cnt++;
}
out.println("<h1>You have scored "+cnt+" points out of 3.</h1>");
}catch(Exception e){out.println("Error: "+e);}
}
}
OUTPUT:
Practical 4

Implement the following jsp application

a. Develop a simple JSP application to display values obtained from the use of intrinsic
objects of various
types.

Index.html:

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="ImpObject.jsp" method="post">
Enter your class :<input type="text" name="t1"><br>
<input type="submit" value="request/out" name="detail"><br><br>

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


<input type="submit" value="response/session" name="detail2"><br><br>
Enter your address : <input type="text" name="add"><br>
<input type="submit" value="PageContext" name="detail3">
</form>
</body>
</html>

ImpObject.jsp :

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


<%@ page isErrorPage="true" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
//IMPLICIT OBJECT
//request
String cls = request.getParameter("t1");
if(request.getParameter("detail")!=null){
//out
out.println("Your class is " + cls);
}
//session , response
if (request.getParameter("detail2") != null) {
String name = request.getParameter("t2");
session.setAttribute("user", name);
response.sendRedirect("second.jsp");
}
if (request.getParameter("detail3") != null) {
String na = request.getParameter("add");
pageContext.setAttribute("user", na, PageContext.SESSION_SCOPE);
response.sendRedirect("third.jsp");
}
%>
<!-- exception implicit object-->
Sorry following exception occurred:<%= exception %>
</body>
</html>

Second.jsp :

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


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String name = (String) session.getAttribute("user");
out.println("Welcome " + name);
%>
</body>
</html>
Third.jsp :

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


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String nam = (String) pageContext.getAttribute("user", PageContext.SESSION_SCOPE);
out.print("Welcome to " + nam);
%>
</body>
</html>

Output-
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).

Index.html :

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="form.jsp" method="post">
Enter your name<input type="text" name="t1"><br>
Enter your age <input type="number" name="t2"><br>
Enter your email <input type="email" name="t3"><br>
Please select your hobbies <input type="checkbox" name="check1" value="play" >Playing
<input type="checkbox" name="check1" value="swimming" >swimming<br><br>
Gender <input type="radio" name="gen" value="Male"> Male&nbsp;&nbsp;
<input type="radio" name="gen" value="Female"> Female<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

form.jsp :
<%@page contentType="text/html" pageEncoding="UTF-8" import="java.sql.*,java.io.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String name = request.getParameter("t1");
int age = Integer.parseInt(request.getParameter("t2"));
String email = request.getParameter("t3");
String hobbies = request.getParameter("check1");
String gender = request.getParameter("gen");
RequestDispatcher rd= request.getRequestDispatcher("form2.jsp");
rd.forward(request, response);
%
</body>
</html>

form2.jsp :

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


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String name = request.getParameter("t1");
int age = Integer.parseInt(request.getParameter("t2"));
String email = request.getParameter("t3");
String hobbies = request.getParameter("check1");
String gender = request.getParameter("gen");
out.println("Your name is :"+name+"<br>");
out.println("Your age is :"+age+"<br>");
out.println("Your email is "+email+"<br>");
out.println("Your hobbies is :"+hobbies+"<br>");
out.println("I am a "+gender+"<br>");
%>
</body>
</html>

Output:
c. Create a registration and login JSP application to register and authenticate the user
based on username and password using JDBC.

Index.html :

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="reg.jsp" method="post">
Enter name: <input type="text" name="t1"><br>
Enter username (email): <input type="email" name="t2"><br>
Enter pass:<input type="password" name="t3"><br>
Re enter password: <input type="password" name="t4"><br>
<input type="submit" value="REGISTER" name="b1">
</form>
</body>
</html>

Reg.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8" import="java.sql.*,java.io.*;"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
try{
String name=request.getParameter("t1");
String username=request.getParameter("t2");
String pass=request.getParameter("t3");
String reenter=request.getParameter("t4");
if(pass.equals(reenter)){
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection c=DriverManager.getConnection("jdbc:derby://localhost:1527/pvt","app","app");
PreparedStatement ps=c.prepareStatement("insert into login values(?,?,?)");
ps.setString(1,name);
ps.setString(2,username);
ps.setString(3,pass);
int i=ps.executeUpdate();
if(i>0)
{
out.println("data saved");
}
else{
out.println("data not saved");
}
}
else{
out.println("password doesn't match with reenter password");
}
}
catch(Exception e){
out.println("error="+e);
} %>
</body>
</html>

Login.html :

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="Login.jsp" method="POST">
Enter username: <input type="email" name="t1"><br>
Enter pass: <input type="password" name="t2"><br>
<input type="submit" value="LOGIN" name="b1">
</form>
</body>
</html>

Login.jsp :

<%@page contentType="text/html" pageEncoding="UTF-8" import="java.sql.*,java.io.*;"%>


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
try{
String username=request.getParameter("t1");
String pass=request.getParameter("t2");
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection c=DriverManager.getConnection("jdbc:derby://localhost:1527/pvt","app","app");
PreparedStatement ps=c.prepareStatement("select * from login where username=? and password=? ");
ps.setString(1,username);
ps.setString(2,pass);
ResultSet rs=ps.executeQuery();
if(rs.next())
{
RequestDispatcher rd=request.getRequestDispatcher("Admin.jsp");
rd.forward(request, response);
}
else{
out.println("invalid user");
}
}
catch(Exception e) {
out.println(e); }
%>
</body>
</html>

Admin.jsp :

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


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body> <%
String u=request.getParameter("t1");
out.println("Welcome " +u); %>
</body>
</html>

Output-
PRACTICAL 5

A. CREATE AN HTML PAGE WITH FIELDS, ENO, NAME, AGE, DESG, SALARY. NOW ON
SUBMIT THIS DATA
TO A JSP PAGE WHICH WILL UPDATE THE EMPLOYEE TABLE OF
DATABASE WITH MATCHING ENO.
INPUT:-
Index.html
<!DOCTYPE html>

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="UpdateEmp.jsp" >
Enter Employee Number<input type="text" name="txtEno" ><br> Enter
Name<input type="text" name="txtName" ><br>
Enter age<input type="text" name="txtAge" ><br> Enter
0Salary<input type="text" name="txtSal" ><br>
<input type="reset" ><input type="submit">
</form>
</body>
</html>

UpdateEmp.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Employee Record Update</h1>
<%
String eno=request.getParameter("txtEno"); String
name=request.getParameter("txtName"); String age =
request.getParameter("txtAge"); String sal =
request.getParameter("txtSal");
try{ Class.forName("com.mysql.jdbc.Driver"); Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/C","root","root"); PreparedStatement stmt =
con.prepareStatement("select * from emp where empid=?"); stmt.setString(1, eno);
ResultSet rs = stmt.executeQuery(); if(rs.next()){
out.println("<h1>~~~ Employee "+name+" Exist ~~~ </h1>");
PreparedStatement pst1= con.prepareStatement("update emp set salary=? where empid=?");
PreparedStatement pst2= con.prepareStatement("update emp set age=? where empid=?");
pst1.setString(1, sal);
pst1.setString(2, eno);
pst2.setString(1, age);
pst2.setString(2, eno);
pst1.executeUpdate();
pst2.executeUpdate();
}else{
out.println("<h1>Employee Record not exist !!!!!</h1>");
}}catch(Exception e){out.println(e);}%>
</body>
</html>
OUTPUT:-
PRACTICAL NO: 6

currency.java
/*
* 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.
*/
package p1;

import javax.ejb.Stateless;

/**
*
* @author student
*/
@Stateless
public class currency implements currencyRemote {

@Override
public float currencyMethod(int a)
{ return a*80;
}

// Add business logic below. (Right-click in editor and choose


// "Insert Code > Add Business Method")

@Override
public float dollartoinr(float a) { return
0.0F;
}
}

currencyremote.java
/*
* 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.
*/
package p1;

import javax.ejb.Remote;

/**
*
* @author student
*/ @Remote
public interface currencyRemote { float

currencyMethod(int a); float dollartoinr(float

a);

Main.java
/*
* 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.
*/
package applicationclient1;

import javax.ejb.EJB; import


p1.currencyRemote;

/**
*
* @author student
*/
public class Main {

@EJB
private static currencyRemote currency;

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here float f =
currency.currencyMethod(10); System.out.println(f);

}
6(B) DEVELOP SIMPLE BANK APPLICATION USING EJB [STATEFUL
SESSION BEAN].

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="banking" method="post">

enter amount&nbsp;<input type="text" name="t1"></br></br>

<input type="submit" name="dep" value="deposit">&nbsp;&nbsp;


<input type="submit" name="with" value="withdraw">&nbsp;&nbsp;
<input type="submit" name="bal" value="balance">&nbsp;&nbsp;
</form>
</body>
</html>

banking.java
package p1;

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.ejb.*;

@WebServlet(name = "banking", urlPatterns = {"/banking"})


public class banking extends HttpServlet {

@EJB
p1.BankLocal b;

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {

String amount = request.getParameter("t1");


if (request.getParameter("bal") != null) {

out.println("Current Amount: " + b.getBalance());

}
if (request.getParameter("with") != null) {
boolean status = b.withdraw(Integer.parseInt(amount));
if (status) {

out.print("Amount successfully withdrawn!");


} else {
out.println("Enter less amount");
}
}
if (request.getParameter("dep") != null)
{ b.deposit(Integer.parseInt(amount));
out.print("Amount successfully deposited!");
}
}
}
}

bankLocal.java
package p1;
import javax.ejb.Local;
@Local

public interface BankLocal


{ boolean withdraw(int
amount); void deposit(int
amount);

int getBalance();

Bank.java
package p1;
import javax.ejb.Stateful;
@Stateful

public class Bank implements BankLocal {


private int amount=5000;

public boolean withdraw(int amount){


if(amount<=this.amount){

this.amount-=amount;
return true;

}else{
return false;
}
}
public void deposit(int amount){
this.amount+=amount;

public int getBalance(){


return amount;

}
}

Output-
Check balance

Deposit
withdraw
PRACTICAL: 7

. DEVELOP SIMPLE EJB APPLICATION TO DEMONSTRATE SERVLET HIT


COUNT USING SINGLETON SESSION BEANS

INPUT:-
Index.html
<!DOCTYPE html>

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>TODO write content</div>
<h1>Welcome to Hit Count Page</h1> Page was hit #{count.hitCount} times<h1/>
</body>
</html>
package mypack;
import java.io.Serializable; import
javax.ejb.EJB;
import javax.enterprise.context.ConversationScoped; import
javax.inject.Named;
import counter.ejb.CounterBean; @Named("count")
@ConversationScoped public class Count implements
Serializable {
@EJB

private CounterBean counterBean; private int hitCount; public Count()


{ this.hitCount = 0;
}

public int getHitCount() {


hitCount = counterBean.getHits(); return hitCount;
}

public void setHitCount(int newHits) { this.hitCount = newHits;


}
}
CounterBean.java
package counter.ejb; import
javax.ejb.Singleton;

@Singleton
public class CounterBean { private int hits = 1; public int
getHits() {
return hits++;
}}
OUTPUT:-

PRACTICAL 8
8(A) IMPLEMENT THE FOLLOWING JPA APPLICATIONS.
CREATE A SIMPLE JPA APPLICATION TO PERFORM CRUD OPERATIONS
ON BOOK DETAILS. BOOK DETAILS LIKE BOOK_ID, BOOK_NAME,
BOOK_PRICE, BOOK_PRICE, BOOK_AUTHOR, BOOK_PUBLICATION

.html

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="jpa.jsp" method="POST">
<table>
<tr><td>Enter book id</td><td> <input type="number" name="id"></td></tr>
<tr><td>Enter book Name</td><td> <input type="text" name="name"></td></tr>

<tr><td>Enter book price </td><td> <input type="text" name="price"></td></tr>

<tr><td> Enter author name </td><td><input type="text" name="ath"></td></tr>


<tr><td> publisher name </td><td><input type="text" name="pub"></td></tr>

</table>

</br> </br>
<input type="submit" name="save" value="SAVE">&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" name="ret" value="retrive">&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" name="up" value="update">&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" name="del" value="delete">
</form>
</body>
</html>

Jpa.jsp

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


<%@page import="p1.Book"%>
<%@page import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%

int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");

int price = Integer.parseInt(request.getParameter("price"));


String ath = request.getParameter("ath");

String pub = request.getParameter("pub");

EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPA");


EntityManager em = emf.createEntityManager();

if (request.getParameter("save") != null) {
em.getTransaction().begin();

Book b1 = new Book();


b1.setB_id(id);
b1.setB_price(price);
b1.setB_ath(ath);
b1.setB_pub(pub);
b1.setB_name(name);
em.persist(b1);
em.getTransaction().commit();
out.println("Data Saved");

if (request.getParameter("ret") != null) {
em.getTransaction().begin();

Query query = em.createQuery("Select b from Book b");


List result = query.getResultList();

Iterator itr = result.iterator();


out.println("<table border='1'>");
out.print("<tr><th>book id</th>");
out.print("<th>book name</th>");
out.print("<th>price</th>");
out.print("<th>author</th>");
out.print("<th>publisher</th></tr>");
while (itr.hasNext()) {

Book b = (Book) itr.next();


out.println("<tr>");
out.print("<td>"+b.getB_id() + "</td>");
out.print("<td>"+b.getB_name() + "</td>");
out.print("<td>"+b.getB_price() + "</td>");
out.print("<td>"+b.getB_ath() + "</td>");
out.print("<td>"+b.getB_pub() + "</td>");
out.println("</tr>");

}
out.println("</table>");
em.getTransaction().commit();

}
if (request.getParameter("up") != null) {
em.getTransaction().begin();

Book b=em.find(Book.class,id);
b.setB_price(price);
em.getTransaction().commit();
out.print("data updated");

}
if (request.getParameter("del") != null) {
em.getTransaction().begin();

Book b=em.find(Book.class,id);
em.remove(b);
em.getTransaction().commit();
out.println("row deleted");

em.close();
emf.close();
%>
</body>
</html>

Book.java
package p1;
import java.io.*;

import javax.persistence.*;

@Entity
public class Book implements Serializable
{ private static final long serialVersionUID =
1L; @Id

private int b_id;


private int b_price;

private String b_name,b_ath,b_pub;

public int getB_id() {


return b_id;
}

public void setB_id(int b_id) {


this.b_id = b_id;

public int getB_price() {


return b_price;

public void setB_price(int b_price) {


this.b_price = b_price;

public String getB_name() {


return b_name;

public void setB_name(String b_name) {


this.b_name = b_name;

public String getB_ath() {


return b_ath;

}
public void setB_ath(String b_ath) {
this.b_ath = b_ath;
}

public String getB_pub() {


return b_pub;

public void setB_pub(String b_pub) {


this.b_pub = b_pub;

}
Output-
Retrieve

Update
PRACTICAL 9
9(A) IMPLEMENT THE FOLLOWING JPA APPLICATIONS WITH ORM
AND HIBERNATE.

.HTML
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="hb.jsp" method="post">
enter your name<input type="text" name="name"></br> </br>
enter your email<input type="email" name="email"></br></br>

feedback <textarea name="fed"></textarea> </br> </br>


<input type="submit" name="s1" value="sumbit">
</form>
</body>
</html>

hb.jsp
<%@page contentType="text/html" import="org.hibernate.*"
import="org.hibernate.cfg.*" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String name=request.getParameter("name");
String email=request.getParameter("email");
String feed=request.getParameter("fed");

SessionFactory sf=new
Configuration().configure("hibernate.cfg.xml").buildSessionFactory()
;
Session s=sf.openSession();
Transaction t=s.beginTransaction();
p1.Pojo e=new p1.Pojo();
e.setF_email(email);
e.setF_name(name);
e.setF_fed(feed);

s.save(e);

t.commit();
s.close();
out.println("<script>alert('feddback saved');window.location.href='index.html'</script>")
%>
</body>
</html>
hb.java
package p1;

import javax.persistence.*;
@Entity
@Table(name="feedback")
public class Pojo {

@Id
private String f_email;
@Column

private String f_name,f_fed;


public String getF_email() {

return f_email;
}
public void setF_email(String f_email) {
this.f_email = f_email;

}
public String getF_name() {
return f_name;

public void setF_name(String f_name) {


this.f_name = f_name;

}
public String getF_fed() {
return f_fed;

}
public void setF_fed(String f_fed) {
this.f_fed = f_fed;

}
}

Hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD
3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">root</property>
<mapping class="p1.pojo "/>
</session-factory>
</hibernate-configuration>

Output-
0

Delete
9(B) DEVELOP A HIBERNATE APPLICATION TO PERFORM CRUD
OPERATION ON EMPLOYEE DETAILS IN MYSQL DATABASE.
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="emp.jsp" method="post">
<table>
<tr><td>enter employee id</td><td><input type="text" name="id"></td></tr>
<tr><td>enter employee name</td><td><input type="text" name="name"></td></tr>
<tr><td>employee phone number</td><td><input type="text" name="number"></td></tr>
<tr><td>employee salary </td><td><input type="text" name="sal"></td></tr>
</table>
</br>
<input type="submit" name="save" value="save">&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" name="ret" value="show">&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" name="update" value="update"> &nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" name="del" value="delete" >&nbsp;&nbsp;&nbsp;&nbsp;
</form>
</body>
</html>
emp.,jsp

<%@page contentType="text/html" import="java.util.*" import="org.hibernate.*"


import="org.hibernate.cfg.*" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>0
<%
String id = request.getParameter("id");
String name = request.getParameter("name");
String phone = request.getParameter("number");
String sal = request.getParameter("sal");

SessionFactory sf = new
Configuration().configure("hybernate1.cfg.xml").buildSessionFactory();
Session s = sf.openSession();

if (request.getParameter("save") != null) {
Transaction t = s.beginTransaction();
p1.Emp e = new p1.Emp();
e.setE_id(id);

e.setE_name(name);
e.setE_phone(phone);
e.setE_sal(sal);

s.save(e);
t.commit();
s.close();
out.println("<script>alert('data saved');window.location.href='emp.html'</script>");
}
if (request.getParameter("ret") != null) {
Transaction t = s.beginTransaction();
p1.Emp e = new p1.Emp();

Query q = s.createQuery("FROM Emp");

List result = q.list();


out.println("<table border='1'>");
out.println("</tr><th>id</th>");
out.println("<th>name</th>");
out.println("<th>phone</th>");
out.println("<th>salary</th></tr>");
Iterator itr = result.iterator();

while (itr.hasNext()) {
out.println("<tr>");

p1.Emp e1 = (p1.Emp) itr.next();


out.print("<td>"+e1.getE_id() + "</td>");
out.print("<td>"+e1.getE_name() + "</td>");
out.print("<td>"+e1.getE_phone()+"</td>");
out.print("<td>"+e1.getE_sal()+"</td>");
out.println("<tr>");

} out.println("</table>");
t.commit();

s.close();

}
if (request.getParameter("update") != null) {
Transaction t = s.beginTransaction();
p1.Emp em = (p1.Emp)s.get(p1.Emp.class,id);
em.setE_name(name);

s.update(em);
t.commit();
s.close();
out.println("data saved");

}
try{
if (request.getParameter("del") != null) {
Transaction t = s.beginTransaction();

p1.Emp em = (p1.Emp)s.get(p1.Emp.class,id);
s.delete(em);

t.commit();
s.close();
out.println("data deletd");

}catch(Exception e){
out.print(e);

}
%>
</body>
</html>
emp.java
package p1;
import javax.persistence.*;
@Entity

@Table(name = "Emp")
public class Emp {

@Id
private String e_id;
@Column

private String e_name, e_phone, e_sal;


public String getE_id() {

return e_id;

}
public void setE_id(String e_id) {
this.e_id = e_id;

public String getE_name() {


return e_name;

}
public void setE_name(String e_name) {
this.e_name = e_name;

public String getE_phone() {


return e_phone;

public void setE_phone(String e_phone) {


this.e_phone = e_phone;
}
public String getE_sal() {
return e_sal;

}
public void setE_sal(String e_sal) {
this.e_sal = e_sal;

}
}

Hybernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD


3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">root</property>
<mapping class="p1.Emp"/>
</session-factory>
</hibernate-configuration>

Output
Retrieve

Update
Delete
PRACTICAL 10
10) DEVELOP A FIVE PAGE WEB APPLICATION SITE USING ANY TWO OR
THREE JAVA EE TECHNOLOGIES.
E-commerce
website
Welcome.html

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>electronics E-shop</h1>
<h3>
shop online here with grate discounts !!!!</h3></br>

<a href="login.html"><button>login</button></a>
<a href="register.html"><button>register here</button></a>
</center>
</body>
</html>

Output
Register page
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="register.jsp" method="post">
<h1>register here</h1>
<table>
<tr>
<td>Enter Name:</td><td><input type="text" name="name"></td>
</tr>
<tr>
<td>Enter Email:</td><td><input type="text" name="email"></td></tr>
<tr>
<td>Enter User_Name :</td><td> <input type="text" name="username"></td>
</tr>
<tr>
<td>Enter password:</td>
<td><input type="password" name="pass"></td></tr>
</table><br>
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;
&nbsp;<input type="submit" name="n1" value="Register">
</form>
</body>
</html>

Register.jsp

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


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

String name=request.getParameter("name");
String pass=request.getParameter("pass");
String email=request.getParameter("email");
String username=request.getParameter("username");
Class.forName("com.mysql.jdbc.Driver");
Connection
c=DriverManager.getConnection("jdbc:mysql://localhost/test","root","");
PreparedStatement ps=c.prepareStatement("insert into tyit values(?,?,?,?)");
ps.setString(1,name);
ps.setString(2,email);
ps.setString(3,username);
ps.setString(4,pass);
ps.executeUpdate();
ps.close();
c.close();
out.println("<script>alert('data saved');window.location.href='login.html'</script>");
%>
</body>
</html>

Output
Login page

Login.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>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="login.jsp" method="post">
username&nbsp;<input type="text" name="uname"></br>
password&nbsp;<input type="text" name="pass"></br>
<input type="submit" name="t1">
</form>
</body>
</html>
Login.jsp

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


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String name=request.getParameter("uname");
String pass=request.getParameter("pass");
String u,p;
Class.forName("com.mysql.jdbc.Driver");
Connection
c=DriverManager.getConnection("jdbc:mysql://localhost/test","root","");
PreparedStatement ps=c.prepareStatement("select * from tyit");
ResultSet RS= ps.executeQuery();
while (RS.next())
{
u=RS.getString(3);
p=RS.getString(4);
if(name.equals(u) && pass.equals(p))
{

session.setAttribute("username",name);
response.sendRedirect("homepage.jsp");
break;

}
}
out.println("<script>alert('invalid username and
password')window.location.href='login.html'</script>");

Output:

%>
</body>
</html>
RS.close();
ps.close();
c.close();
Homepage

Homepage.jsp

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


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

<h1>electronics e-SHOP</h1>
<h3>
shop by category
</h3>
<table>
<tr>
<td><img src="tv.jpg" height="70" width="100" alt="tv"></td>
<td><img src="laptop.jpg" height="100" width="100" alt="laptop"></td>
<td><img src="washing.jpg" height="100" width="160" alt="wshing machine"></td>
</tr>
<tr>
<td>washing machine</td>
<td>&nbsp;&nbsp;&nbsp;laptop</td>
<td>&nbsp;&nbsp;&nbsp;tv</td>
</tr>
</table>
</br></br>
products
<table border="1">
<tr>
<td><image src="phone.jpg" height="100" width="100"></td>
<td>samsung galaxy fold </br> price=52000/- </td>
<td><a href="product.jsp?pd=4">&nbsp;&nbsp;&nbsp;product description</a></td>
</tr>
<tr>
<td><image src="laptop.jpg" height="100" width="100"></td>
<td>HP chromebook</br> price=30000/- </td>
<td><a href="product.jsp?pd=2">&nbsp;&nbsp;&nbsp;product description</a></td>
</tr>
<tr>
<td><image src="washing.jpg" height="100" width="100"></td>
<td>one plus Smart TV </br> price=14000/- </td>
<td><a href="product.jsp?pd=3">&nbsp;&nbsp;&nbsp;product description</a></td>
</tr>
</table>
</body>
</html>

Output
Product details page

ProductDetail.jsp

<%--
Document : product
Created on : Oct 17, 2020, 6:54:28 PM
Author : Admin
--%>

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


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>product description</h1>
<%!
String p_id;
String img;
%>
<%
String id=request.getParameter("pd");
Connection c=DriverManager.getConnection("jdbc:mysql://localhost/test","root","");
PreparedStatement ps=c.prepareStatement("select * from pd");
ResultSet RS= ps.executeQuery();
out.println("<table>");
out.println("<tr>");
while(RS.next())
{

p_id=RS.getString(1);
if(p_id.equals(id))
{
out.println("<td rowspan='6'>");
out.println("<img src='"+RS.getString(2)+"'>"+"</td>");
out.println("<td>"+RS.getString(3)+"</td>");
out.println("</tr>");
out.println("<tr><td>"+RS.getString(4)+"</td></tr>");
out.println("<tr><td>"+RS.getString(5)+"&nbsp;warranty<td></tr>");
out.println("<tr><td>mfg by&nbsp;"+RS.getString(6)+"<td></tr>");
out.println("<tr><td>price&nbsp;"+RS.getString(7)+"<td></tr>");
out.println("<tr><td><a href='cart.jsp?pd="+id+"'><button>add to
cart</button></a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='buy.jsp'><button>buy
product</button></a></td></tr>");
out.println("</table>");
}
}
%>

</body>
</html>

output
Cart page

Cart.jsp

<%--
Document : cart.jsp
Created on : Oct 17, 2020, 6:31:49 PM
Author : Admin
--%>

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


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<center><h3>cart</h3></center>
<table border="1" style="width:100%;">
<tr>
<th>product</th><th>name</th><th>price</th><th>buy</th>
</tr>

<%
String p_id;
String id = request.getParameter("pd");
Connection c = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "");
PreparedStatement ps = c.prepareStatement("select * from pd");
ResultSet RS = ps.executeQuery();

while (RS.next()) {
p_id = RS.getString(1);
if (p_id.equals(id)) {
out.println("<tr>");
out.println("<td>" + "<img src='" + RS.getString(2) + "'>" + "</td>");
out.println("<td>" + RS.getString(3) + "</td>");

out.println("<td>price&nbsp;" + RS.getString(7) + "</td>");


out.println("<td><a href='buy.jsp'>buy product</a></td>");

}
}
%>

</table>
</body>
</html>

Output

You might also like