Java Document by Harsh-1
Java Document by Harsh-1
BY
Seat Number:
Servlet1.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;
@WebServlet(urlPatterns = {"/Servlet1"})
public class Servlet1 extends HttpServlet
{
protected void doPost (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
Output:
If user is Valid:
If user is Invalid:
Servlet2.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.*;
@WebServlet(urlPatterns = {"/Servlet2"})
public class Servlet2 extends HttpServlet
{
protected void doPost (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try
{
Connection con = DriverManager.getConnection
("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
PreparedStatement ps = con.prepareStatement("INSERT INTO STUDENT VALUES
(?,?,?,?)");
ps.setString(1,uname);
ps.setString(2,pass);
ps.setString(3,email);
ps.setString(4,country);
ps.executeUpdate();
out.println("<center><h1>Registration Successful</h1></center>");
}
catch(SQLException e)
{
System.out.println(e);
}
}
}
}
Output:
Servlet3.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.*;
@WebServlet(urlPatterns = {"/Servlet3"})
public class Servlet3 extends HttpServlet
{
try
{
Connection con
=DriverManager.getConnection("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
PreparedStatement ps = con.prepareStatement("INSERT INTO Emp VALUES
(?,?,?,?,?,?)");
ps.setString(1,name);
ps.setInt(2,age);
ps.setString(3,address);
ps.setString(4,hobbies);
ps.setString(5,gender);
ps.setString(6,qual);
ps.executeUpdate();
Database:
Servlet4.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;
@WebServlet(urlPatterns = {"/Servlet4"})
public class Servlet4 extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String uname=request.getParameter("u1");
String pass=request.getParameter("p1");
Output:
If user is Valid:
If user is Invalid:
Servlet5.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;
@WebServlet(urlPatterns = {"/Servlet5"})
public class Servlet5 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
out.println("<center><h1>Details</h1>");
out.println("<h2>Name is : " + name);
out.println("<br>Age is : " + age);
out.println("<br>Address is : " + address);
out.println("<br>Hobbies is : " + hobbies);
out.println("<br>Gender is : " + gender);
out.println("<br>Qualification is : " + qual);
out.println("</h2></center>");
} }}
Output:
Servlet7.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 javax.servlet.RequestDispatcher;
@WebServlet(urlPatterns = {"/Servlet7"})
public class Servlet7 extends HttpServlet
{
String operation=request.getParameter("op");
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request,response);
switch(operation)
{
case "add":
{
out.println("<center><h2>Addition is : "+ (num1+num2) +"</h2></center>");
break;
}
case "sub":
{
out.println("<center><h2>Subtraction is : "+ (num1-num2) +"</h2></center>");
break;
}
case "multi":
{
out.println("<center><h2>Multiplication is : "+ (num1*num2)
+"</h2></center>");
break;
}
case "div":
{
out.println("<center><h2>Division is : "+ (num1/num2) +"</h2></center>");
break;
}
default:
{
out.println("<center><h2>Invalid Inputs</h2></center>");
break;
}
}
}
Output:
Success.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h2>Welcome</h2>
</center>
</body>
</html>
Servlet12.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
@WebServlet(urlPatterns = {"/S12"})
public class S12 extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String uname=request.getParameter("u1");
String pass=request.getParameter("p1");
else if (uname.equals(""))
{
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request,response);
out.println("<center><h2>Please Enter Username</h2></center>");
}
else if (pass.equals(""))
{
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request,response);
out.println("<center><h2>Please Enter Password</h2></center>");
}
else
{
RequestDispatcher rd=request.getRequestDispatcher("success.html");
rd.forward(request,response);
}
Output:
Servlet13.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.*;
@WebServlet(urlPatterns = {"/Servlet13"})
public class Servlet13 extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String uname=request.getParameter("u1");
String pass=request.getParameter("p1");
ps.setString(1,uname);
ps.setString(2,pass);
if (rs.next())
{
out.println("<center><h2>Valid User</h2></center>");
}
else
{
out.println("<center><h2>Invalid User</h2></center>");
}
rs.close();
ps.close()
}
catch(SQLException e)
{
System.out.println(e);
}
}
}
}
Output:
If user is Valid:
Database:
A: Create a servlet that uses Cookies to store the number of times a user has
visited servlet.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Practical 6</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<form method="Post" action="Servlet6">
UserName <input type="text" name="u1">
<br><br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>
Servlet6.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 javax.servlet.http.Cookie;
@WebServlet(urlPatterns = {"/Servlet6"})
public class Servlet6 extends HttpServlet
{
static int i=1;
if (j==1)
{
out.println("<center><h1>Welcome "+ uname + "</h1></center>");
}
else
{
out.println("<center><h1>You have Visited "+j+" Times </h1></center>" );
}
i++;
}
}
}
Output:
Servlet8.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 javax.servlet.http.HttpSession;
@WebServlet(urlPatterns = {"/Servlet8"})
public class Servlet8 extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String n=request.getParameter("u1");
HttpSession session=request.getSession();
session.setAttribute("user",n);
}
}
}
S8.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 javax.servlet.http.HttpSession;
@WebServlet(urlPatterns = {"/S8"})
public class S8 extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
HttpSession session=request.getSession(false);
String s=(String)session.getAttribute("user");
out.println("Welcome "+ s);
}
}
}
Output:
Servlet1:
Success.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h2>Welcome</h2>
</center>
</body>
</html>
@WebServlet(urlPatterns = {"/Servlet9"})
public class Servlet9 extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String uname=request.getParameter("u1");
String pass=request.getParameter("p1");
if (pass.equals("servlet"))
{
RequestDispatcher rd=request.getRequestDispatcher("success.html");
rd.forward(request,response);
}
else
{
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request,response);
out.println("<center><h2>Invalid user</h2></center>");
}
}
}
}
If user is Valid:
If user is Invalid:
Servlet10.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 javax.servlet.http.HttpSession;
@WebServlet(urlPatterns = {"/Servlet10"})
public class Servlet10 extends HttpServlet
{
static int i=1;
session.invalidate();
i++;
}
}
}
Output:
Servlet11.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;
@WebServlet(urlPatterns = {"/Servlet11"})
public class Servlet11 extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String q1=request.getParameter("r1");
String q2=request.getParameter("r2");
String q3=request.getParameter("r3");
int score=0;
}
}
}
Output:
implicit.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>
<center>
<h1>Use of Intrinsic Objects in JSP </h1>
<h2>Request Object</h2>
Query String<%=request.getQueryString() %><br>
Context Path<%=request.getContextPath() %><br>
Remote Host<%=request.getRemoteHost() %><br><br>
<h2>Response Object</h2>
<h2>Session Object</h2>
ID<%=session.getId()%><br>
Creation Time<%=new java.util.Date(session.getCreationTime()) %><br>
Last Access Time<%=new java.util.Date(session.getLastAccessedTime())%>
</center>
</body>
</html>
Output:
check.jsp
<%@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>
<%
String uname=request.getParameter("n1");
String pass=request.getParameter("p1");
try
{
Connection con=DriverManager.getConnection
("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
PreparedStatement ps= con.prepareStatement("SELECT * FROM STUDENT_Table
WHERE NAME= ? and PASSWORD= ?");
ResultSet rs =ps.executeQuery();
if(rs.next())
{
out.println("<center><h1>Login Successful</h1>");
%>
<a href="update.html">Update Values</a>
<%
out.println("</center>");
}
else
{
out.println("<center><h1>Login Fails</h1>");
%>
<a href="index.html">Try again</a>
<%
out.println("</center>");
}
rs.close();
ps.close();
}
catch(SQLException e)
{
System.out.println(e);
}
%>
</body>
</html>
update.html
<!DOCTYPE html>
<html>
<head>
<title>Update Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Update Data</h1>
<form method="post" action="update.jsp">
update.jsp
<%@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>
<%
int id=Integer.parseInt(request.getParameter("id"));
String uname=request.getParameter("n1");
String pass=request.getParameter("p1");
String course=request.getParameter("c1");
String hobbies=request.getParameter("h1");
try
{
Connection con=DriverManager.getConnection
("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
PreparedStatement ps= con.prepareStatement("UPDATE STUDENT_Table SET
NAME=?, PASSWORD=?,COURSE=?,HOBBIES=? WHERE ID=?");
ps.setString(1,uname);
ps.setString(2,pass);
ps.setString(3,course);
ps.setString(4,hobbies);
ps.setInt(5,id);
ps.executeUpdate();
ps.close();
Output:
If user is Invalid:
If user is Valid:
implicit.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>
<center>
<%
String uname = request.getParameter("n1");
String pass = request.getParameter("p1");
session.setAttribute("Username", uname);
session.setAttribute("Password", pass);
response.setContentType("text/html");
response.setStatus(200);
%>
%>
<h2>Session Details:</h2>
<h3>
Session ID: <%= session.getId() %><br><br>
Username from session: <%= session.getAttribute("Username") %><br><br>
Password from session: <%= session.getAttribute("Password") %>
</h3>
</center>
</body>
</html>
Output:
delete.jsp
<%@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>
<%
int id=Integer.parseInt(request.getParameter("id"));
try
{
Connection
con=DriverManager.getConnection("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
PreparedStatement ps= con.prepareStatement("DELETE FROM STUDENT_Table
WHERE ID= ?");
if(rowdelete>0)
{
out.println("<center><h1>Employee record Deleted Successfully</h1></center>");
}
else
{
out.println("<center><h1>No such Employee ID</h1>");
%>
<a href="index.html">Try again</a>
<%
out.println("</center>");
}
ps.close();
con.close();
}
catch(SQLException e)
{
System.out.println(e);
}
%>
</body>
</html>
Output:
check.jsp
<%@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>
<%
String uname=request.getParameter("n1");
String pass=request.getParameter("p1");
try {
Connection con=DriverManager.getConnection
("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
PreparedStatement ps= con.prepareStatement("SELECT * FROM STUDENT_Table
WHERE NAME= ? and PASSWORD= ?");
ps.setString(1,uname);
ps.setString(2,pass);
ResultSet rs =ps.executeQuery();
Output:
If user is Valid:
If user is Invalid:
View.jsp
<%@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>
<style>
table,th,td
{
border: 2px solid black;
}
</style>
</head>
<body>
<%
int d_id=Integer.parseInt(request.getParameter("id"));
try
{
ps.setInt(1,d_id);
ResultSet rs= ps.executeQuery();
%>
<center>
<h2>Employee Details</h2>
<table>
<tr>
<th>D_Id</th>
<th>D_Name</th>
<th>Emp_Name</th>
<th>Emp_Age</th>
<th>Emp_Salary</th>
</tr>
<%
while(rs.next())
{
out.println("<tr>");
out.println("<td>"+rs.getInt(1)+"</td>");
out.println("<td>"+rs.getString(2)+"</td>");
out.println("<td>"+rs.getString(3)+"</td>");
out.println("<td>"+rs.getInt(4)+"</td>");
out.println("<td>"+rs.getInt(5)+"</td>");
out.println("</tr>");
}
%>
</table>
</center>
<%
rs.close();
ps.close();
con.close();
}
catch(SQLException e)
{
System.out.println(e);
}
%>
Output:
Employee Records:
Database:
login.html
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<center>
<h1>Login</h1>
<form method="post" action="check.jsp">
Enter Username: <input type="text" name="n1"><br><br>
Enter Password: <input type="password" name="p1"><br><br>
<input type="submit" value="Login"> <a href="index.html"> Register</a>
</form>
registration.jsp
<%@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>
<%
int id=Integer.parseInt(request.getParameter("id"));
String uname=request.getParameter("n1");
String pass=request.getParameter("p1");
String course=request.getParameter("c1");
String hobbies=request.getParameter("h1");
try
{
Connection con
=DriverManager.getConnection("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
PreparedStatement ps = con.prepareStatement("INSERT INTO STUDENT_Table
VALUES (?,?,?,?,?)");
ps.setInt(1,id);
ps.setString(2,uname);
ps.setString(3,pass);
ps.setString(4,course);
ps.setString(5,hobbies);
ps.executeUpdate();
out.println("<center><h1>Registration Successful</h1>");
%>
<a href="login.html">Login</a>
<%
out.println("</center>");
ps.close();
con.close();
}
Check.jsp
<%@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>
<%
String uname=request.getParameter("n1");
String pass=request.getParameter("p1");
try
{
Connection con =DriverManager.getConnection
("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
PreparedStatement ps = con.prepareStatement("SELECT * FROM STUDENT_Table
WHERE NAME= ? and PASSWORD= ?");
ps.setString(1,uname);
ps.setString(2,pass);
if (rs.next())
{
out.println("<center><h1>Login Successful</h1>");
out.println("<br><h2>Welcome "+ uname+"</h2></center>");
}
else
{
out.println("<center><h1>Login Fails</h1>");
%>
<a href="login.html">Try again</a>
Output:
Login:
If user is Invalid:
Database:
registration.jsp
<%@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>
<%
int num=Integer.parseInt(request.getParameter("num"));
String name=request.getParameter("n1");
int sal=Integer.parseInt(request.getParameter("s1"));
try
{
Connection con =DriverManager.getConnection
("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
Output:
Database:
check.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 uname=request.getParameter("n1");
String pass=request.getParameter("p1");
Output:
If user is Vaild:
If user is Invaild:
check.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("n1");
int age = Integer.parseInt(request.getParameter("a1"));
String hob = request.getParameter("h1");
String gender = request.getParameter("r1");
boolean isValid = true;
if(name == null || age <=0 || gender ==null || hob == null) {
isValid = false;
Output:
JSTL.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@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>
<center>
<h2>CORE Tag</h2>
<%
String uname=request.getParameter("n1");
%>
<c:set var="name" value="<%=uname%>"/>
<c:choose>
<c:when test="${name eq 'Harsh'}">
<h2>FUNCTION Tag</h2>
<c:set var="string1" value="HARSH"/>
Output:
Update.jsp
<%@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>
<%
int num=Integer.parseInt(request.getParameter("num"));
String name=request.getParameter("n1");
int age=Integer.parseInt(request.getParameter("a1"));
String des=request.getParameter("d1");
int sal=Integer.parseInt(request.getParameter("s1"));
ps.setString(1,name);
ps.setInt(2,age);
ps.setString(3,des);
ps.setInt(4,sal);
ps.setInt(5,num);
int rowupdate=ps.executeUpdate();
if(rowupdate>0)
{
out.println("<center><h1>Employee record Update Successfully</h1></center>");
}
else
{
out.println("<center><h1>No such Employee Exist</h1>");
%>
<a href="index.html">Try again</a>
<%
out.println("</center>");
}
ps.close();
con.close();
}
catch(SQLException e)
{
System.out.println(e);
}
%>
</body>
</html>
If Employee Exist:
check.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@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 uname=request.getParameter("n1");
String pass=request.getParameter("p1");
<c:if test="${sessionScope.authenticated}">
<h2>Welcome, ${sessionScope.username}!</h2>
<p>You have successfully logged in.</p>
</c:if>
<c:if test="${!sessionScope.authenticated}">
<h2>Login Failed</h2>
<p>Invalid username or password. Please try again.</p>
<a href="index.html">Go back to Login</a>
</c:if>
</center>
</body>
</html>
Output:
If user is Vaild:
If user is Invaild:
display.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("n1");
out.println("<center><h1>Hello "+name+ "</center></h1>");
%>
<jsp:include page="include.jsp">
<jsp:param name="user" value= "<%= name %>" />
</jsp:include>
</body>
</html>
display.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("n1");
out.println("<center><h1>Hello "+name+ "</center></h1>");
%>
<jsp:forward page="forward.jsp">
<jsp:param name="user" value= "<%= name %>" />
</jsp:forward>
</body>
</html>
forward.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>
<center>
<h1>
<%
String n = request.getParameter("user");
out.println("Hello "+n);
%>
</h1>
Output:
Output:
Convert.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"
import="mypackage.MoneyConverter;"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
float currency = Float.parseFloat(request.getParameter("e1"));
String str = request.getParameter("r1");
MoneyConverter ob = new MoneyConverter();
if(str.equals("d2r")) {
float usd = ob.DtoR(currency);
out.println("<center><h3>"+currency + " $ = " +usd +" Rs</h3></center>");
Output:
JSTL.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"
import="mypackage.RoomReservation"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
int rnum = Integer.parseInt(request.getParameter("num"));
String name = request.getParameter("name");
String rtype= request.getParameter("n1");
ps.setInt(1,rnum);
ps.setString(2,name);
ps.setString(3,rtype);
ps.executeUpdate();
ps.close();
con.close();
}
catch(SQLException e)
{
System.out.println(e);
}
return "Room Booked Successfully.";
}
}
Database:
3. Which component is used to compile, debug and execute the java programs? <br>
<input type="radio" name="r3" value="a">A) JRE <br>
<input type="radio" name="r3" value="b">B) JIT<br>
<input type="radio" name="r3" value="c">C) JDK<br>
<input type="radio" name="r3" value="d">D) JVM<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
check.jsp
<%@page contentType="text/html" pageEncoding="UTF-8" import="mypackage.quiz;"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
insert.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"
import="mypackage.product_data;"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String pn = request.getParameter("n1");
double pr = Double.parseDouble(request.getParameter("p1"));
int pq = Integer.parseInt(request.getParameter("q1"));
@Stateless
public class product_data {
ps.setString(1,d1);
ps.setDouble(2,d2);
ps.setInt(3,d3);
ps.executeUpdate();
ps.close();
con.close();
}
catch(SQLException e)
{
System.out.println(e);
}
return "Data Inserted Successfully.";
}
}
Database:
calculate.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"
import="mypackage.InterestCalculator;"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
double p=Double.parseDouble(request.getParameter("p1"));
double r=Double.parseDouble(request.getParameter("r1"));
double y=Double.parseDouble(request.getParameter("y1"));
Output:
insert.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"import="mypackage.register"%>
<!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("n1");
int roll=Integer.parseInt(request.getParameter("r1"));
int age=Integer.parseInt(request.getParameter("a1"));
String course=request.getParameter("c1");
String hobbies=request.getParameter("h1");
import javax.ejb.Stateless;
import java.sql.*;
@Stateless
public class register {
public String add_data(String name, int roll, int age, String course,String hobbies)
{
try
{
Connection con = DriverManager.getConnection
("jdbc:derby://localhost:1527/TYIT","tyit","tyit");
ps.setString(1,name);
ps.setInt(2,roll);
ps.setInt(3,age);
ps.setString(4,course);
ps.setString(5,hobbies);
ps.executeUpdate();
ps.close();
con.close();
}
catch(SQLException e)
{
System.out.println(e);
}
Output:
Database:
3. Which component is used to compile, debug and execute the java programs? <br>
<input type="radio" name="r3" value="a">A) JRE <br>
<input type="radio" name="r3" value="b">B) JIT<br>
<input type="radio" name="r3" value="c">C) JDK<br>
<input type="radio" name="r3" value="d">D) JVM<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
check.jsp
<%@page contentType="text/html" pageEncoding="UTF-8" import="mypackage.quiz;"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
ResultSet rs=ps.executeQuery();
while(rs.next())
{
if (rs.getString(1).equals(arr[i]) )
{
score+=1;
}
i++;
}
ps.close();
con.close();
}
catch(SQLException e)
{
Output:
Database:
marks_entry.jsp
<%@page contentType="text/html" pageEncoding="UTF-8" import="insert_data.entry;"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
int SPD=Integer.parseInt(request.getParameter("m1"));
int IOT=Integer.parseInt(request.getParameter("m2"));
int AWP=Integer.parseInt(request.getParameter("m3"));
String result=ob.insert_marks(SPD,IOT,AWP,AI,EJava);
out.println("<center><h2>" + result + "</center></h2>");
%>
</body>
</html>
ps.setInt(1,m1);
ps.setInt(2,m2);
ps.setInt(3,m3);
ps.setInt(4,m4);
ps.setInt(5,m5);
ps.executeUpdate();
ps.close();
con.close();
}
catch(SQLException e)
{
System.out.println(e);
}
return "Marks Inserted Successfully.";
Output:
Database:
result.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"
import="mypacakage.addition;"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
int num1 = Integer.parseInt(request.getParameter("n1"));
int num2 = Integer.parseInt(request.getParameter("n2"));
addition a = new addition();
int ans = a.Add(num1, num2);
out.println("<center><h1>Addition is : "+ ans +"</h1></center>");
%>
</body>
</html>
import javax.ejb.Stateless;
@Stateless
public class addition
{
public int Add(int n1 , int n2)
{
return n1 + n2;
}
Output:
NewServlet.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 javax.ejb.EJB;
import mypackage.hitcount;
@WebServlet(urlPatterns = {"/NewServlet"})
public class NewServlet extends HttpServlet {
}
}
}
Output: