List of Experiments :
1. Develop at least 5 components such as Order Processing, Payment Processing,
etc., using .NET component technology.
2. Develop at least 5 components such as Order Processing, Payment Processing,
etc., using EJB component technology.
3. Invoke .NET components as web services.
4. Invoke EJB components as web services.
5.
Develop a Service Orchestration Engine (workflow) using WS-BPEL and
implement service composition. For example, a business process for
planning business travels will invoke several services. This process
will invoke several airline companies (such as American Airlines, Delta
Airlines etc. ) to check the airfare price and buy at the lowest
price. 64
6. Develop a J2EE client to access a .NET web service.
7. Develop a .NET client to access a J2EE web service.
Ex:no:1 Ejb components for User login
Date:31.7.2012
Aim:
To create a project userlogin using ejb
components.
Algorithm:
Step 1
:Create a new project userlogin.
Step 2
:Create a login jsp with username and password. Also with a link to
registration for new users
Step 3
:Create a servlet and ejb file and link the ejb component with the
servlet.
Step4
:If the user enters the username nd password,validate them in serverside
by using a ejb component verify in the database.
Step 4.1:if the user is an authenticated
user then display login success in success.jsp
Step 4.2:Else display login failure in
the same login.jsp
Step 5
:If the user is new user get the registration details in the
registration.jsp and store them in the database by using the ejb component
update.
Step 6
:Run the project and display
the output.
Program:
Index.jsp:
<%--
Document : index
Created on : Jul 31, 2012, 9:24:44 AM
Author : ITLABC55
--%>
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>JSP Page</title>
<script type="text/javascript">
function validate()
{
var name,password,rpw;
name=document.signup.name.value;
password=document.signup.password.value;
rpw=document.signup.rpw.value;
if(name=="") alert("Enter
user name");
if(password=="")
alert("Enter Password");
if(!(password==rpw))
alert("Password doesn't match");
}
</script>
</head>
<body>
<center>
<h2><font
color="red">SIGNUP HERE</font></h2>
<pre> <form name="signup"
action="loginservlet" method="get">
Name :<input type="text"
name="name" />
UserName :<input type="text"
name="uname" />
Password :<input type="password"
name="password"/>
Retype Password :<input
type="password" name="rpw"/>
<input type="submit"
value="signup" name="submit"
onfocus="validate()"/>
</form></pre>
</center>
</body>
</html>
Enter.jsp:
<%--
Document : enter
Created on : Jul 31, 2012, 9:26:36 PM
Author : ITLABC55
--%>
<%@page
contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<center>
<h2><font color="red"
family="comic sans">LOGIN FORM</font></h2>
<pre><form
action="loginservlet" method="post">
USERNAME :<input type="text"
name="uname"/>
PASSWORD :<input type="password"
name="password" size="20"/>
<input type="submit"
name="submit" value="submit"/>
</form>
<a
href="index.jsp">Click here to SIGNUP</a>
</pre>
<%
String nam = (String) request.getAttribute("lastlogin");
if (nam == "false") {%>
<h1>Login fail</h1>
<%}
%>
</center>
</body>
</html>
Success.jsp:
<%--
Document : success
Created on : Jul 31, 2012, 9:40:21 AM
Author : ITLABC55
--%>
<%@page
contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String name = (String) request.getAttribute("username");
String time = (String)
request.getAttribute("lastlogin");
if (time == "newuser") {%>
<center> <h2>Welcome <%=name%></h2>
<h2>Your are a new user</h2>
<%} else {%>
<h2> Welcome
<%=name%></h2>
<h2>LastLogin
was<%=time%></h2>
<%}%>
</center>
</body>
</html>
Fail.jsp:
<%--
Document : fail
Created on : Jul 31, 2012, 9:40:35 PM
Author : ITLABC55
--%>
<%@page
contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Login fail</h1>
</body>
</html>
Loginejb.java:
/*
* To change this template, choose Tools
| Templates
* and open the template in the editor.
*/
package ejb;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
/**
*
* @author ITLABC55
*/
@Stateless
@LocalBean
public class loginejb {
public void insert(String name, String
username, String password) throws SQLException {
Connection con = null;
try {
con = getconnect();
if (con == null) {
System.out.print("Database
Connection is null");
} else {
String sql = "insert into
Login(name,username,password) values(?,?,?)";
PreparedStatement st = con.prepareStatement(sql);
st.setString(1, name);
st.setString(2, username);
st.setString(3, password);
st.executeUpdate();
con.close();
}
} catch (Exception e) {
System.out.println(e);
} finally {
con.close();
}
}
public ArrayList login(String Username,
String Password) throws SQLException {
ArrayList l = new ArrayList();
Connection con = null;
try {
con = getconnect();
Statement s = con.createStatement();
ResultSet rk =
s.executeQuery("select * from Login");
while (rk.next()) {
String name =
rk.getString("name");
String uname =
rk.getString("username");
String upass =
rk.getString("password");
if ((Username.equals(uname)) &&
(Password.equals(upass))) {
l.add(0, name);
l.add(0, name);
String dat =
rk.getString("lastlogin");
if (dat == null) {
l.add(1, "newuser");
} else {
l.add(1, dat);
}
Date d = new Date();
SimpleDateFormat sd = new
SimpleDateFormat("dd/MM/yy hh:mm:ss");
String frmt = sd.format(d);
String sql = "update Login set
[lastlogin]='" + frmt + "' where [username]='" + uname +
"'";
PreparedStatement st = con.prepareStatement(sql);
st.executeUpdate();
break;
} else {
l.add(0, "false");
}
}
} catch (Exception e) {
System.out.print(e);
} finally {
con.close();
}
return l;
}
public Connection getconnect() {
Connection con = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String path = "D:/SOA
LAB/login/login/Login.mdb";
String db =
"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + path +
";DriverID=22";
con = DriverManager.getConnection(db,
"", "");
} catch (Exception e) {
System.out.print(e);
} finally {
return con;
}
}
}
// Add business logic below.
(Right-click in editor and choose
// "Insert Code > Add Business
Method")
Loginservlet.java:
/*
* To change this template, choose Tools
| Templates
* and open the template in the editor.
*/
package log;
import ejb.loginejb;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
/**
*
* @author ITLABC55
*/
public class loginservlet extends
HttpServlet {
@EJB
private loginejb loginejb;
/**
* Processes requests for both HTTP
<code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a
servlet-specific error occurs
* @throws IOException if an I/O error
occurs
*/
protected void
processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String uname =
request.getParameter("uname");
String password = request.getParameter("password");
String name =
request.getParameter("name");
String submit =
request.getParameter("submit");
String Name = null;
String Dat = null;
ArrayList l = new ArrayList();
if (submit.equals("signup")) {
loginejb.insert(name, uname, password);
RequestDispatcher rd1 =
request.getRequestDispatcher("enter.jsp");
rd1.forward(request, response);
}
if (submit.equals("submit")) {
l = loginejb.login(uname, password);
Name = (String) l.get(0);
Dat = (String) l.get(1);
System.out.println(Name + Dat);
if (Name.equals("false")) {
Dat = "false";
request.setAttribute("lastlogin",
Dat);
RequestDispatcher rd =
request.getRequestDispatcher("enter.jsp");
rd.forward(request, response);
} else {
request.setAttribute("username",
Name);
request.setAttribute("lastlogin",
Dat);
RequestDispatcher rd =
request.getRequestDispatcher("success.jsp");
rd.forward(request, response);
}
}
} catch (Exception e) {
System.out.println(e);
} finally {
out.close();
}
}
// <editor-fold
defaultstate="collapsed" desc="HttpServlet methods. Click on the
+ sign on the left to edit the code.">
/**
* Handles the HTTP
<code>GET</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 doGet(HttpServletRequest
request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* 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>
}
Result:
Thus,the project userlogin is created by
using ejb components and the output is verified
successfully.
Ex:no:2 Ejb components for Shopping cart
Date:28.8.2012
Aim:
To create a shopping cart using ejb
components.
Algorithm:
Step 1:Create a new project shopcart
Step 2:Create a index page to display
the items available and their respective
links.
Step 3:Create three pages for
ipad,phones and games and their item details should be fetched from database.
Step 4:Create a servlet and stateful
session ejb and link the ejb components with the servlet.
Step 5:Add the items that are selected
by the user to the stateful cart.
Step 6:Display the selected items when
display option is selected.
Step 7:Provide a facility to remove
items from the cart if required.
Step 8:Run the project and display the output.
Program:
Index.jsp:
<%--
Document :
index
Created on : Aug 28, 2012, 9:22:16 AM
Author : ITLABC55
--%>
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Shopping Cart</h1>
<form action="shop">
<input type="submit"
name="buttons" value="book"/>
<input type="submit"
name="buttons" value="electronic"/>
<input type="submit" name="buttons"
value="games"/>
<input type="submit"
name="buttons" value="display"/>
</form>
</body>
</html>
Book.jsp:
<%--
Document :
book
Created on : Aug 28, 2012, 9:35:49 PM
Author : ITLABC55
--%>
<%@page
import="java.util.ArrayList"%>
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<% ArrayList l=null;
l=(ArrayList)request.getAttribute("l");
%>
<% if(!l.isEmpty()) { %>
<h1>Available Books</h1>
<form action="shop">
<%for(int i=0;i<l.size();i++)
{%>
<input type="checkbox"
name="groups"
value="<%=l.get(i)%>"/><%=l.get(i)%> <%}%>
<input type="submit"
name="buttons" value="addtocart"/>
<input type="submit"
name="buttons" value="display"/>
</form>
<% }
else {%>
<h1>No items Available!</h1> <% }
%>
</body>
</html>
Electronic.jsp:
<%--
Document :
electronic
Created on : Aug 28, 2012, 9:36:15 AM
Author : ITLABC55
--%>
<%@page
import="java.util.ArrayList"%>
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<% ArrayList l=null;
l=(ArrayList)request.getAttribute("l");
%>
<% if(!l.isEmpty()) { %>
<h1>Available Electronic Items</h1>
<form action="shop">
<%for(int i=0;i<l.size();i++)
{%>
<input type="checkbox"
name="groups"
value="<%=l.get(i)%>"/><%=l.get(i)%>
<%}%>
<input type="submit"
name="buttons" value="addtocart"/>
<input type="submit"
name="buttons" value="display"/>
</form>
<% }
else {%>
<h1>No items Available!</h1> <% }
%> </body>
</html>
Games.jsp:
<%--
Document :
games
Created on : Aug 28, 2012, 9:36:30 AM
Author : ITLABC55
--%>
<%@page
import="java.util.ArrayList"%>
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>GAMES</h1>
<% ArrayList l=null;
l=(ArrayList)request.getAttribute("l");
%>
<% if(!l.isEmpty()) { %>
<h1>Games</h1>
<form action="shop">
<%for(int i=0;i<l.size();i++)
{%>
<input type="checkbox"
name="groups"
value="<%=l.get(i)%>"/><%=l.get(i)%>
<%}%>
<input type="submit"
name="buttons" value="addtocart"/>
<input type="submit"
name="buttons" value="display"/>
</form>
<% }
else {%>
<h1>No items Available!</h1> <% }
%>
</body>
</html>
Display.jsp:
<%--
Document :
display
Created on : Jul 11, 2012, 8:17:37 AM
Author : ITLABC55
--%>
<%@page
import="javax.validation.constraints.Size"%>
<%@page
import="java.util.ArrayList"%>
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<% ArrayList l=null;
l=(ArrayList)request.getAttribute("list");
%>
<% if(!l.isEmpty()) { %>
<h1>Selected items</h1>
<form action="shop">
<%for(int i=0;i<l.size();i++)
{%>
<input type="checkbox"
name="groups"
value="<%=l.get(i)%>"/><%=l.get(i)%>
<%}%>
<input type="submit"
name="buttons" value="remove"/>
</form>
<% }
else {%>
<h1>No items selected!</h1> <% }
%>
</body>
</html>
Shopejb.java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ejb;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateful;
import javax.ejb.LocalBean;
/**
*
* @author ITLABC55
*/
@Stateful
@LocalBean
public class shopejb {
ArrayList list=new ArrayList();
int i=0;
public void add(ArrayList item) {
for(i=0;i<item.size();i++)
if(!list.contains(item.get(i)))
list.add(i,item.get(i));
}
public ArrayList display() {
return list;
}
public ArrayList remove(ArrayList item) {
for(i=0;i<item.size();i++)
list.remove((String)item.get(i));
return list;
}
public ArrayList getitems(String item) {
ArrayList l=new ArrayList();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String path="D:/SOA LAB/2nd/2nd/2.mdb";
String db="jdbc:odbc:Driver={Microsoft Access
Driver (*.mdb)};DBQ="+path+";DriverID=22";
Connection con=DriverManager.getConnection(db);
String qu="select * from "+item;
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(qu);
while(rs.next())
{
l.add(rs.getString("item"));
}
}
catch (SQLException ex) {
Logger.getLogger(shopejb.class.getName()).log(Level.SEVERE,
null, ex);
} catch
(ClassNotFoundException ex) {
Logger.getLogger(shopejb.class.getName()).log(Level.SEVERE,
null, ex);
}
return l;
}
// Add business logic below. (Right-click in editor
and choose
// "Insert Code > Add Business Method")
}
Shop.java:
/*
* To change this template, choose Tools
| Templates
* and open the template in the editor.
*/
package action;
import ejb.shopejb;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
/**
*
* @author ITLABC55
*/
public class shop extends HttpServlet {
shopejb shopejb = lookupshopejbBean();
/**
* Processes requests for both HTTP
<code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a
servlet-specific error occurs
* @throws IOException if an I/O error
occurs
*/
protected void
processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
int i=0;
RequestDispatcher rd;
ArrayList items=new ArrayList();
String
option=request.getParameter("buttons");
if(option.equals("book"))
{
items=shopejb.getitems(option);
request.setAttribute("l",
items);
rd=request.getRequestDispatcher("book.jsp");
rd.forward(request, response);
}
if(option.equals("electronic"))
{
items=shopejb.getitems(option);
request.setAttribute("l",
items);
rd=request.getRequestDispatcher("electronic.jsp");
rd.forward(request, response);
}
if(option.equals("games"))
{
items=shopejb.getitems(option);
request.setAttribute("l",
items);
rd=request.getRequestDispatcher("games.jsp");
rd.forward(request, response);
}
if(option.equals("addtocart"))
{
String[]
item=request.getParameterValues("groups");
for(i=0;i<item.length;i++)
items.add(i,item[i]);
shopejb.add(items);
rd=request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
}
if(option.equals("remove"))
{
String[]
item=request.getParameterValues("groups");
for(i=0;i<item.length;i++)
items.add(i, item[i]);
items=shopejb.remove(items);
request.setAttribute("list",
items);
rd=request.getRequestDispatcher("display.jsp");
rd.forward(request, response);
}
if(option.equals("display"))
{
items=shopejb.display();
request.setAttribute("list",items);
rd=request.getRequestDispatcher("display.jsp");
rd.forward(request, response);
}
/* TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet
shop</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet shop
at " + request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");
*/
} finally {
out.close();
}
}
// <editor-fold
defaultstate="collapsed" desc="HttpServlet methods. Click on the
+ sign on the left to edit the code.">
/**
* Handles the HTTP
<code>GET</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 doGet(HttpServletRequest
request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* 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>
private shopejb lookupshopejbBean() {
try {
Context c = new InitialContext();
return (shopejb) c.lookup("java:global/Shopping-war/shopejb!ejb.shopejb");
} catch (NamingException ne) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE,
"exception caught", ne);
throw new RuntimeException(ne);
}
}
}
Result:
Thus the project shopping cart is
created using ejb components and the output is verified successfully.
Ex:no:3
Ejb components for Order processing
Date:11.9.2012
Aim:
To create a project order processing using
ejb components.
Algorithm:
Step 1:Create a new project order
process.
Step 2:Create a index page with a link
to product catalogue display page.
Step 3:Create home.jsp and retrieve the
data’s from catalog database and display it in the table.
Step 4:Select the product you are
willing to purchase by selecting the checkbox and enter the no of products you
require and submit order.
Step 5:When order is clicked,the servlet
moves to the database and check for availability,if the product is available
make for payment,else display no product available.
Step 6:For payment,calculate the total
amount,if amount exceeds 1,00,000 display error message transaction failed,else
display order success.
Step 7:Run the project and display the
output.
Program:
Index.jsp:
<%--
Document :
index
Created on : Sep 11, 2012, 9:10:19 AM
Author : ITLABC55
--%>
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<center>
WELCOME TO SHOP
<form action="servlet">
<br>
<br>
<input type="submit"
name="buttons" value="home" />
</form></center>
</body>
</html>
Home.jsp:
<%--
Document :
home
Created on :Sep 11, 2012, 9:11:01 AM
Author : ITLABC55
--%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<center>LIST OF AVAILABLE
PRODUCTS</center>
<br>
<br>
<br>
<hr>
<%
ArrayList l = null;
l =
(ArrayList)request.getAttribute("list");
ArrayList l1=null;
l1=(ArrayList)request.getAttribute("error");
if (!(l.isEmpty())) {%>
<center><form
action="servlet">
<table border="0"
cellpadding="0" cellspacing="7">
<tr color="red"><th>
SELECTED
</th>
<th>
PRODUCTID
</th>
<th>
PRODUCTNAME
</th>
<th>
PRICE
</th>
<th>
QUANTITY
</th>
<th>
REQUIRED
</th>
</tr>
<%for (int i = 0; i < l.size();i+=4) {%>
<tr>
<td>
<input type="checkbox"
name="group" value="<%=l.get(i+1)%>">
</td>
<td>
<%=l.get(i)%>
</td>
<td>
<%=l.get(i + 1)%>
</td>
<td>
<%=l.get(i + 2)%>
</td>
<td>
<%=l.get(i + 3)%>
</td>
<td>
<input type="text"
name="<%=l.get(i+1)%>"/>
</td>
</tr>
<%}%>
</table>
<input type="submit"
name="buttons" value="order"/>
</form>
<%}%>
<hr>
<%
if(!l1.isEmpty())
{
for(int i=0;i<l1.size();i++)
{%>
<font
color="red"><%=l1.get(i)%></font>
<% }
}%>
</center>
</body>
</html>
Success.jsp:
<%--
Document :
success
Created on :Sep 11, 2012, 9:30:56 AM
Author : ITLABC55
--%>
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<% String n=null;
n=(String)request.getAttribute("status");
if(n.equals("true"))
{%>
<font color="green" >Transaction has
been Successfully completed</font>
<%}else
{
%>
<font color="red">process
fail-Amount exceeds 1 LAKH</font>
<%}%>
<form action="servlet">
<input type="submit"
value="home" name="buttons"/>
</form>
</html>
Session.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ejb;
import java.lang.String;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
/**
*
* @author ITLABC55
*/
@Stateless
@LocalBean
public class session {
public Connection getconnect() {
String path = "D:/SOA LAB/3rd/3.mdb";
String db = "jdbc:odbc:Driver={Microsoft Access
Driver (*.mdb)};DBQ=" + path + ";DriverID=22";
Connection con = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(db);
} catch (Exception e) {
System.out.print(e);
} finally {
return con;
} }
public ArrayList getlist() throws SQLException {
Connection con = null;
ArrayList list = new ArrayList();
try {
con = getconnect();
if (con == null) {
System.out.println("error");
} else {
String qu = "select * from 3";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(qu);
while (rs.next()) {
list.add(rs.getString("productid"));
list.add(rs.getString("productname"));
list.add(rs.getString("price"));
list.add(rs.getString("quantity"));
}
}
} catch (SQLException e) {
System.out.println(e);
} finally {
con.close();
}
return list;
}
public int getqty(String qty) {
Connection con = null;
int quantity=0;
try {
con = getconnect();
if (con == null) {
System.out.println("error");
} else {
String qu = "select * from 3";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(qu);
while (rs.next()) {
if(rs.getString("productname").equals(qty))
{
quantity=Integer.parseInt(rs.getString("quantity"));
break;
}
}
}
}
catch(Exception e)
{
System.out.print(e);
}
return quantity;
}
public int getprice(String name) {
Connection con = null;
int price=0;
try {
con = getconnect();
if (con == null) {
System.out.println("error");
} else {
String qu = "select * from 3";
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(qu);
while (rs.next()) {
if(rs.getString("productname").equals(name))
{
price=Integer.parseInt(rs.getString("price"));
break;
}
}
}
}
catch(Exception e)
{
System.out.print(e);
}
return price;
}
public void update(int n, String name)throws
SQLException {
Connection con = null;
try {
con = getconnect();
if (con == null) {
System.out.println("error");
} else {
int quantity=getqty(name)-n;
String qu="update 3 set
[quantity]="+quantity+" where
[productname]='"+name+"'";
PreparedStatement st=con.prepareStatement(qu);
st.executeUpdate();
}
}
catch(SQLException e)
{
System.out.println(e);
}
finally
{
con.close();
}
}
// Add business logic below. (Right-click in editor
and choose
// "Insert Code > Add Business Method")}
Servlet.java:
/*
* To change this template, choose Tools
| Templates
* and open the template in the editor.
*/
package action;
import ejb.session;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
/**
*
* @author ITLABC55
*/
public class servlet extends HttpServlet
{
@EJB
private session session;
/**
* Processes requests for both HTTP
<code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific
error occurs
* @throws IOException if an I/O error
occurs
*/
protected void
processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException,
SQLException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
ArrayList list = new ArrayList();
ArrayList error=new ArrayList();
String option =
request.getParameter("buttons");
RequestDispatcher rd;
if (option.equals("home")) {
list = session.getlist();
request.setAttribute("list",
list);
request.setAttribute("error",error);
rd =
request.getRequestDispatcher("home.jsp");
rd.forward(request, response);
}
if(option.equals("order"))
{
String
[]check=request.getParameterValues("group");
int need=0;
int availability=0;
int total=0;
for(int i=0;i<check.length;i++)
{
need=Integer.parseInt(request.getParameter(check[i]));
availability=session.getqty(check[i]);
if(availability<need)
{
error.add("there is only
"+availability+" "+check[i]+" available");
}
else
{
total+=need*session.getprice(check[i]);
}}
if(error.isEmpty())
{
if(total>100000) {
request.setAttribute("status","false");
rd =
request.getRequestDispatcher("success.jsp");
rd.forward(request, response);
}
else
{
for(int i=0;i<check.length;i++)
{
int ne=Integer.parseInt(request.getParameter(check[i]));
session.update(ne,check[i]);
}
request.setAttribute("status","true");
rd =
request.getRequestDispatcher("success.jsp");
rd.forward(request, response);
}
}
else
{
list = session.getlist();
request.setAttribute("list",
list);
request.setAttribute("error",error);
rd =
request.getRequestDispatcher("home.jsp");
rd.forward(request, response);
}
}
}finally
{
out.close();
}
}
// <editor-fold
defaultstate="collapsed" desc="HttpServlet methods. Click on the
+ sign on the left to edit the code.">
/**
* Handles the HTTP
<code>GET</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 doGet(HttpServletRequest
request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(servlet.class.getName()).log(Level.SEVERE,
null, ex);
}
}
/**
* 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 {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(servlet.class.getName()).log(Level.SEVERE,
null, ex);
}
}
/**
* Returns a short description of the
servlet.
* @return a String containing servlet
description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>}
Result:
Thus,the project order processing is
created by using ejb components and the output is verified successfully.
Ex:no:4 Ejb components for Payment
processing
Date:25.9.2012
Aim:
To create a project payment processing using
ejb components.
Algorithm:
Step 1
:Create a new project payment
Step 2
:Create a home.jsp to get the informations like cardnumber,cardtype,ccv
number,name on card and expirary date.
Step 3
:When the card details is submitted ,connect to the servlet
payprocess,get the card details and verify it with the details in the database
using ejb component.
Step 4
:If the details are correct then check for the balance.
Step 4.1:If the required balance is
available display payment success.
Step 4.2:If balance is not
available,then display insufficient balance.
Step 5
:Else if the card details submitted is wrong,display enter correct card
details.
Step 6
:Run the project and display
the output.
Program:
Index.jsp
<%--
Document :
index
Created on : Sep 25, 2012, 9:23:08 AM
Author : ITLABC55
--%>
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<% String
res=(String)request.getAttribute("res");
%>
<center><font
color="#ccccc">Payment processing</font></center>
<form action="servlet"
method="get">
<table align="center"
border="0" cellspacing="7">
<tr><td>
Cardtype:</td><td><input
type="radio" name="ctype"
value="visa"/>master-visa card
<input type="radio"
name="ctype" value="debit" />debit card
</td>
</tr>
<tr><td> Card no: </td><td>
<input type="text" name="cardno"
size="20"/></td>
</tr>
<tr><td>Expirary date </td>
<td><select name="eyear">
<option
value="year">year</option><br>
<option
value="2018">2018</option><br>
<option
value="2017">2017</option><br>
<option value="2016">2016</option><br>
<option
value="2015">2015</option><br>
<option
value="2014">2014</option><br>
<option
value="2013">2013</option><br>
<option
value="2012">2012</option><br></select>
<select name="emonth">
<option
value="month">month</option><br>
<option value="jan">jan</option><br>
<option
value="feb">feb</option><br>
<option
value="mar">mar</option><br>
<option
value="apr">apr</option><br>
<option
value="may">may</option><br>
<option
value="june">june</option><br>
<option
value="july">july</option><br>
<option value="aug">aug</option><br>
<option
value="sep">sep</option><br>
<option
value="oct">oct</option><br>
<option
value="nov">nov</option><br>
<option
value="dec">dec</option><br></select>
</td>
</tr>
<tr><td> ccv number:</td>
<td> <input type="password" name="ccvno"
size="10"/></td></tr>
<tr><td> Name on card: </td>
<td> <input type="text" name="name"
size="20"/></tr>
<br>
<br>
<tr><td> Amount :</td> <td>
<input type="text" name="amt"
size="10"/></td></tr>
</table>
<br>
<br>
<center> <input type="submit"
name="submit" value="pay"/>
<input
type="submit" name="submit" value="cancel"/>
</center>
</form>
<br>
<br>
<br>
<%
if(res != null)
{
%>
<br>
<br>
<br>
<center><font color="red">
<%=res%></font></center>
<% } %>
</body>
</html>
Payment.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ejb;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
/**
*
* @author ITLABC55
*/
@Stateless
@LocalBean
public class payment {
public Connection getconnect() {
Connection con = null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String path = "D:/SOA LAB/4th/4th/4.mdb";
String db = "jdbc:odbc:Driver={Microsoft Access
Driver (*.mdb)};DBQ=" + path + ";DriverID=22";
con=DriverManager.getConnection(db,"
"," ");
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
return con;
}
}
public int check(String cardno, String year, String
month, String ccvno,String amount)throws SQLException {
Connection con=null;
int n=0;
String amt=null;
try
{
con=getconnect();
if(con==null)
System.out.print("database connection is
null");
else
{
Statement s=con.createStatement();
ResultSet rk=s.executeQuery("select * from
info");
while(rk.next())
{
if((cardno.equals(rk.getString("cardno")))&&(year.equals(rk.getString("year")))&&(month.equals(rk.getString("month")))&&(ccvno.equals(rk.getString("ccvno"))))
{
amt=rk.getString("amount");
if(Integer.parseInt(amount)<Integer.parseInt(amt))
{
n=Integer.parseInt(amt)-Integer.parseInt(amount);
update(cardno, n);
n=1;
}
else
n=-1;
break;
}
else
{
n=0;
}
}
}
}
catch(SQLException e)
{
System.out.println(e);
}
finally
{
con.close();
}
return n;
}
public void update(String card, int n)throws
SQLException
{
Connection con = null;
try {
con = getconnect();
if (con == null) {
System.out.println("error");
} else {
String qu="update info set
[amount]="+n+" where [cardno]='"+card+"'";
PreparedStatement st=con.prepareStatement(qu);
st.executeUpdate();
}
}
catch(SQLException e)
{
System.out.println(e);
}
finally
{
con.close();
}
}
// Add business logic below. (Right-click in editor
and choose
// "Insert Code > Add Business Method")
}
Servlet.java:
/*
* To change this template, choose Tools
| Templates
* and open the template in the editor.
*/
package action;
import ejb.payment;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author ITLABC55
*/
public class servlet extends HttpServlet
{
@EJB
private payment payment;
/**
* Processes requests for both HTTP
<code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a
servlet-specific error occurs
* @throws IOException if an I/O error
occurs
*/
protected void
processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException,
SQLException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String
cardno=request.getParameter("cardno");
String
ccvno=request.getParameter("ccvno");
String year=request.getParameter("eyear");
String
month=request.getParameter("emonth");
String
option=request.getParameter("submit");
String
amount=request.getParameter("amt");
int n=0;
if(option.equals("pay"))
{
n=payment.check(cardno, year, month,
ccvno,amount);
System.out.print(n);
if(n==1)
{
request.setAttribute("res","payment
success");
RequestDispatcher
rd=request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
}
else if(n==-1)
{
request.setAttribute("res","there
is no sufficient amount in the bank");
RequestDispatcher
rd=request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
}
else
{
request.setAttribute("res","check
ur account details");
RequestDispatcher
rd=request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
}
}
/* TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet
servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet
servlet at " + request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");
*/
} finally {
out.close();
}
}
// <editor-fold
defaultstate="collapsed" desc="HttpServlet methods. Click on the
+ sign on the left to edit the code.">
/**
* Handles the HTTP
<code>GET</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 doGet(HttpServletRequest
request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(servlet.class.getName()).log(Level.SEVERE,
null, ex);
}
}
/**
* 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 {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(servlet.class.getName()).log(Level.SEVERE,
null, ex);
}
}
/**
* Returns a short description of the
servlet.
* @return a String containing servlet
description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Result:
Thus,the project payment processing is
created by using ejb components and the output is verified successfully.
Ex:no:5 Ejb components for Banking
transaction
Date:9.10.2012
Aim:
To create a project banking transaction using
ejb components.
Algorithm:
Step 1:Create a new project banktrans.
Step 2:Create a index.jsp to get the
account number.
Step 3:When account no is submitted
dispatch to home.jsp which have facility to credit,debit and balance check.
Step 4:If the user choose balance check
retrieve the balance to the respective account from the database,and display
the balance.
Step 5:If the user choose credit or
debit amount create a stateful session to temporily store the transaction
history until the user logout.
Step 6: Insert the details like account
no,transaction type,amount and time of transaction in a separate databse for
every transaction.
Step 7:For all the three process perform
the business methods by using ejb components via servlet.
Step 8:Run the project and display the output.
Program:
Index.jsp:
<%--
Document :
index
Created on : Oct 9, 2012, 9:44:11 AM
Author : ITLABC55
--%>
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
Bank Transaction
<form action="servlet">
ACCOUNT NO:<input type="text"
name="accno">
<input type="submit"
name="submit" value="enter">
</form>
</body>
</html>
Firstpage.jsp:
<%--
Document :
firstpage
Created on : Oct 9, 2012, 9:04:57 AM
Author : ITLABC55
--%>
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="servlet">
<pre>
ENTER THE OUNT HERE:
<input type="text"
ne="ount"/>
<input type="submit"
ne="submit" value="credit"/> <input type="submit"
ne="submit" value="debit"/> <input type="submit"
ne="submit" value="balance"/>
</pre>
</form>
</body>
</html>
Balance.jsp:
<%--
Document :
balance
Created on : Oct 9, 2012, 9:56:40AM
Author : ITLABC55
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%String bal=null;
bal=(String)request.getAttribute("bal");
%>
<h2>
your account balance</h2>
<%if(bal!=null)
{%>
your balance is<%= bal %>
<%}
%>
<form action="servlet">
<input type="submit"
ne="submit" value="back"> <input type="submit"
ne="submit" value="logout">
</form>
</body></html>
Credit.jsp:
<%--
Document :
credit
Created on : Oct 9, 2012, 9:56:11 AM
Author : ITLABC55
--%>
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Has been successfully credited into ur
account</h1>
<form action="servlet">
<input type="submit" ne="submit"
value="home">
</form>
</body>
</html>
Debit.jsp:
<%--
Document :
debit
Created on : Oct 9, 2012, 9:56:27 AM
Author : ITLABC55
--%>
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Has been debited from your account
successfully</h1>
<form action="servlet">
<input type="submit"
ne="submit" value="home">
</form>
</body>
</html>
Session.java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ejb;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.ejb.Stateful;
import javax.ejb.LocalBean;
/**
*
* @author ITLABC55
*/
@Stateful
@LocalBean
public class session {
public int n;
public int i=0;
String[] a=new String[10];
public String acc;
public ArrayList list=new ArrayList();
public void credit(int ount)throws SQLException {
try
{
n=ount+n;
list.add(acc);
list.add("credit");
list.add(""+ount);
list.add(""+n);
list.add(gettime());
}
catch(Exception e)
{
System.out.print(e);
}
}
public void debit(int ount)throws SQLException {
String time=null;
try
{
n=n-ount;
list.add(acc);
list.add("debit");
list.add(""+ount);
list.add(""+n);
list.add(gettime());
}
catch(Exception e)
{
System.out.print(e);
}
}
public int balance(String accno)throws SQLException
{
return n;
}
public Connection getconnect() {
String path = "D:/SOA LAB/5th/5th/5.mdb";
String db = "jdbc:odbc:Driver={Microsoft Access
Driver (*.mdb)};DBQ=" + path + ";DriverID=22";
Connection con = null;
try
{
Class.forNe("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(db);
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
return con;
}
}
public void getount(String accno)throws SQLException
{
Connection con=null;
String t;
int s=0;
try {
con = getconnect();
if (con == null) {
System.out.println("error");
}
else
{
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from
info2");
while(rs.next())
{
if(accno.equals(rs.getString("accountno")))
{
t=rs.getString("ount");
n=Integer.parseInt(t);
acc=accno;
break;
}
}
}
}
catch(SQLException e)
{
System.out.print(e);
}
finally
{
con.close();
}
}
public void update()throws SQLException{
Connection con = null;
try {
transaction();
con = getconnect();
if (con == null) {
System.out.println("error");
} else {
String qu="update info2 set
[ount]="+n+" where [accountno]='"+acc+"'";
PreparedStatement st=con.prepareStatement(qu);
st.executeUpdate();
}
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
con.close();
list=new ArrayList();
}
}
public String gettime() {
Date d = new Date();
SimpleDateFormat sd = new
SimpleDateFormat("dd/MM/yy hh:mm:ss");
String frmt = sd.format(d);
return frmt;
}
public void transaction()throws SQLException {
Connection con = null;
String
accno=null,trans=null,bal=null,tim=null,t=null;
try {
con = getconnect();
if (con == null) {
System.out.println("error");
} else {
for(int k=0;k<list.size();k+=5)
{
accno=(String)list.get(k);
trans=(String)list.get(k+1);
t=(String)list.get(k+2);
bal=(String)list.get(k+3);
tim=(String)list.get(k+4);
String sql ="insert into
info3(accountno,transaction,ount,balance,_time)values(?,?,?,?,?)";
PreparedStatement st = con.prepareStatement(sql);
st.setString(1,accno);
st.setString(2,trans);
st.setString(3,t);
st.setString(4,bal);
st.setString(5,tim);
st.executeUpdate();
}
}
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
con.close();
}
}
// Add business logic below. (Right-click in editor
and choose
// "Insert Code > Add Business Method")
}
Servlet.java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package action;
import ejb.session;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ning.Context;
import javax.ning.InitialContext;
import javax.ning.NingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author ITLABC55
*/
public class servlet extends HttpServlet {
session session = lookupsessionBean();
/**
* Processes requests for both HTTP
<code>GET</code> and <code>POST</code> methods.
* @par request servlet request
* @par response servlet response
* @throws ServletException if a servlet-specific
error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest
request, HttpServletResponse response)
throws ServletException, IOException, SQLException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String accno=request.getPareter("accno");
String ount=request.getPareter("ount");
String
option=request.getPareter("submit");
RequestDispatcher rd;
if(option.equals("enter"))
{
session.getount(accno);
//System.out.println(session.n);
rd=request.getRequestDispatcher("firstpage.jsp");
rd.forward(request, response);
}
if(option.equals("back"))
{
request.setAttribute("ne",accno);
rd=request.getRequestDispatcher("firstpage.jsp");
rd.forward(request, response);
}
if(option.equals("credit"))
{
session.credit(Integer.parseInt(ount));
rd=request.getRequestDispatcher("firstpage.jsp");
rd.forward(request, response);
}
if(option.equals("debit"))
{
session.debit(Integer.parseInt(ount));
rd=request.getRequestDispatcher("firstpage.jsp");
rd.forward(request, response);
}
if(option.equals("logout"))
{
session.update();
rd=request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
}
if(option.equals("balance"))
{
int n;
n=session.balance(accno);
System.out.println(n);
request.setAttribute("bal"," "+n);
rd=request.getRequestDispatcher("balance.jsp");
rd.forward(request, response);
}
/* TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet
servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet servlet at
" + request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");
*/
} finally {
out.close();
}
}
// <editor-fold
defaultstate="collapsed" desc="HttpServlet methods. Click on the
+ sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code>
method.
* @par request servlet request
* @par response servlet response
* @throws ServletException if a servlet-specific
error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(servlet.class.getNe()).log(Level.SEVERE,
null, ex);
}
}
/**
* Handles the HTTP <code>POST</code>
method.
* @par request servlet request
* @par 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 {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(servlet.class.getNe()).log(Level.SEVERE,
null, ex);
}
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
private session lookupsessionBean() {
try {
Context c = new InitialContext();
return (session)
c.lookup("java:global/5th-war/session!ejb.session");
} catch (NingException ne) {
Logger.getLogger(getClass().getNe()).log(Level.SEVERE,
"exception caught", ne);
throw new RuntimeException(ne);
}
}
}
Result:
Thus,the project banking transaction is
created by using ejb components and the output is verified successfully.
No comments:
Post a Comment