Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Name: Anuj Balmiki Subject: CJ

Class: SYIT Date: 26/09/2021


Practical 11: Implement the following JSP applications.
Practical: 11a
Aim:
a. Develop a simple JSP application to display values obtained from the use of
b. intrinsic objects of various types. Develop a simple JSP application to pass values from
one page to another with
Code:
index.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>
    <div>To go to different pages....</div>
    <a href="Page1.jsp">Next</a><br>
    <a href="User_input.html">User Input</a><br>
    <a href="display.jsp">Display</a><br><br><br>

    <div style="width: 300px; height: 100px; background: red; padding: 10px; padding-top: 60px; border-radius: 20px;
            background: rgb(34,193,195);
            background: radial-gradient(circle, rgba(34,193,195,1) 0%, rgba(45,253,110,1) 100%);
            margin-left: 220px">
        <form action="Page1.jsp" method="post">
            Enter the number: <input type="number" name="number"><br>
            <input type="submit" value="Submit"><br>
        </form>
    </div>
</body>
</html>

Code:
Page1.jsp
<%--
Document : page1.jsp
Created on : Sep 18, 2021, 3:11:50 PM
Author : Anuj
--%>

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


<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Factorial Page</title>
</head>
<body>
<%
int num=(Integer.parseInt(request.getParameter("number")));
%>
<%! int numberfactorial(int number) {
if (number == 1)
return number;
else
return number * numberfactorial(number - 1);
}
%>
<div style="width: 300px; height: 150px; background: red; padding: 10px;border-radius: 20px;
background: rgb(34,193,195);
background: radial-gradient(circle, rgba(34,193,195,1) 0%, rgba(45,253,110,1) 100%);
margin-left: 220px; margin-top: 120px">
<h1>Factorial Calculation!</h1>
<%
out.println("The factorial of "+num+" is " + numberfactorial(num));
%>
</div>

</body>
</html>

Output:
Practical: 11 b & c
Aim:
b. intrinsic objects of various types. Develop a simple JSP application to pass values from
one page to another with
c. Create a registration and login JSP application to register and authenticate the
Code:
User_input.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>User Input</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div style="width: 300px; height: 150px; background: red; padding-left: 20px; padding-top: 5px;border-radius: 20px;
background: rgb(34,193,195);
background: radial-gradient(circle, rgba(34,193,195,1) 0%, rgba(45,253,110,1) 100%);
margin-left: 220px; margin-top: 120px">
<h4 style="height: 5px">Enter Username & Password</h4>
<form action="authenticate.jsp" method="post">
<input type="text" name="u_nm"><br>
<input type="text" name="u_ps"><br>
<input type="submit" value="Submit"><br>
</form>
<a href="register.html">Register</a>
</div>
</body>
</html>

authenticate.jsp:
<%--
Document : authenticate
Created on : Sep 20, 2021, 12:27:20 PM
Author : Anuj
--%>
<%@page import="java.sql.*"%>

<%@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>
<div style="width: 250px; height: 100px; background: red; padding-left: 100px; padding-top: 60px; border-radius: 20px;
background: rgb(34,193,195);
background: radial-gradient(circle, rgba(34,193,195,1) 0%, rgba(45,253,110,1) 100%);
margin-left: 220px; margin-top: 120px">
<%
try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/anuj","root","");
out.println("Connection done");
out.println("<br>");

PreparedStatement ps=con.prepareStatement("Select * from user");


ResultSet rs=ps.executeQuery();
String uname=request.getParameter("u_nm");
String upass=request.getParameter("u_ps");

Boolean found=false;
String user=null;
while(rs.next()){
if(rs.getString(2).equals(uname) && rs.getString(3).equals(upass)){
found=true;
user=rs.getString(1);
break;
}
}
if(found){
out.println("Welcome "+user);
}
else{
out.println("Invalid User");
}

}
catch(Exception e){
out.println(e);
}
%>
</div>
</body>
</html>

Output:
register.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>Register</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div style="width: 300px; height: 200px; background: red; padding: 10px;border-radius: 20px;
background: rgb(34,193,195);
background: radial-gradient(circle, rgba(34,193,195,1) 0%, rgba(45,253,110,1) 100%);
margin-left: 220px; margin-top: 100px">
<h1 style="height: 5px">Register</h1><br>
<form action="register.jsp" method="post">
Name: <input type="text" name="nm"><br>
User Name: <input type="text" name="u_nm"><br>
Password: <input type="text" name="u_ps"><br>
<input type="submit" value="Register"><br>
<input type="reset" value="Clear"><br>
</form>
</div>
</body>
</html>

register.jsp:
<%--
Document : register
Created on : Sep 21, 2021, 9:14:18 AM
Author : Anuj
--%>
<%@page import="java.sql.*"%>
<%@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>
<div style="width: 250px; height: 100px; background: red; padding-left: 100px; padding-top: 70px; border-radius: 20px;
background: rgb(34,193,195);
background: radial-gradient(circle, rgba(34,193,195,1) 0%, rgba(45,253,110,1) 100%);
margin-left: 220px; margin-top: 120px">
<%
try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/anuj","root","");
//out.println("Connection done");

PreparedStatement ps=conn.prepareStatement("Insert into user values(?,?,?)");


ps.setString(1,request.getParameter("nm"));
ps.setString(2,request.getParameter("u_nm"));
ps.setString(3,request.getParameter("u_ps"));

int a=ps.executeUpdate();
out.println(a+" Records Inserted");
}catch(Exception e){
out.println(e);
}
%>
</div>
</body>
</html>

Output:

You might also like