Professional Documents
Culture Documents
728 - Om Gupta - EJ
728 - Om Gupta - EJ
728 - Om Gupta - EJ
1] Click on file and then click New Project .Then Select Java Web and click on Next.
2] Give the project name and set the path where you want to save your project. Then click on
Next.
3] Then click on Finish.
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>
<form method="get" action="calServlet">
enter no1: <input type="text" name="txt1"><br>
enter no2: <input type="text" name="txt2"><br>
<input type="radio" name="opr" value="+">ADDITION<br>
<input type="radio" name="opr" value="-">SUBTRACTION<br>
<input type="radio" name="opr" value="*">MULTIPLICATION<br>
<input type="radio" name="opr" value="/">DIVISION<br>
<input type="submit" value="Calculate">
<input type="reset">
</form>
</body>
</html>
1] Click on Source Package , then right click on default Package , then click on New and
click on Servlet_.
2] Give Class Name and Package name and click on Next.
3] Click on Finish.
Calservl //delete all the commnets:
package mypack;
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;
if(op.equals("+"))
result=n1+n2;
if(op.equals("-"))
result=n1-n2;
if(op.equals("*"))
result=n1*n2;
if(op.equals("/"))
result=n1/n2;
out.println("The result of operation is"+result);
}
}
@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";
}// </editor-fold>
Output:
1B]Aim:create servlet for login page if password and email correct then give it welcome
message.
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>Login form</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="loginServlet">
ENTER USER NAME:<input type="text" name="textUN"><br>
ENTER PASSWORD:<input type="password" name="textpaw"><br>
<input type="submit" value="Login"><br>
<input type="Reset">
</form>
</body>
</html>
loginServlet.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.RequestDispatcher;
}
else
{
out.println("<h2>login failed!Try again</h2>");
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request,response);
}
}
}
welcomeServlet.java
package wpack;
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;
Output:
1c]Aim:Create a registration servlet in java using jdbc . Accept details such as user name
,password ,email and country from the user using HTML form and store the registration
details in the database
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>Registration Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="RgServletb1">
<h1>Welcome to Registration page</h1>
Enter User Name <input type="text" name="txtUid"><br>
Enter Password <input type="password" name="txtPass"><br>
Enter Email <input type="text" name="txtEmail" ><br>
Enter Country <input type="text" name="txtCon" ><br>
<input type="reset"><input type="submit" value="REGISTER">
</form>
</body>
</html>
RgServletb1.java
package rpack;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponse;
public class RgServletb1 extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String eid=request.getParameter("txtUid");
String ps=request.getParameter("textPass");
String em=request.getParameter("txtEmail");
String co=request.getParameter("txtCon");
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/empregistrationb1","root","patkar");
PreparedStatement pst = con.prepareStatement("insert into userb1 values(?,?,?,?)");
pst.setString(1,eid);
pst.setString(2,ps);
pst.setString(3,em);
pst.setString(4,co);
int row = pst.executeUpdate();
if (row==1)
{
out.println("DataInserted successfully");
}
else
{
out.println("Data is not Inserted successfully");
}
pst.close();
con.close();
}
catch(Exception e)
{
out.println(e);
}
}
}
8) Expand empregistrationb1 then table then userb1. Right click on userb1 and select
view data.
A) Aim: using Request Dispatcher interface create servlet which validate the password
enter by user if the user has enter “servlet” as password then he will be forward to
welcome servlet else will use stay on the index html and error will be displayed.
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>
<form method="POST" action="NewServlet">
Enter User ID: <input type="text" name="txtID" value="" size="20"><br>
Enter Password: <input type="text" name="txtpw" value="" size="20"><br>
<input type="submit" value="login">
<input type="reset" value="clear" >
</form>
</body>
</html>
NewServlet.java
package mypack;
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;
<!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="page1Servlet">
ENTER YOUR NAME:<input type="text" name="txtUN">
<input type="submit" value="submit">
</form>
</body>
</html>
page1Servlet.java
package p1pack;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.*; //imported
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet page1Servlet</title>");
out.println("</head>");
out.println("<body bgcolor=cyan>");
String uname=request.getParameter("txtUN");
out.println("<h1>WELCOME "+uname+"<h1>");
Cookie ck1=new Cookie("username",uname);
Cookie ck2=new Cookie("visit","1");
response.addCookie(ck1);
response.addCookie(ck2);
out.println("<h3><a href=page2Servlet>CLICK TO VISIT PAGE 2</h3>");
out.println("</body>");
out.println("</html>");
}
Page2Servlet.java
package p2pack;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet page2Servlet</title>");
out.println("</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)
{
out.println("<body bgcolor=yellow>");
int count=Integer.parseInt(ck[i].getValue())+1;
out.println("<h1>visit no:"+count+"</h1>");
ck[i]=new Cookie("visit",count+"");
response.addCookie(ck[i]);
}
else
{
out.println("<body bgcolor=red>");
out.println("you have visited this page before");
}
}
}
out.println("<h3><a href=page3Servlet>CLICK TO VISIT PAGE 3</a></h3>");
out.println("</body>");
out.println("</html>");
}
}
Page3Servlet.java
package p3pack;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet page3Servlet</title>");
out.println("</head>");
out.println("<body bgcolor=orange>");
Cookie[] ck=request.getCookies();
for(int i=0;i<ck.length;i++)
{
if(ck[i].getName().equals("visit"))
{
int count=Integer.parseInt(ck[i].getValue())+1;
out.println("<h1>visit no:"+count+"</h1>");
ck[i]=new Cookie("visit",count+"");
response.addCookie(ck[i]);
}
else
{
out.println(ck[i].getName()+"="+ck[i].getValue());
}
out.println("</body>");
out.println("</html>");
}
}
2c)Create a servlet demosnstatring the use of session creation and destruction.Whether
the user has visited this page first time or has visited earlier also using the session.
Source of 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>
<form action="p">
ENTER USER ID:<input type="text" name="txtName"><br>
<input type="reset">
<input type="submit">
</form>
</body>
package ppack;
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;
public class p extends HttpServlet {
out.println("<html><head><title>p</title></head>");
HttpSession hs= request.getSession(true);
if(hs.isNew())
{
out.println("<body bgcolor=yellow");
String name = request.getParameter("txtName");
hs.setAttribute("uname", name);
hs.setAttribute("visit","1");
out.println("<h1>Welcome First Time</h1>");
}
else
{
out.println("<h1>Welcome agian</h1>");
int visit = Integer.parseInt((String)hs.getAttribute("visit"))+1;
out.println("<h1>You Visited"+visit+"Times</h1>");
hs.setAttribute("visit",""+visit);
}
out.println("<h1>Your SessionID"+hs.getId()+"</h1>");
out.println("<h1>You logged at"+new java.util.Date(hs.getCreationTime())+"(/h1)");
out.println("<h1><a href=p2>Click on the page 2</a></h1>");
out.println("</body>");
out.println("</html>");
}
}
Source code of p2.java :
package p2pack;
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;
out.println("<html><head><title>p2</title></head>");
HttpSession hs= request.getSession(false);
out.print("<h1>Welcome Again on Page no:2</h1>");
int visit = Integer.parseInt((String)hs.getAttribute("visit"))+1;
out.println("<h1>You Visited"+visit+"Times</h1>");
hs.setAttribute("visit",""+visit);
out.println("<h1>Your SessionID"+hs.getId()+"</h1>");
out.println("<h1>You logged at"+new java.util.Date(hs.getCreationTime())+"(/h1)");
out.println("<h1><a href=p>Click on the page 1</a></h1>");
out.println("<h1><a href=p3>Click on the page 3</a></h1>");
out.println("<h1><a href=p4>Click on the page 4</a></h1>");
out.println("<h1><a href=Logout>Click for termination Session</a></h1>");
out.println("</body>");
out.println("</html>");
}
}
Source code of p3.java:
package p3pack;
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;
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet p4</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet p4 at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Source code of Logout.java:
package logpack;
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;
out.println("<html><head><title>SERVLET LOGOUT</title></head>");
out.println("<body");
javax.servlet.http.HttpSession hs = request.getSession();
if(hs!= null)hs.invalidate();
out.println("<h1>YOU ARE LOGGED NOW</h1>");
out.println("</body>");
out.println("</html>");
}
}
PRACTICAL NO: 3
Topic:Implement the Servlet IO and File applications.
Uploading file
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>
<form action="uploadServlet" method="post" enctype="multipart/form-data">
<h1> Uploading the 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>
uploadServlet
package upack;
import java.io.IOException;
import java.io.*;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.MultipartConfig;
@MultipartConfig
public class uploadServlet extends HttpServlet {
Create project then copy file which you want to download then paste this file in Project
under Webpages
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>
<h1>File download Application</h1>
Click<a href="downloadServlet?filename=TYBscIT.pdf.pdf">Sample file</a>
</body>
</html>
downloadServlet
package dpack;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
int i;
while((i=is.read())!=-1)
{
out.write(i);
}
is.close();
out.close();
}
Output:
PRACTICAL NO 4
implement the folowing jsp application.
Aim:4A]devolopve simple to display values often from the use of Intrinsic object of
various type.
Index.html
Source code:
<!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>
<h1>Registration Page</h1>
<form action="simple.jsp">
Username:
<input type="text" name="txtusr"><br>
<input type="submit" value="submit">
</form>
</body>
</html>
Simple.jsp
Source code:
<%--
Document : simple
Created on : Jul 27, 2021, 11:41:53 AM
Author : sneha
--%>
</body>
</html>
Output:
4B]Aim:Create a registration and login jsp application to register and authenticate the
user based on user name and password using JSP.
Index.html
Source code:
<!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>
<h1>Registration Page</h1>
<form action="Registration.jsp">
Enter User Name:
<input type="text" name="txtName"><br>
Enter Password:
<input type="password" name="pass"><br>
Re-enter Password:
<input type="password" name="repass"><br>
Email id:
<input type="email" name="eid"><br>
Enter country name:
<input type="text" name="con"><br>
<input type="reset" value="Reset"><br>
<input type="submit" value="submit">
</form>
</body>
</html>
Registration.jsp
<%--
Document : Registration
Created on : Jul 27, 2021, 12:50:54 PM
Author : sneha
--%>
%>
</body>
</html>
2) Click on databases.
8) Right click on MySQL server at Localhost choose connect.
10) Click on MySQL connector then build and run the program.
Output:
PRACTICAL NO 5
5A)Create an HTML page with fields employee number ,name ,age , designation ,salary.
Now on submit this data to a JSP page which will update the employee table of database with
matching employee number.
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">
<style>
form{text-align:center;margin:230px 70px;}
</style>
</head>
<body style="background-color:red;">
<form action="Updateemp.jsp">
Enter Employee Number:<input type="text" name="txtEno"><br>
<br>
Enter Name:<input type="text" name="txtName"><br>
<br>
Enter age:<input type="text" name="txtAge"><br>
<br>
Enter Designation:<input type="text" name="txtdesg"><br>
<br>
Enter Salary:<input type="text" name="txtSal"><br>
<br>
<br><input type="submit" style="background-color:lightcoral">
<input type="reset" style="background-color:lightcoral">
</form>
</body>
</html>
Updateemp.jsp
<%--
Document : Updateemp
Created on : Aug 3, 2021, 12:40:14 PM
Author : mange
--%>
%>
</body>
</html>
Before updating
After updating
5B]Create a JSP page to demonstrate the use of expression language.
<%--
Document : newjsp
Author : mange
--%>
<%
application.setAttribute("author","TYIT");
session.setAttribute("country", "India");
%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
<style>
h1{color: red}
</style>
</head>
<body>
<form action="show.jsp">
<h1>Provide your details</h1>
</form>
</body>
</html>
Show.jsp
<%--
Document : show
Author : mange
--%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
<style>
h1{color: red}
</style>
</head>
<body>
<p>Author NAME:<b>${applicationScope.author}</b></p>
<p>Counrty Name:<b>${sessionScope.country}</b></p>
</p>
6+7=${6+7}<br>
8*9=${8*9}<br>
</p>
</body>
</html>
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="JSTLDemo.jsp ">
<h1>Provide your details</h1>
User's First Name:<input type="text" name="ufname" /><br>
<br>User's Last Name:<input type="text" name="ulname" /><br>
<br>Enter Input:<input type='number' name="input"/><br>
<br> <input type="submit" >
</form>
</body>
</html>
JSTLDemo.jsp
<%--
Document : JSTLDemo
Created on : Aug 10, 2021, 1:22:59 PM
Author : mange
--%>
</body>
</html>
Output:
PRACTICAL NO 6
Q6]Implement the following EJB Application .
6A]Aim:Create a currency converter using EJB.
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>
<form action="convertServ">
<div>
Enter Amount:<input type="text" name="amt"><br>
Select Conversion:
<input type="radio" name="type" value="r2d" checked>Rupees to Dollar
<input type="radio" name="type" value="d2r">Dollar to Rupees
<br>
<input type="reset" name="Reset">
<input type="submit" name="CONVERT">
</div>
</form>
</body>
</html>
convertBean.java
package myservfile;
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 mybeanfile.convertBean;
if(request.getParameter("type").equals("d2r"))
{
out.println("<h1>"+amt+"Dollar="+obj.d2Rupees(amt)+"Repees</h1>");
}
}
}
convertBean.java
package mybeanfile;
import javax.ejb.Stateless;
@Stateless
public class convertBean {
public convertBean(){}
public double r2Dollar(double r)
{
return r/73.65;
}
public double d2Rupees(double d)
{
return d*73.65;
}
}
Output:
Implement the following EJB application
</form>
</body>
</html>
RoomBookServ.java
package servfile;
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 beanfile.RoomBookBean;
@EJB
RoomBookBean obj;
}
}
RoomBookBean.java
package beanfile;
import javax.ejb.Stateless;
import javax.ejb.*;
import java.sql.*;
@Stateless
public class RoomBookBean {
public RoomBookBean(){}
Output:
/*
mysql> use roomreservedb3;
Database changed
mysql> create table rbooking(
-> roomId varchar(5) primary key,
-> Rtype varchar(20),
-> rcharge int(10),
-> CustName varchar(15),
-> CustMobno varchar(15),
-> Rstatus varchar(15)
-> );
Query OK, 0 rows affected (0.01 sec)
7A]Aim:Develop a simple EJB application to demonstrate the Servlet Hit count using singleton
Session Beans.
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>
<form action="HitCountServ" method="post">
Check the No. of Hits on the page:
<input type="submit" value="Check Count">
</form>
</body>
</html>
HitCountServ.java
package myserv;
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 mybean.HitCountBean;
@EJB
HitCountBean obj;
HitCountBean.java
package mybean;
import javax.ejb.Singleton;
@Singleton
public class HitCountBean {
return hit++;
}
}
Output:
7B]Aim:develop simple mask entry to demonstrate the accessing database using EJB.
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>
<form action="StudentmarksServlet">
<h1>Student Details</h1>
Enter Student Name:<input type="text" name="sname"><br>
Enter Student1 marks:<input type="number" name="sub1"><br>
Enter Student2 marks:<input type="number" name="sub2"><br>
Enter Student3 marks:<input type="number" name="sub3"><br>
<input type="submit" value="Insert">
<input type="reset" value="Clear">
</form>
</body>
</html>
StudentmarksServlet.java
package StudMarksPackage;
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 MyBeanPackage.StudentMarksBean;
@EJB
StudentMarksBean obj;
String Studname=request.getParameter("sname");
int mark1=Integer.parseInt(request.getParameter("sub1"));
int mark2=Integer.parseInt(request.getParameter("sub2"));
int mark3=Integer.parseInt(request.getParameter("sub3"));
int totalmarks=mark1+mark2+mark3;
msg=obj.marksEntry(Studname,mark1,mark2,mark3,totalmarks);
out.println(msg);
}
StudentMarksBean.java
package MyBeanPackage;
import javax.ejb.Stateless;
import java.sql.*;
@Stateless
public class StudentMarksBean {
public StudentMarksBean(){}
}
else
{
confirmationmsg="<h2>could not updated data successfully.Please try again</h2>";
}
}
catch(Exception e)
{
confirmationmsg=""+e;
}
return confirmationmsg;
}
Output:
PRACTICAL NO 8
<!DOCTYPE html>
<html>
<head>
<style>
body{
background-color:pink;
text-align:center;
margin:230px 70px;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>LOGIN PAGE</h1>
<form action="Pract8a" method="post">
Enter User ID: <input type="text" name="uid"><br>
Enter Password: <input type="password" name="pass"><br>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
</body>
</html>
Pract8a.java
package mypack;
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.RequestDispatcher;
String uname=request.getParameter("uid");
String pass=request.getParameter("pass");
if(pass.equals("om"))
{
RequestDispatcher rd=request.getRequestDispatcher("welcome.jsp");
rd.forward(request, response);
}
else
{
RequestDispatcher rd=request.getRequestDispatcher("failed.jsp");
rd.forward(request, response);
}
}
}
welcome.jsp
<%--
Document : welcome
Created on : Oct 2, 2021, 4:18:48 PM
Author : mange
--%>
<h1>Response Object</h1>
Character Encoding Type: <%=response.getCharacterEncoding()%><br>
Content Type: <%=response.getContentType()%><br>
Locale: <%=response.getLocale()%><br>
<h1>Session Object</h1>
ID: <%= session.getId() %><br>
Creation time: <%= new java.util.Date(session.getCreationTime()) %><br>
Last Access Time: <%= new java.util.Date(session.getLastAccessedTime())%><br>
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 displaypack.display;
import javax.ejb.Stateless;
@Stateless
public class display {
public display(){}