In this EJB-JPA Tutorial, we will create servlets which talks to SavingsAccount Management html forms. These servlets will perform form processing of our EJB-JPA CRUD Example.
Create Account
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package tube.codingexamples.servlets; import java.io.IOException; import javax.ejb.EJB; import javax.servlet.RequestDispatcher; 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 tube.codingexamples.ejb.statelessbean.SavingAcBeanRemote; @WebServlet("/CreateAccountServlet") public class CreateAccountServlet extends HttpServlet { //Sample 4.05: Declare EJB Stateless Session Bean @EJB(beanName="SavingAcBean") SavingAcBeanRemote SACBean; private static final long serialVersionUID = 1L; public CreateAccountServlet() { super(); } @Override protected void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Sample 4.06: Get Form Field Data String PersonName = request.getParameter("person_name"); String InitBalance = request.getParameter("balance"); if (PersonName != null && PersonName.trim().length() > 0) { //Sample 4.07: Call EJB to request new account creation int balance = new Integer(InitBalance).intValue(); int id = SACBean.newAccount(PersonName, balance); //Sample 4.08: Delegate response to a JSP File request.setAttribute("Action", "Add"); request.setAttribute("NewAccountID", id); RequestDispatcher respJSP = request.getRequestDispatcher("SavingsAcResponse.jsp"); respJSP.forward(request, response); } } } |
Transact Account
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
package tube.codingexamples.servlets; import java.io.IOException; import javax.ejb.EJB; import javax.servlet.RequestDispatcher; 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 tube.codingexamples.ejb.statelessbean.SavingAcBeanRemote; @WebServlet("/AcTransactions") public class AcTransactions extends HttpServlet { //Sample 4.09: Declare EJB Stateless Session Bean @EJB(beanName="SavingAcBean") SavingAcBeanRemote SACBean; private static final long serialVersionUID = 1L; public AcTransactions() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Sample 4.10: Get Form Field Data String pid = request.getParameter("pid"); if (pid != null && pid.trim().length() > 0) { int custid = new Integer(pid).intValue(); String amount = request.getParameter("amount"); String action = request.getParameter("transaction"); //Sample 4.11: Call EJB to perform Transaction int iamount = new Integer(amount).intValue(); if (action.compareToIgnoreCase("Deposit") == 0) { int newBalance = 0; newBalance = SACBean.depositMoney(custid, iamount); request.setAttribute("Action", "deposit"); request.setAttribute("NewBalance", newBalance); } else { int newBalance = 0; newBalance = SACBean.withdrawMoney(custid, iamount); request.setAttribute("Action", "withdraw"); request.setAttribute("NewBalance", newBalance); } //Sample 4.12: Delegate response to a JSP File RequestDispatcher respJSP = request.getRequestDispatcher("SavingsAcResponse.jsp"); respJSP.forward(request, response); } } } |
Close Account
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
package tube.codingexamples.servlets; import java.io.IOException; import javax.ejb.EJB; import javax.servlet.RequestDispatcher; 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 tube.codingexamples.ejb.statelessbean.SavingAcBeanRemote; @WebServlet("/CloseAcServlet") public class CloseAcServlet extends HttpServlet { //Sample 4.13: Declare EJB Stateless Session Bean @EJB(beanName="SavingAcBean") SavingAcBeanRemote SACBean; private static final long serialVersionUID = 1L; public CloseAcServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Sample 4.14: Call EJB to Close the A/c boolean ret = false; String pid = request.getParameter("pid"); if (pid != null && pid.trim().length() > 0) { int custid = new Integer(pid).intValue(); ret = SACBean.closeSavingsAc(custid); } //Sample 4.15: Set Request Attribute and Delegate processing to JSP request.setAttribute("Action", "close"); request.setAttribute("CloseVal", new Boolean(ret).toString()); RequestDispatcher respJSP = request.getRequestDispatcher("SavingsAcResponse.jsp"); respJSP.forward(request, response); } } |
Categories: JavaEE-EJB-Tube