Web Tech Assignment 4
Web Tech Assignment 4
Session Bean
Session bean encapsulates business logic only, it can be invoked by local, remote and webservice client.
It can be used for calculations, database access etc.The life cycle of session bean is maintained by the application server
(EJB Container).
Stateless Session bean is a business object that represents business logic only. It doesn't have state (data).
In other words, conversational state between multiple method calls is not maintained by the container in case of stateless
session bean.
The stateless bean objects are pooled by the EJB container to service the request on demand.
It can be accessed by one client at a time. In case of concurrent access, EJB container routes each request to different
instance.
Example:- To create EJB application, you need to create bean component and bean client.
1. Create stateless bean component:-
To create the stateless bean component, you need to create a remote interface and a bean class.
File: AdderImplRemote.java
package com.javatpoint;
import javax.ejb.Remote;
@Remote
public interface AdderImplRemote {
int add(int a,int b);
}
File: AdderImpl.java
package com.javatpoint;
import javax.ejb.Stateless;
@Stateless(mappedName="st1")
public class AdderImpl implements AdderImplRemote {
public int add(int a,int b){
return a+b;
}
}
File: AdderImpl.java
package com.javatpoint;
import javax.naming.Context;
import javax.naming.InitialContext;
Stateful Session bean is a business object that represents business logic like stateless session bean. But, it maintains state
(data).
In other words, conversational state between multiple method calls is maintained by the container in stateful session bean.
Example :-As described in the previous example, you need to create bean component and bean client for creating session
bean application.
File: BankRemote.java
package com.javatpoint;
import javax.ejb.Remote;
@Remote
public interface BankRemote {
boolean withdraw(int amount);
void deposit(int amount);
int getBalance();
}
File: Bank.java
package com.javatpoint;
import javax.ejb.Stateful;
@Stateful(mappedName = "stateful123")
public class Bank implements BankRemote {
private int amount=0;
public boolean withdraw(int amount){
if(amount<=this.amount){
this.amount-=amount;
return true;
}else{
return false;
}
}
public void deposit(int amount){
this.amount+=amount;
}
public int getBalance(){
return amount;
}
}
2) Create stateful bean client
The stateful bean client may be local, remote or webservice client. Here, we are going to create web based client and not
using dependency injection.
File: index.jsp
<a href="OpenAccount">Open Account</a>
File: operation.jsp
<form action="operationprocess.jsp">
Enter Amount:<input type="text" name="amount"/><br>
Choose Operation:
Deposit<input type="radio" name="operation" value="deposit"/>
Withdraw<input type="radio" name="operation" value="withdraw"/>
Check balance<input type="radio" name="operation" value="checkbalance"/>
<br>
<input type="submit" value="submit">
</form>
File: operationprocess.jsp
<%@ page import="com.javatpoint.*" %>
<%
BankRemote remote=(BankRemote)session.getAttribute("remote");
String operation=request.getParameter("operation");
String amount=request.getParameter("amount");
if(operation!=null){
if(operation.equals("deposit")){
remote.deposit(Integer.parseInt(amount));
out.print("Amount successfully deposited!");
}else
if(operation.equals("withdraw")){
boolean status=remote.withdraw(Integer.parseInt(amount));
if(status){
out.print("Amount successfully withdrawn!");
}else{
out.println("Enter less amount");
}
}else{
out.println("Current Amount: "+remote.getBalance());
}
}
%>
<hr/>
<jsp:include page="operation.jsp"></jsp:include>
File: OpenAccount.java
package com.javatpoint;
import java.io.IOException;
import javax.ejb.EJB;
import javax.naming.InitialContext;
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("/OpenAccount")
public class OpenAccount extends HttpServlet {
//@EJB(mappedName="stateful123")
//BankRemote b;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
InitialContext context=new InitialContext();
BankRemote b=(BankRemote)context.lookup("stateful123");
request.getSession().setAttribute("remote",b);
request.getRequestDispatcher("/operation.jsp").forward(request, response);
}catch(Exception e){System.out.println(e);}
}
}
2. What do you mean by Database Drivers? Explain each type. Also explain the steps to get any value into
database.
Database Driver
JDBC Driver is a software component that enables java application to interact with the database. There are 4 types of
JDBC drivers:
1. JDBC-ODBC bridge driver
2. Native-API driver (partially java driver)
3. Network Protocol driver (fully java driver)
4. Thin driver (fully java driver)
2) Native-API driver
The Native API driver uses the client-side libraries of the database.
The driver converts JDBC method calls into native calls of the database API. It is not written entirely in java.
Advantage:
--performance upgraded than JDBC-ODBC bridge driver.
Disadvantage:
--The Native driver needs to be installed on the each client machine.
--The Vendor client library needs to be installed on client machine.
Advantage:
--No client side library is required because of application server that can perform many tasks like auditing, load balancing,
logging etc.
Disadvantages:
--Network support is required on client machine.
--Requires database-specific coding to be done in the middle tier.
--Maintenance of Network Protocol driver becomes costly because it requires database-specific coding to be done in the
middle tier.
4) Thin driver
The thin driver converts JDBC calls directly into the vendor-specific database protocol.
That is why it is known as thin driver. It is fully written in Java language.
Advantage:
--Better performance than all other drivers.
--No software is required at client side or server side.
Disadvantage:
--Drivers depend on the Database.
Steps to Get Any Value in Database
• 1.Establish a connection
• 2.Create JDBC Statements
• 3.Execute SQL Statements
• 4.GET ResultSet
• 5.Close connections
3. Differentiate stateful and stateless session bean. Create a stateless session bean for a calculator.