Professional Documents
Culture Documents
Ej Final
Ej Final
Practical No.1
Source Code:
2) Below Window appears then select Java Web in Categories section and select
Web Application in Projects section and click on Next.
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
3) Enter your Project Name and you can also change your Project Location if
necessary and then click Next.
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
index.html
<html>
<body>
<form action="" method="post">
<b> Enter Number1</b> <input type="number" name="N1"><br/><br/>
<b> Enter Number2</b> <input type="number" name="N2"><br/><br/>
Select operation <select name ="Cal">
<option value="Add"> + </option>
<option value="Subtract"> - </option>
<option value="Divide"> / </option>
<option value="Multiply"> *</option>
<option value="Mod"> % </option>
</select>
<input type="submit" value="Calculate">
</form>
</body>
</html>
1)Right click on the working Project and click on New and then click on
Servlet.
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
2) Enter Class Name and Package according to your preference and click on Next.
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
CalculatorServlet.java
package pck1;
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;
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
if(operation.equals("Add"))
{
out.println("Addition of num1 & num2: " + (num1+num2));
}
else if(operation.equals("Subtract"))
{
out.println("Subtraction of num1 & num2: " + (num1-num2));
}
else if(operation.equals("Divide"))
{
out.println("Division of num1 & num2: " + (num1/num2));
}
else if(operation.equals("Multiply"))
{
out.println("Multiplication of num1 & num2: " + (num1*num2));
}
else if(operation.equals("Mod"))
{
out.println("Mod of num1 & num2: " + (num1%num2));
}
else
{
out.println("please select the operation");
}
}
}
Output:
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
B] Aim: create a servlet for login page if username and password are correct then it says
message “Hello” <username> else a message login failed]
index.html
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="LoginServlet" method="post">
Enter Username:<input type="text" name="t1" placeholder="username"><br/><br/>
Enter Password:<input type="password" name="t2" placeholder="***********"
><br/><br/>
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
LoginServlet.java
package pck2;
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;
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
}
}
Output:
C] Aim: 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 database
index.html
<!DOCTYPE html>
<html>
<head>
<title> Employee Application</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="s1">
Enter Username: <input type="text" name="t1" placeholder="Name"><br/>
Enter Password: <input type="password" name="t2" placeholder="9886***"><br/>
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/b2_registrationdetails","roo
t","Amar@0000");
PreparedStatement pst=con.prepareStatement("insert into b2_user value (?,?,?,?)");
pst.setString(1, n);
pst.setString(2, p);
pst.setString(3, eid);
pst.setString(4, c);
int row=pst.executeUpdate();
if (row==1)
{
out.println("record inserted successfully");
}
else
{
out.println("not inserted successfully");
}
con.close();
pst.close();
}
catch (Exception e){
out.println(e);
}
} }
Steps for connecting database(//(Add Screenshot for each step)
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
2) Under Services Section expand the Database section and right click the MySQL
Server at localhost:3306 to connect.
3) Click on Connect.
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
5) Expand MySQL Server at localhost:3306 and right click on the database you
created and click on Connect.
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
7) After expanding the database, you can see your table under Tables section right
click your table name and click on View Data to view the table.
9) Now come back in the Project section and right click on Libraries and click on Add
JAR Folder.
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
Output:
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
ff
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
Practical No. 2
Topic: Implement the following servlet application with cookies and session
A] Aim: using request dispatcher user interface create a servlet which will validate the
password enter by the user ,if the user has enter “ servlet” as password ,then he will be
forwarded to welcome servlet else the user will stay on the index.html page and error
message will be display]
index.html
<!DOCTYPE html>
<html>
<head>
<title>LOGIN</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="s1" method="post">
Enter Username:<input type="text" name="t1" placeholder="username"><br/><br/>
Enter Password:<input type="password" name="t2" placeholder="***********"
><br/><br/>
<input type="submit" value="Click to Login" class="btn">
<input type="reset" value="Reset" class="btn">
</form>
</body>
s1.java
package pkgc;
import javax.servlet.RequestDispatcher;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "Login", urlPatterns = {"/Login"})
public class s1 extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String psw = request.getParameter("t2");
if(psw.equals("servlet"))
{
RequestDispatcher rd = request.getRequestDispatcher("s2");
rd.forward(request, response);
}
else
{
RequestDispatcher pd = request.getRequestDispatcher("index.html");
pd.include(request, response);
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
}
}
Output:
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
B] Aim: Create a servlet that uses cookies to store the number of times a user has visited
servlet
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="p1" method="post">
Enter Username: <input type="text" name="t1" placeholder="username"><br/>
<input type="submit" value="Submit">
</form>
</body>
</html>
p1.java
package pack;
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 javax.servlet.http.Cookie;
public class p1 extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Page 1</title></head>");
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
out.println("<body bgcolor=cyan>");
String un = request.getParameter("t1");
out.println("<h1> Welcome </t>" + un + "</h1>");
Cookie ck1 = new Cookie("Username",un);
Cookie ck2 = new Cookie("Visit","1");
response.addCookie(ck1);
response.addCookie(ck2);
out.println("<a href=P2>Click here to Visit P2</a>");
}
}
p2.java
package pack;
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 = {"/P2"})
public class P2 extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html><head><title>page2</title></head>");
Cookie []ck=request.getCookies();
for(int i=0;i<ck.length;i++)
{
if(ck[i].getName().equals("visit"))
{
if(Integer.parseInt(ck[i].getValue())<=1)
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
{
out.println("<body bgcolor= red>");
int count = Integer.parseInt(ck[i].getValue())+1;
out.println("<b>Visit "+ count +"</b>");
ck[i]=new Cookie("visit",count+" ");
}
response.addCookie(ck[i]);
}
else
{
out.println("<body bgcolor= green>");
out.println("<i>u have visited this page before</i>");
}
}
out.println("<a href=P3>click here to visit p3</a>");
}
}
p3.java
package pack;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Cookie;
@WebServlet(urlPatterns = {"/P3"})
public class P3 extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Page 3</title></head>");
Cookie []ck=request.getCookies();
for(int i=0;i<ck.length;i++)
{
if(ck[i].getName().equals("Visit"))
{
out.println("<body bgcolor=yellow>");
int count=Integer.parseInt(ck[i].getValue())+1;
out.println("<b>Visit"+count+"</b>");
ck[i]=new Cookie("Visit",count+"");
response.addCookie(ck[i]);
}
else
{
out.println(ck[i].getName()+"="+ck[i].getValue());
out.println("<body bgcolor=green>");
out.println("<a href=P2>Click Here To Visit Pg2</a>");
}
}
}
}
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
Output:
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
C] Aim: 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
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="p1" method="post">
Log In:<input type="text" name="t1" placeholder="username"><br/><br/>
<input type="submit" value="Submit" class="btn">
</body>
</html>
p1.java
package pkgc;
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 javax.servlet.http.HttpSession;
import java.util.Date;
public class p1 extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
if(ob1.isNew())
{
out.println("<body bgcolor=yellow>");
String uname = request.getParameter("t1");
ob1.setAttribute("Username", uname);
ob1.setAttribute("visit", "1");
out.println("<h1>Welcome "+uname+"</h1>");
}
else
{
out.println("<body bgcolor=aqua>");
int visit = Integer.parseInt((String)ob1.getAttribute("visit"))+1; //Here we convert
string to integer
out.println("<h1>Welcome back "+ob1.getAttribute("Username")+" </h1>");
out.println("<h2>Visit : "+visit+"</h2>");
ob1.setAttribute("visit", visit+"");
}
out.println("<h2>Session Id : "+ob1.getId()+"<br> Timeout:
"+ob1.getMaxInactiveInterval()+" </h2>");
out.println("<h2>Last Accessed Time:"+ob1.getLastAccessedTime()+"<br> Creation
Time:"+new Date(ob1.getCreationTime())+" </h2>");
out.println("<a href=p2>CLICK HERE TO VISIT PAGE 2 </a>");
out.println("<br><a href=p3>CLICK HERE TO VISIT PAGE 3 </a>");
out.println("<br><a href=LogOut>CLICK HERE TO LOGOUT PAGE </a>");
}
}
p2.java
package pkgc;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
public class p2 extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
if(ob2.isNew())
{
out.println("<body bgcolor=blue>");
String uname = request.getParameter("t1");
ob2.setAttribute("Username", uname);
ob2.setAttribute("visit", "1");
out.println("<h1>Welcome "+uname+"</h1>");
}
else
{
out.println("<body bgcolor=aqua>");
int visit = Integer.parseInt((String)ob2.getAttribute("visit"))+1; //Here we convert
string to integer
out.println("<h1>Welcome back " +ob2.getAttribute("Username")+" </h1>");
out.println("<h2>Visit : "+visit+"</h2>");
ob2.setAttribute("visit", visit+"");
}
out.println("<h2>Session Id : "+ob2.getId()+"<br> Timeout:
"+ob2.getMaxInactiveInterval()+" </h2>");
out.println("<h2>Last Accessed Time:"+ob2.getLastAccessedTime()+"<br> Creation
Time:"+new Date(ob2.getCreationTime())+" </h2>");
out.println("<a href=p1>CLICK HERE TO VISIT PAGE 1 </a>");
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
}
}
p3.java
package pkgc;
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 javax.servlet.http.HttpSession;
import java.util.Date;
public class p3 extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html><head><title> PAGE 3 </title></head></html>");
HttpSession ob3 = request.getSession(false);
if(ob3.isNew())
{
out.println("<body bgcolor=blue>");
ob3.setAttribute("Username", uname);
ob3.setAttribute("visit", "1");
out.println("<h1>Welcome "+uname+"</h1>");
}
else
{
out.println("<body bgcolor=aqua>");
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
}
}
Output:
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
Practical No.3
index.html
<html>
<head>
<title>Upload File</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="s1" method="post" enctype="multipart/form-data">
<h1> Upload File</h1>
Select File: <input type="file" name="file"><br/>
Destination: <input type="text" name="Destination"><br/>
<input type="submit" value="Upload">
</form>
</body>
</html>
s1.java
package pckk;
import javax.servlet.annotation.MultipartConfig;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
@MultipartConfig
public class s1 extends HttpServlet {
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
}
out.println("File Uploaded successfully");
}
catch(FileNotFoundException e)
{
out.println(e);
}}}
Output:
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
index.html
<!DOCTYPE html>
<html>
<head>
<title> Download File</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Download Application</h1>
click<a href="s1?filename=cat.jpg">Docs</a>
</body>
</html>
s1.java
package pkgc;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
response.setHeader("Content-Disposition","attachment:filename=\""+filename+"\"");
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
int i;
while((i=is.read())!=-1)
{
out.write(i);
}
is.close();
out.close();
}
Output:
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
Practical No. 4
index.html
<html>
<head>
<title>Login</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="jsp1.jsp" >
<h1>Registration form</h1>
Enter username : <input type="text" name="t1">
<input type="submit" value="Submit">
</form>
</body>
</html>
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
jsp1.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><b>Use of request object</b></h1>
Query String = <%=request.getQueryString()%><br><br>
Context Path = <%=request.getContextPath() %><br><br>
Remote Host = <%=request.getRemoteHost() %><br><br>
<h1><b>Use of Response object</b></h1>
Character Encoding type = <%=request.getCharacterEncoding() %><br><br>
content = <%=request.getContentType() %><br><br>
Local = <%=request.getLocale()%><br><br>
<h1><b>Use of Session object</b></h1>
session id = <%=session.getId()%><br><br>
Last accessed time = <%=session.getLastAccessedTime() %><br><br>
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
B] Aim: Create a registration and login JSP application to register and authenticate the user
based on username and password using JDBC.
index.html
<!DOCTYPE html>
<html>
<head>
<title>Registration Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
<body>
<form action="Registration.jsp">
Enter Username:
<input type="text" name="t1"><br/>
Enter Password:
<input type="password" name="t2"><br/>
Re-enter Password:
<input type="password" name="t3"><br/>
Enter Email ID:
<input type="email" name="t4"><br/>
Country Name:
<input type="text" name="t5"><br/>
<input type="submit" value="Register">
<input type="reset">
</form>
</body>
</html>
Login.html
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<meta charset="UTF-8">
</head>
<body>
<form>
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
Enter Username:
Enter Password:
<input type="reset">
</form>
</body>
</html>
Registartion.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>Registration Page</title>
</head>
<body>
<%
String n = request.getParameter("t1");
String p = request.getParameter("t2");
String ep = request.getParameter("t3");
String eid = request.getParameter("t4");
String c= request.getParameter("t5");
if(p.equals(ep))
{
try{
Class.forName("com.mysql.jdbc.Driver");// Register Driver
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
Connection con=
DriverManager.getConnection("jdbc:mysql://localhost:3306/employeeinfo","root","Ashish@
000");
//Established Connection
PreparedStatement pst = con.prepareStatement("insert into empdtls values(?,?,?,?)"); //
Create Statement
pst.setString(1,n);
pst.setString(2,p);
pst.setString(3,eid);
pst.setString(4,c);
int row = pst.executeUpdate();
if(row==1)
{
out.println("Record inserted Successfully!!!");
out.println("<a href=Login.html>Click here to Login</a>");
}
else
{
out.println("Registration Failed");
%>
<jsp:include page="index.html"/>
<%
}
con.close();
pst.close();
}
catch(Exception e)
{
out.println(e);
}
}
else
{
out.println("password mismatch");
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
%>
<jsp:include page="index.html"/>
<%
}
%>
</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>Login Page</title>
</head>
<body>
<%
String uname = request.getParameter("t1");
String psw = request.getParameter("t2");
try
{
Class.forName("com.mysql.jdbc.Driver");// Register Driver
Connection con=
DriverManager.getConnection("jdbc:mysql://localhost:3306/employeeinfo","root","Ashish@
000"); // Established Connection
PreparedStatement pst = con.prepareStatement("select password from empdtls
where name=?"); // Create Statement
pst.setString(1,uname);
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
{
out.println("Login Successful!!!");
}
else
{
out.println("Username & Password Incorrect");
%>
<jsp:include page="Login.html"/>
<%
}
}
}
catch(Exception e)
{
out.println(e);
}
%>
</body>
</html>
output:
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
Practical No. 5
index.html
<!DOCTYPE html>
<html>
<head>
<title>Update Query</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="box">
<center> <h1> Employee Records</h1></center>
<form method="post" action="update.jsp">
Enter Number :
<input type="text" name="eno" class="t"><br>
Enter name :
<input type="text" name="name" class="t"><br>
Enter Age :
<input type="text" name="age" class="t"><br>
Enter Designation :
<input type="text" name="desi" class="t"><br>
Enter Salary :
<input type="text" name="sal" class="t"><br>
<input type="submit" value="Update Query" class="submit">
<input type="reset" value= "Reset" class="re submit ">
</form>
</div>
</body>
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
</html>
update.jsp
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
pst2.setString(1,age);
pst2.setString(2,eno);
pst1.executeUpdate();
pst2.executeUpdate();
out.println("<h1>Employee "+name+"Exist!!<br> data updated
successfully!!</h1>");
}
else
{
out.println("<h1>Employee doesn't exist!!</h1>");
}
}
catch(Exception e)
{
out.println(e);
}
%>
</body>
</html>
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
Output:
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
Index.html
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
</form>
</body>
</html>
p1.jsp
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="p2.jsp" method="post">
<h1>This is login page</h1>
First Name:<input type="text" name="t1"><br><br>
Last Name:<input type="text" name="t2"><br><br>
<input type="submit" value="Submit Query">
</form>
</body>
</html>
p2.jsp
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
<p>MATH</p>
6+7 = ${6+7}<br>
8*9 = ${8*9}<br>
</p>
</body>
</html>
Output:
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
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="j1.jsp" method="post">
<h1>This is login page</h1>
First Name:<input type="text" name="t1"><br><br>
Last Name:<input type="text" name="t2"><br><br>
Enter Input:<input type="number" name="input"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
j1.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%--<%@ 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">
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
<title>JSP Page</title>
</head>
<body>
First Name:<b><c:out value="${param.t1}"></c:out></b><br>
Last Name:<b><c:out value="${param.t2}"></c:out></b><br>
Use of If:
<c:set var="mycount" value="25" ></c:set>
<c:if test = "${param.input==25}">
<b><c:out value="Your value is 25"></c:out></b>
</c:if><br><br>
Use of for Each:
<c:forEach var="count" begin="15" end="50" step="5">
<b><c:out value="${count}"></c:out></b>
</c:forEach>
</body>
</html>
Output:
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
Practical No. 6
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="s1">
Enter Amount: <input type="text" name="amt"><br>
Select Conversion Type:
<input type="radio" name="type" value="r2d"
checked>Rupees to Dollar<br/>
<input type="radio" name="type" value="d2r">Dollar to Rupees<br/>
<input type="submit" value="convert"><br/>
<input type="reset" value="reset"><br/>
</form>
</body>
</html>
s1.java
package pkg;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ejb.EJB;
import convertSession.convertBean;
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter() ;
double amt =
Double.parseDouble(request.getParameter("amt"));
if(request.getParameter("type").equals("r2d")){
out.println("<h1>"+amt+"Rupees="+obj.r2Dollar(amt)+"Dollars</h1> ");
}
else{
out.println("<h1>"+amt+"Dollar="+obj.d2Rupees(amt)+"Rupees</h1>" );
}
}
}
convert.Bean.java
package convertSession;
import javax.ejb.Stateless;
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
@Stateless
public class convertBean {
public convertBean(){}
public double r2Dollar(double r){
return r/79.88 ;
}
public double d2Rupees(double d){
return d*79.88 ;
}
}
Output:
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
index.html
<!DOCTYPE html>
<html>
<head>
<title>Room Reservation</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="box">
<form action="s1">
<h2>Welcome to our portal for Room Reservation</h2><br>
Select the type of room
<select name="roomType">
<option>Single</option>
<option>Double</option>
<option>Deluxe</option>
<option>Super Deluxe</option>
</select>
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
</div>
</body>
</html>
s1.java
package pck8;
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 javax.ejb.EJB;
import mybeanpack.RoomBookBean;
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
import javax.ejb.Stateless;
import java.sql.*;
@Stateless
public class RoomBookBean {
public RoomBookBean(){}
public String rBook(String rtype, String cname, String cmob) {
String confirmationMsg = "";
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/roomreservedb","root","Ashish
@000");
PreparedStatement pst = con.prepareStatement("select * from rbooking where
rtype=? and rstatus='Not Booked'");
pst.setString(1, rtype);
ResultSet rs = pst.executeQuery();
if(rs.next())
{
String roomno=rs.getString(1);
PreparedStatement stm1=con.prepareStatement("update rbooking set
custname=? where roomId=?");
stm1.setString(1, cname);
stm1.setString(2, roomno);
PreparedStatement stm2=con.prepareStatement("update rbooking set
custmobno=? where roomId=?");
stm2.setString(1, cmob);
stm2.setString(2, roomno);
PreparedStatement stm3=con.prepareStatement("update rbooking set
rstatus='Booked' where roomId=?");
stm3.setString(1, roomno);
stm1.executeUpdate();
stm2.executeUpdate();
stm3.executeUpdate();
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
Practical No.7
Topic: Implement the following EJB applications with different types of Beans
A] Aim: Develop a simple EJB application to demonstrate servlet hit count using singleton
session beans
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="s1">
<h2>Check the number of hits on the page</h2>
<input type="submit" value="Check count"><br>
</form>
</body>
</html>
s1.java
package pkg1;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
import javax.servlet.http.HttpServletResponse;
import javax.ejb.EJB;
import pk.bean1;
Bean.java
Bean1.java
package pk;
import javax.ejb.Singleton;
@Singleton
public class bean1 {
private int hit=0;
public bean1(){}
public int verifyCount()
{ return hit++;
}
}
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
Output:
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
B] Aim: Develop simple marks entry application into demonstrate accessing database using
EJB.
index.html
<!DOCTYPE html>
<html>
<head>
<title>Student Details</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="s1" method="POST">
<h1><b>Student Details<b></h1>
Enter Student Name: <input type="text" name="t1"><br><br>
Enter marks of subject 1: <input type="number" name="t2" ><br><br>
Enter marks of subject 2: <input type="number" name="t3"><br><br>
Enter marks of subject 3: <input type="number" name="t4"><br><br>
Enter marks of subject 4: <input type="number" name="t5"><br><br>
Enter marks of subject 5: <input type="number" name="t6"><br><br>
<input type="submit" value="insert" >
<input type="reset" value="clear" >
</form>
</body>
</html>
s1.java
package pkv;
import java.io.IOException;
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@EJB
b1 obj;
int totalmarks=s1+s2+s3+s4+s5;
b1.java
package pkv;
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
import javax.ejb.Stateless;
import java.sql.*;
@Stateless
public class b1 {
public b1() {
}
public String marks(String n, Integer s1, Integer s2, Integer s3, Integer s4, Integer s5,
Integer totalmarks) {
String Confirmationmsg = "";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/studentdetails", "root",
"Ashish@000");
PreparedStatement pst = con.prepareStatement("insert into marksheet
values(?,?,?,?,?,?,?)");
pst.setString(1, n);
pst.setInt(2, s1);
pst.setInt(3, s2);
pst.setInt(4, s3);
pst.setInt(5, s4);
pst.setInt(6, s5);
pst.setInt(7, totalmarks);
int rs = pst.executeUpdate();
if (rs == 1) {
Confirmationmsg = "Marks Successfully updates";
} else {
Confirmationmsg = "Try Again";
}
con.close();
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
pst.close();
}
catch(Exception e)
{
Confirmationmsg=""+e;
}
return Confirmationmsg;
}
}
Output:
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
Practical No. 8
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
application.setAttribute("user","Guest");
session.setAttribute("country","India");
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1><b>Login Credentials</b></h1>
<form action="s2" method="POST">
Enter Username:<input type="text" placeholder="Username" name="text1"
class="t"><br><br>
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
</body>
</html>
s2.java
package xhz;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
RequestDispatcher rd = request.getRequestDispatcher("Demo.jsp");
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
rd.forward(request, response);
}
else {
RequestDispatcher rd = request.getRequestDispatcher("Fail.jsp");
rd.include(request, response);
}
}
Demo.jsp
Fail.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Fail</title>
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
</head>
<body>
<h1>Login failed!</h1><br/>
<b>${param.name}</b><br/>
<p>Author name:<b>${applicationScope.user}</b></p><br/>
<p>country name:<b>${sessionScope.country}</b></p><br/>
<a href="ViewLog.jsp">Click to view details</a>
</body>
</html>
Home.java
package xhz;
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 javax.ejb.EJB;
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
}
Beans.java
package xhz;
import javax.ejb.Stateless;
@Stateless
public class Beans {
}
ViewLog.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ViewLog</title>
</head>
<body>
<h1>Log details:</h1>
<h1>Request object</h1>
Context Path:<%=request.getContextPath()%><br>
Remote Host:<%=request.getRemoteHost()%><br>
<h1>Response object</h1>
Character encoding type:<%=response.getCharacterEncoding()%><br>
Content type:<%=response.getContentType()%><br>
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
Locale:<%=response.getLocale()%><br>
<h1>Session object</h1>
ID:<%=session.getId()%><br>
Creation time:<%=new java.util.Date(session.getCreationTime())%><br>
</body>
</html>
Output:
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
ENTERPRISE JAVA
T.Y.BSc.IT(Sem-V) Roll No:893
ENTERPRISE JAVA