Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
MODULE 3
Implement the following JSP & JSTL applications
a. 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).
b. Create a registration and login JSP application to register and
authenticate the user based on username and password using JDBC.
c. Create a JSP application to demonstrate the use of JSTL.
d. Create a Currency Converter application using EJB.
e. Develop a Simple Room Reservation System Application Using EJB.
f. Develop simple shopping cart application using EJB [Stateful Session
Bean].
g. Develop simple EJB application to demonstrate Servlet Hit
count using Singleton Session Beans.
Page No : 42
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
THEORY- PRACTICAL
Aim: Implement the following JSP & JSTL applications
Page No : 43
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
Page No : 44
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
Page No : 45
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
Page No : 46
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
Page No : 47
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
PRACTICAL 3
Aim: Implement the following JSP & JSTL applications
a. 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).
[Link]
<!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="[Link]" method="post">
Enter Your Name:<input type="text" name="name">
<br>
<br>
Enter Your Age:<input type="text" name="age">
<br>
<br>
Select Hobbies:<br>
Singing<input type="checkbox" name="hobbies" value="singing"><br>
Reading<input type="checkbox" name="hobbies" value="reading"><br>
Football<input type="checkbox" name="hobbies" value="football"><br>
<br>
<br>
Enter Email:<input type="email" name="email">
<br>
<br>
Select Gender:<br>
<input type="radio" name="gender" value="Male">Male<br>
Page No : 48
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
<input type="radio" name="gender" value="Female">Female<br>
<input type="radio" name="gender" value="Other">Other<br>
<br>
<br>
<input type="hidden" name="error">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
[Link]
package mypack;
public class CheckerBean
{
String name,age,hobbies,email,gender,error;
public void CheckerBean()
{
error="";
}
public void setName(String n)
{
name=n;
}
public void setAge(String n)
{
age=n;
}
public void setHobbies(String n)
{
hobbies=n;
}
public void setEmail(String n)
{
email=n;
}
public void setGender(String n)
{
gender=n;
}
public void setError(String n)
Page No : 49
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
{
error=n;
}
public String getName()
{
return name;
}
public String getAge()
{
return age;
}
public String getHobbies()
{
return hobbies;
}
public String getEmail()
{
return email;
}
public String getGender()
{
return gender;
}
public String getError()
{
return error;
}
public boolean validate()
{
boolean res=true;
if([Link]().equals("") || (name==null))
{
error+="<br>Enter First Name";
res=false;
}
if([Link]() > 2 || (age==null))
{
error+="<br>Age Invalid";
res=false;
}
return res;
Page No : 50
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
}
}
[Link]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Registration Successful !!!</title>
</head>
<body>
<h1>Data Validated Successfully..!</h1>
</body>
</html>
[Link]
<jsp:root xmlns:jsp="[Link] version="2.0">
<jsp:[Link] contentType="text/html" pageEncoding="UTF-8" import =
"[Link]" />
<!-- any content can be specified here, e.g.: -->
<jsp:element name="text">
<jsp:attribute name="lang">EN</jsp:attribute>
<jsp:body>Validation Page</jsp:body>
<jsp:useBean id="obj" scope="request" class="[Link]">
<jsp:setProperty name="obj" property="*"/>
<jsp:scriptlet>if ([Link]())
{
</jsp:scriptlet>
<jsp:forward page="[Link]"/>
<jsp:scriptlet>
}else {
</jsp:scriptlet>
<jsp:include page="[Link]"/>
<jsp:scriptlet>
}
</jsp:scriptlet>
<jsp:expression>[Link]()</jsp:expression>
</jsp:useBean>
Page No : 51
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
</jsp:element>
</jsp:root>
OUTPUT:
Page No : 52
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
b. Create a registration and login JSP application to register and
authenticate the user based on username and password using JDBC.
[Link]
<!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">
<style>
<style>
body {
background-color:
#333; color: #ddd;
font-family: Arial, sans-serif;
padding: 20px;
}
form {
max-width: 400px;
margin: 0 auto;
background-color:
#333; padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0,0,0,0.2);
}
form label{
color: white;
}
input[type="text"], input[type="password"] {
width: calc(100% - 20px);
Page No : 53
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
padding:
10px; margin:
8px 0;
border: 1px solid #444;
background-color:
#444; color: #fff;
border-radius: 4px;
box-sizing: border-box;
}
input[type="submit"], input[type="reset"] {
width: 100%;
padding: 10px;
margin-top: 10px;
border: none;
background-color:
#4CAF50; color: white;
cursor: pointer;
border-radius:
4px; font-size:
16px;
}
input[type="submit"]:hover, input[type="reset"]:hover {
background-color: #45a049;
}
input[type="submit"]:active, input[type="reset"]:active
{ background-color: #3e8e41;
}
h2 {
color: white;
text-align:
center;
}
</style>
</head>
<body>
<form action="[Link]" method="post">
<h2>User Login</h2>
<label for="username">Enter Username:</label>
<input type="text" id="username" name="username" required>
<label for="password">Enter Password:</label>
<input type="password" id="password" name="password" required>
<input type="submit" value="Login">
<input type="reset" value="Reset">
Page No : 54
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
</form>
</body>
</html>
[Link]
<!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.
-->
<!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">
<style>
body {
background-color: #333;
color: #fff;
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
div {
width: 80%;
margin: 100px auto;
text-align: center;
}
h1 {
font-size: 1.5em;
margin-bottom: 20px;
}
a{
color: #fff;
text-decoration: none;
padding: 10px 20px;
Page No : 55
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
background-color: #555;
border-radius: 5px;
display: inline-block;
margin: 10px;
transition: background-color 0.3s ease;
}
a:hover {
background-color: #777;
}
</style>
</head>
<body>
<div>
<h1>New user?</h1><a href="[Link]">Click Here TO Register</a>
<h1>Old user?</h1><a href="[Link]">Click Here TO Login</a>
</div>
</body>
</html>
[Link]
<!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">
<style>
body {
background-color:
#222; color: #ddd;
font-family: Arial, sans-serif;
padding: 20px;
}
form {
max-width: 400px;
Page No : 56
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
margin: 0 auto;
background-color:
#333; padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0,0,0,0.2);
}
input[type="text"], input[type="password"], input[type="email"] {
width: calc(100% - 20px);
padding:
10px; margin:
8px 0;
border: 1px solid #444;
background-color:
#444; color: #ddd;
border-radius: 4px;
box-sizing: border-box;
}
input[type="submit"], input[type="reset"] {
width: 100%;
padding: 10px;
margin-top: 10px;
border: none;
background-color:
#4CAF50; color: white;
cursor: pointer;
border-radius:
4px; font-size:
16px;
}
input[type="submit"]:hover, input[type="reset"]:hover {
background-color: #45a049;
}
input[type="submit"]:active, input[type="reset"]:active
{ background-color: #3e8e41;
}
h2 {
text-align: center;
}
</style>
</head>
<body>
<form action="[Link]" method="post">
<h2>User Registration</h2>
<label for="username">Enter Username:</label>
Page No : 57
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
<input type="text" id="username" name="username" required>
<label for="password">Enter Password:</label>
<input type="password" id="password" name="password" required>
<label for="password_confirm">Re-Enter Password:</label>
<input type="password" id="password_confirm" name="password_confirm" required>
<label for="email">Enter Email:</label>
<input type="email" id="email" name="email" required>
<label for="country">Enter Country Name:</label>
<input type="text" id="country" name="country" required>
<input type="submit" value="Register">
<input type="reset" value="Reset">
</form>
</body>
</html>
<%--
Document : register
Created on : Jul 12, 2024, [Link]
PM Author : Admin
--%>
[Link]
<%@page contentType="text/html" pageEncoding="UTF-8" import="[Link].*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String s1 = [Link]("username");
String s2 = [Link]("password");
String s3 = [Link]("password_confirm");
String s4 = [Link]("email");
String s5 = [Link]("country");
Page No : 58
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
if([Link](s3))
{
try{
[Link]("[Link]");
Connection conn =
[Link]("jdbc:mysql://localhost:3306/test","root","root");
PreparedStatement pst =[Link]("insert into registration_041
values(?,?,?,?)");
[Link](1, s1);
[Link](2, s2);
[Link](3, s4);
[Link](4, s5);
int rows = [Link]();
if(rows==1)
{
[Link]("Mubarakho !!! Registration Succesfully !!!");
}
else
{
[Link]("Kuch Toh Gadbad Hai...!!! Firse try Kar!!!");
%>
<jsp:include page="[Link]"></jsp:include>
<%
}
}
catch(Exception e)
{
[Link]("Exception :"+e);
}
}
else
{
[Link]("Password Mismatch..!!! Reenter All Details");
%>
<jsp:include page="[Link]"></jsp:include>
<%
Page No : 59
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
}
%>
</body>
</html>
[Link]
<%@page contentType="text/html" pageEncoding="UTF-8" import="[Link].*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String s1 =[Link]("username");
String s2 =[Link]("password");
try
{
[Link]("[Link]");
Connection conn
=[Link]("jdbc:mysql://localhost:3306/test","root","root");
PreparedStatement pst =[Link]("select * from registration_041
where username=?");
[Link](1,s1);
ResultSet rs = [Link]();
if ([Link]())
{
if([Link](2).equals(s2))
{
[Link]("Login HoGaya Mubarakho !!");
}
else
{
[Link]("Appka Password Galat hai !!");
%>
<jsp:include page="[Link]"></jsp:include>
<%
Page No : 60
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
}
}
else
{
[Link]("Tum Exist Nhi karte Register Karo Jaakar");
%>
<jsp:include page="[Link]"></jsp:include>
<%
}
catch(Exception e)
{
[Link]("Exception:"+e); %>
</body>
</html>
Page No : 61
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
OUTPUT:
Page No : 62
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
c. Create a JSP application to demonstrate the use of JSTL.
[Link]
<!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>Jsp Module</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Choose Option</h1>
<a href="[Link]">Insert Record</a><br><br>
<a href="[Link]">Display Record</a>
</body>
</html>
[Link]
<%@page contentType="text/html" pageEncoding="UTF-8"
import="[Link].*,[Link].*,[Link].*" %>
<%@taglib uri="[Link] prefix="c" %>
<%@taglib uri="[Link] prefix="sql" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
Page No : 63
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
<body>
<sql:setDataSource var="dbsource" driver="[Link]"
url="jdbc:mysql://localhost/test" user="root" password="root"/>
<sql:query dataSource="${dbsource}" var="result">
SELECT * from product_details120 WHERE pid=?;
<sql:param value="${[Link]}"/>
</sql:query>
<h3>Update</h3>
<form action="[Link]">
<table>
<tr>
<th>Product Name</th>
<th>Quantity</th>
</tr>
<c:forEach var="row" items="${[Link]}">
<tr>
<td><input type="hidden" value="${[Link]}" name="pid" >
<input type="text" value="${[Link]}" name="pname">
</td>
<td><input type="text" value="${[Link]}"
name="quantity"></td>
<td><input type="submit" value="Update"></td>
</tr>
</c:forEach>
</table>
<a href="[Link]">Go Home</a>
</form>
</body>
</html>
[Link]
<%@page contentType="text/html" pageEncoding="UTF-8"
import="[Link].*,[Link].*,[Link].*" %>
<%@taglib uri="[Link] prefix="c" %>
<%@taglib uri="[Link] prefix="sql" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
Page No : 64
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
<body>
<sql:setDataSource var="dbsource" driver="[Link]"
url="jdbc:mysql://localhost/test" user="root" password="root"/>
<sql:update dataSource="${dbsource}" var="count">
UPDATE product_details120 SET pname=?, quantity=? WHERE
pid='${[Link]}';
<sql:param value="${[Link]}"/>
<sql:param value="${[Link]}"/>
</sql:update>
<c:if test="${count>=1}">
<font size="5" color="green"> wohohohohohohoho data updated </font><br>
<a href="[Link]">Go Home</a>
</c:if>
</body>
</html>
[Link]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="[Link] prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Insert Page</title>
</head>
<body>
<form action="[Link]">
<table border="1">
<tr>
<th colspan="2"> Purchase Product</th>
</tr>
<tr>
<td>Product Id</td>
<td><input type="number" name="pid"></td>
</tr>
<tr>
<td>Product Name</td>
<td><input type="text" name="pname"></td>
</tr>
<tr>
<td>Quantity</td>
<td><input type="number" name="quantity"></td>
Page No : 65
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
</tr>
<tr>
<td><input type="submit" name="save" value="Save"></td>
<td><input type="reset" name="reset" value="Reset"></td>
</tr>
</table>
</form>
<font color="red">
<c:if test="${not empty [Link]}">
<c:out value="${[Link]}"/>
<a href="[Link]">Go Back</a>
</c:if>
</font>
<font color="green">
<c:if test="${not empty [Link]}">
<c:out value="${[Link]}"/>
<a href="[Link]">Go Back</a>
</c:if>
</font>
</body>
</html>
[Link]
<%@page contentType="text/html" pageEncoding="UTF-8"
import="[Link].*,[Link].*,[Link].*" %>
<%@taglib uri="[Link] prefix="c" %>
<%@taglib uri="[Link] prefix="sql" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<c:if test="${empty [Link] or empty [Link] or empty [Link]}">
<c:redirect url="[Link]">
<c:param name="errMsg" value="Please enter product details"/>
</c:redirect>
</c:if>
<sql:setDataSource var="dbsource" driver="[Link]"
url="jdbc:mysql://localhost/test" user="root" password="root"/>
<sql:update dataSource="${dbsource}" var="result">
Page No : 66
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
Insert into product_details120 VALUES(?,?,?)
<sql:param value="${[Link]}"/>
<sql:param value="${[Link]}"/>
<sql:param value="${[Link]}"/>
</sql:update>
<c:if test="${result>=1}">
<font size="5" color="green">Data inserted</font>
<c:redirect url="[Link]">
<c:param name="susMsg" value="Data inserted"/>
</c:redirect>
</c:if>
</body>
</html>
[Link]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="[Link].*,[Link].*,[Link].*"%>
<%@taglib uri="[Link] prefix="c" %>
<%@taglib uri="[Link] prefix="sql" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP display Page</title>
<script>
function confirmGo(m,u){
if(confirm(m)){
[Link] = u;
}
}
</script>
</head>
<body>
<sql:setDataSource var="dbsource" driver="[Link]"
url="jdbc:mysql://localhost/test" user="root" password="root"/>
<sql:query dataSource="${dbsource}" var="result">
SELECT * from product_details120;
</sql:query>
<form action="">
<center>
<table border="1">
<tr>
Page No : 67
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
<th>Product ID</th>
<th>Product Name</th>
<th>Product Quantity</th>
<th colspan="2">Action</th>
</tr>
<c:forEach var="row" items="${[Link]}">
<tr>
<td><c:out value="${[Link]}"/></td>
<td><c:out value="${[Link]}"/></td>
<td><c:out value="${[Link]}"/></td>
<td><a href="[Link]?pid=<c:out
value="${[Link]}"/>">update</a></td>
<td><a href="javascript:confirmGo('Sure to delete this
record?','[Link]?pid=<c:out value="${[Link]}"/>')">Delete</a></td>
</tr>
</c:forEach>
</table>
</form>
<a href="[Link]">Go Home</a>
</center>
</body>
</html>
[Link]
<%@page contentType="text/html" pageEncoding="UTF-8"
import="[Link].*,[Link].*,[Link].*" %>
<%@taglib uri="[Link] prefix="c" %>
<%@taglib uri="[Link] prefix="sql" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Delete JSP Page</title>
</head>
<body>
<sql:setDataSource var="dbsource" driver="[Link]"
url="jdbc:mysql://localhost/test" user="root" password="root"/>
<sql:update dataSource="${dbsource}" var="count">
DELETE FROM product_details120 WHERE pid='${[Link]}';
Page No : 68
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
</sql:update>
<c:if test="${count>=1}">
<font size="5" color="green"> Data deleted successfully! </font><br>
<a href="[Link]">Go Home</a>
</c:if>
</body>
</html>
OUTPUT:
Page No : 69
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
d. Create a Currency Converter application using EJB.
[Link]
package mybean;
import [Link];
@Stateless
public class CCBean {
public
CCBean(){}
public double r2Dollar(double r){
return r/83.90;
}
public double d2Rupees(double d){
return d*83.90;
}
}
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet(urlPatterns = {"/ccservlet"})
public class ccservlet extends HttpServlet {
@EJB
CCBean obj;
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
Page No : 70
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
[Link]("text/html;charset=UTF-8");
try (PrintWriter out = [Link]()) {
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet ccservlet</title>");
[Link]("</head>");
[Link]("<body>");
double amt = [Link]([Link]("amount"));
if ([Link]("r1").equals("r2d"))
{
double result = obj.r2Dollar(amt);
[Link]("Dollar = "+result);
}
else{
double result = obj.d2Rupees(amt);
[Link]("Rupees = "+result);
}
[Link]("</body>");
[Link]("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign
on the left to edit the code.">
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
Page No : 71
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
}// </editor-fold>
}
[Link]
<!DOCTYPE html>
<html>
<head>
<title>Module 3d</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="ccservlet">
Enter amount <input type="text" name="amount"><br>
Select Conversion Type:<input type="radio" name="r1" value="r2d">Rupees to
Dollar <input type="radio" name="r1"
value="d2r">Dollar to Rupees
<br>
<input type="reset" value="RESET">
<input type="Submit" value="CONVERT">
</form>
</body>
</html>
Page No : 72
Advanced Java –Rushil Bhimani - 53003220091 - Batch 4
OUTPUT:
Page No : 73