Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Right click under Project Explorer and Select New->Dynamic Web Project.





  2. Name the project as StatefulClient and Select Next.





  3. Keep the default settings as shown in the figure. Select Next.





  4. On the next screen keep default values. Select Next.





  5. Default values on this screen too. Select Finish.





  6. Right click on the StatefulClient project and Select New->Servlet.





  7. Name the package as ejb.stateful and Servlet as Controller.





  8. Keep the default values and Select Next.





  9. Keep the default values and Select Finish.





  10. Once the servlet is created it shows error. This is due to servlet api missing from the runtime. This can be easily resolved. Right click on StatefulClient project and Select properties.





  11. On the next screen select Java build path and select Libraries.





  12. Select Add External jars.





  13. Browse to your <GERONIMO_HOME>\repository\org\apache\geronimo\specs\geronimo-servlet_2.5_spec\1.1.2 and select geronimo-servlet_2.5_spec-1.1.2.jar and Select Open.





  14. Select Ok on the next screen this will remove all the errors.





  15. Add the following code to Controller.java servlet.
    Code Block
    titleController.java
    borderStylesolid
    package ejb.stateful;
    
    import java.io.IOException;
    import java.util.Properties;
    
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    /**
     * Servlet implementation class for Servlet: Controller
     *
     */
     public class Controller extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
       static final long serialVersionUID = 1L;
       
       AccountCreator ac;
       
       
        /* (non-Java-doc)
    	 * @see javax.servlet.http.HttpServlet#HttpServlet()
    	 */
    	public Controller() {
    		super();
    	}   	
    	
    	/* (non-Java-doc)
    	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	
    /*PrintWriter out =response.getWriter();
    out.println(request.getRequestURI());*/
    	try{
    		Properties prop=new Properties();
    		  prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
    		  prop.put("java.naming.provider.url", "ejbd://localhost:4201");
    		   Context context = new InitialContext(prop);
    		   ac=(AccountCreator)context.lookup("AccountCreatorBeanRemote");
    		}
    		catch(Exception e)
    		{
    			e.printStackTrace();
    		}
    	if ( (request.getRequestURI()).equals("/StatefulClient/Controller"))
    	{
    		PersonalInfo personalinfo=new PersonalInfo();
    		personalinfo.setFirstName(request.getParameter("FirstName"));
    		personalinfo.setLastName(request.getParameter("LastName"));
    		personalinfo.setNationality(request.getParameter("Nationality"));
    		personalinfo.setUserName(request.getParameter("UserName"));
    		personalinfo.setPassword(request.getParameter("Password"));
    		ac.addPersonalInfo(personalinfo);
    		HttpSession hs= request.getSession(true);
    		hs.setAttribute("handle", ac);
    		RequestDispatcher rd=request.getRequestDispatcher("BillingInfo.jsp");
    		rd.forward(request, response);
    		
    	}
    	else
    	{
    		BillingInfo billingInfo=new BillingInfo();
    		billingInfo.setBank(request.getParameter("Bank"));
    		billingInfo.setCardno(request.getParameter("CardNo"));
    		billingInfo.setCity(request.getParameter("City"));
    		billingInfo.setCountry(request.getParameter("Country"));
    		billingInfo.setHouseNo(request.getParameter("HouseNo"));
    		billingInfo.setPincode(request.getParameter("PinCode"));
    		billingInfo.setStreet(request.getParameter("Street"));
    		HttpSession hs= request.getSession(true);
    		ac=(AccountCreator)hs.getAttribute("handle");
    		ac.addBillingInfo(billingInfo);
    		ac.createAccount();
                      PrintWriter out=response.getWriter();
    		out.println("Account successfully created");
    	}
    		
    	}
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		doProcess(request, response);
    	}  	
    	
    	/* (non-Java-doc)
    	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		doProcess(request, response);
    		// TODO Auto-generated method stub
    	}   	  	    
    }



  16. This code contains EJB specific annotation and so we need to add EJB spec definition to the project. Follow the previous procedure to Add External jars and browse to <GERONIMO_HOME>\repository\org\apache\geronimo\specs\ geronimo-ejb_3.0_spec\1.0.1 and add geronimo-ejb_3.0_spec-1.0.1.jar to java build path. This servlet also contains code referring to bean interface class and PersonalInfo and BilllingInfo class. We need to add these projects to the build path so that the classes can be compiled. Right click on StatefulClient project and Select Properties->Java Build Path->Projects. Select Add.





  17. Check StatefulBean and Select Ok.





  18. Once done the project will be visible in the build path. Select Ok.





  19. Next step is to add jsp pages to our client project. Right click on WebContent under StatefulClient project and Select New->jsp.





  20. Name the jsp as PersonalInfo.jsp and Select Next.





  21. On the next screen select Finish.





  22. Add the following code to PersonalInfo.jsp.


    Code Block
    titlePersonalInfo.jsp
    borderStylesolid
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!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=ISO-8859-1">
    <title>User Personal Information</title>
    </head>
    <body>
    <form action="Controller">
    <h1>Enter your personal information</h1>
    <table>
    <tr>
    <td><h3>Name</h3></td>
    </tr>
    <tr>
    <td>FirstName</td>
    <td><input type="text" name="FirstName"></td>
    </tr>
    <tr>
    <td>LastName</td>
    <td><input type="text" name="LastName"></td>
    </tr>
    <tr><td><h3>Nationality</h3></td></tr>
    <tr>
    <td>Nationality</td>
    <td><input type="text" name="Nationality"></td>
    </tr>
    <tr><td><h3>Login</h3></td></tr>
    <tr>
    <td>UserName</td>
    <td><input type="text" name="UserName"></td>
    </tr>
    <tr>
    <td>Password</td>
    <td><input type="password" name="Password"></td>
    </tr>
    </table>
    <input type="submit" Name="Next">
    </form>
    </body>
    </html>
    



  23. Similarly add another jsp with the name BillingInfo.jsp and add the following code.


    Code Block
    titleBillingInfo.jsp
    borderStylesolid
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!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=ISO-8859-1">
    <title>User Billing Information</title>
    </head>
    <body>
    <form action="Controller1">
    <h1>Enter your Billing information</h1>
    <table>
    <tr>
    <td><h3>Address</h3></td>
    </tr>
    <tr>
    <td>House No</td>
    <td><input type="text" name="HouseNo"></td>
    </tr>
    <tr>
    <td>Street</td>
    <td><input type="text" name="Street"></td>
    </tr>
    <tr>
    <td>City</td>
    <td><input type="text" name="City"></td>
    </tr>
    <tr>
    <td>PinCode</td>
    <td><input type="text" name="PinCode"></td>
    </tr>
    <tr>
    <td>Country</td>
    <td><input type="text" name="Country"></td>
    </tr>
    <tr><td><h3>Credit Card Information</h3></td></tr>
    <tr>
    <td>Bank</td>
    <td><input type="text" name="Bank"></td>
    </tr>
    <tr>
    <td>Card No</td>
    <td><input type="text" name="CardNo"></td>
    </tr>
    </table>
    <input type="submit" name="Submit">
    </form>
    </body>
    </html>
    
  24. Let us walkthrough the servlet and jsp code. First through Controller servlet code. @EJB AccountCreator ac;- @EJB annotation is used to inject an EJB to the client code.
    • if ( (request.getRequestURI()).equals("/StatefulClient/Controller"))- This code act as a controller on the jsp which is making a request. This is possible only when the jsp's make a call to th servlet with different names. How this can be done will be illustrated in the next section.
    • HttpSession hs= request.getSession(true); hs.setAttribute("handle", ac); This part of the code saves the remote interface handle and later in the second call same handle is used to make another call to bean methods.
    • RequestDispatcher rd=request.getRequestDispatcher("BillingInfo.jsp")- This code section and the next line forwards the control to next jsp that is BillingInfo.jsp.
    • Rest of the servlet deals with calling the setter methods and later sets the object so as to persist the data between different calls.
  25. Next walkthrough the jsp code.
    • PersonalInfo.jsp has <form action="Controller"> whereas BillingInfo.jsp has <form action="Controller1"> as the action element but both internally calling the same servlet. This can be easily done by modifying web.xml this will be shown in the next section.

...