In this EJB JPA Tutorial, we will modify the html form to collect City and Steet to populate the Location Entity. The servlet will collect these two information and pass it to the Entity Manager sitting in the Bean.
Html Form Changes for Location Entity
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 |
<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <!-- Sample 4.01: Create Form for Account Creation --> <h1>Savings Account Creation Form</h1> <FORM action="http://localhost:8080/EJBWeb/CreateAccountServlet"> Enter Your Name: <br> <input name="person_name" type="text" size="25" value=""><br> Initial Amount: <br> <Input name="balance" type="text" size="5" value=""><br> <!-- Sample 7.10: To learn 1-1 Relation --> Street: <Input name="Street" type="text" size="15" value=""> City: <Input name="City" type="text" size="15" value=""><br> <input name="NewAc" type="submit" value="Submit"><br> </FORM> <h3> <a href="http://localhost:8080/EJBWeb/SavingsAcHomePage.html">Home </a> </h3> </body> </html> |
Servlet Layer Changes to Collect City and State
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 |
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 { String PersonName = request.getParameter("person_name"); String InitBalance = request.getParameter("balance"); //Sample 7.11: For Location Entity String Street = request.getParameter("Street"); String City = request.getParameter("City"); if (PersonName != null && PersonName.trim().length() > 0) { //Sample 4.07: Call EJB to request new account creation int balance = new Integer(InitBalance).intValue(); //Sample 7.12: Create new account int id = SACBean.newAccount(PersonName, balance, Street, City ); //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); } } } |
Categories: JavaEE-EJB-Tube