EJB CICS sample application task guide: Development

sga030


The EJB CICS sample application task guide

Develop a web servelet using VisualAge for Java

This section describes the task steps to create the web servlet which provides an interface between the enterprise bean in the EJB CICS Sample and a JavaServer Page (JSP). We assume that you have completed all the tasks for setting up and configuring an enterprise bean environment in which to run the sample application. In addition we assume that you have:
  1. Installed the sample COBOL programs, DB2 components and CICS definitions that play the part of "existing" CICS COBOL applications in your system.
  2. Created the command beans which the enterprise bean will use to access data via the "existing" CICS COBOL applications.
  3. Created the enterpise bean in which sample application business logic is coded.

The tasks involved in creating the servlet are:

  1. Start up Visual Age for Java
  2. Create a servlet
  3. Customise the servlet for an EJB

In the CICS EJB Sample application the servlet classes are to be added to the project and package which contains the command beans and the enterprise bean.

Start up Visual Age for Java

  1. Click on Start Button->Programs->IBM VisualAge for Java for Windows V3.5->IBM VisualAge for Java.
  2. If necessary, select the Projects tab.
  3. Select your project from the project list. Sample: CICSEJBSample.
  4. Expand the project to show its contents the package cics.sample.

Create a Web servlet

The web servlet contains a class which is used at run time to pass data between the browser pages and the enterprise bean. It is also used at development time to generate:

    Create Servlet

  1. Right click on the cics.sample package and select Add -> Servlet to start the Create Servlet smartguide.
  2. Ensure that the correct project and package are shown. If necessary click on the Browse buttons to select the sample project: CICSEBJSample, and package: cics.sample.
  3. In the Class name field, enter the name for the servlet. Sample: SampleServlet.
  4. the Superclass field is automatically filled with the name of the class to inherit from. Sample: javax.servlet.http.Httpservlet.
  5. Check the Import Java bean option.
  6. Click Add package... to add each of the following statements:

    Create Servlet smartguide panel: Specify servlet

  7. Click Next until the Attributes panel of the Create Servlet smartguide is showing.
  8. On the Attributes panel set the properties required for the servlet. For the sample servlet these are:
    Modifier: public
    Create method stub: doGet()
    Create method stub: doPost()

    Create Servlet smartguide panel: Specify servlet attributes

  9. Click Finish.
    The servlet is added to the sample package cics.sample.

    Customise the servlet for an EJB

    Double click on the servlet class to see the class in a 'Class Browser'. The class browser shows the methods in the class and an editable view of the methods for the classes. Code must be added to the methods in the servlet class to make it pass input data from the HTML to the enterprise bean, and to pass data from the enterprise bean to the JSP.
  10. Select the doGet()method in the left pane to show the generated code stub in the right pane.
  11. The code for the selected method is editable in the bottom pane. Copy the following code and paste it to replace the stub code in the doGet() method:

    
    /**
    	 * Service method.
    	 */
    	public void doGet(HttpServletRequest request, HttpServletResponse response) 
    		throws ServletException, java.io.IOException {
    
    		response.sendRedirect("/cicssample/index.html");
    	}
    
    

  12. Press Ctrl-S to save the code.
  13. Select the doPost()method in the left pane to show the generated code stub in the right pane.
  14. The code for the selected method is editable in the bottom pane. Copy the following code and paste it to replace the stub code in the doPost() method:

    
    /**
    	 * This is the main Servlet method. It performs a lookup on the specified JNDI server, 
    	 * narrows the returned object to the enterprise bean home class, calls create() on the 
    	 * home interface, then invokes the sayHello() method on the enterprise bean. In a real
    	 * production Servlet, you should cache the JNDI information so that a JNDI lookup is 
    	 * not done every time.
    	 */
    	public void doPost(HttpServletRequest request, HttpServletResponse response) 
    		throws ServletException, java.io.IOException {
    
    		// Look up the enterprise bean.
    		java.util.Hashtable properties = new java.util.Hashtable(2);
    		String nameService = request.getParameter("nameService");
    		properties.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, nameService);
    		String providerURL = request.getParameter("providerURL");
    		properties.put(javax.naming.Context.PROVIDER_URL, providerURL);
    		String jndiName = request.getParameter("jndiName");
    		String customerNumber = request.getParameter("customerNumber");
    
    		CICSSampleHome sampleHome = null;
    		try {
    			//System.out.println("creating initial context");
    			javax.naming.InitialContext ctx = new javax.naming.InitialContext(properties);
    			//System.out.println("looking up "+jndiName);
    			Object obj = ctx.lookup(jndiName); // Use the JNDI name from HTML Page
    			//System.out.println("narrowing");
    			sampleHome = (CICSSampleHome)javax.rmi.PortableRemoteObject.narrow((org.omg.CORBA.Object)obj, CICSSampleHome.class);
    			//System.out.println("invoking create() on home interface");
    			CICSSample theEJB = sampleHome.create();
    			//System.out.println("invoking getCustomerInfo() on EJB");
    			// set Customer Info from CICS-DB2
    			int custNum = new Integer(request.getParameter("customerNumber")).intValue();
    			CustomerData sc = theEJB.getCustomerInfo(custNum);
    			//System.out.println("returned from EJB request:");
    
    			request.setAttribute("theCustomer", sc);
    
    			//System.out.println("Calling the JSP");
    			RequestDispatcher rd =
    				getServletContext().getRequestDispatcher("SampleResults.jsp");
    			rd.forward(request, response);
    
    		} catch (Throwable t) {
    			System.out.println("unexpected Throwable:"+t);
    			t.printStackTrace();
    
    			ErrorData ed;
    			System.out.println("Instantiating error bean to pass to JSP");
    			try {
    				ed = (ErrorData) Beans.instantiate(
    				this.getClass().getClassLoader(), "cics.sample.ErrorData");
    			} catch (Exception e) {
    				throw new ServletException("Can't create ErrorData bean: " + e);
    			}
    			ed.setMessage("Exception occurred in CICS EJB sample servlet: " + t);
    			System.out.println("Adding the error bean to the request object");
    			request.setAttribute("ed", ed);
    
    			//System.out.println("Calling the error JSP");
    			RequestDispatcher rd =
    				getServletContext().getRequestDispatcher("SampleError.jsp");
    			rd.forward(request, response); 
    
    		}
    	}
    
    

  15. Press Ctrl-S to save the code.

    Create error-handling class

    This class takes errors from the enterprise bean and sets a message string to be passed to a JSP and so to the web browser.
  16. Right click on the servlet SampleServlet and select Add -> Class to start the Create Class smartguide.
  17. Ensure that the correct project and package are shown. If necessary click on the Browse buttons to select the sample project: CICSEBJSample, and package: cics.sample.
  18. In the Class name field, enter the name for the error class. Sample: ErrorData.
  19. Ensure that the superclass field is blank.

    Create Class smartguide panel: Specify class

  20. Click Finish. The class is added to the servlet
  21. Right click on the class ErrorData and select Add -> Field to start the Create Field smartguide.
  22. Enter field name, Sample: message.
  23. Enter field type, Sample: string.
  24. Set Access Modifier to private.
  25. Check option: Access with getter and setter methods.
  26. Set setter to public.
  27. Set getter to public.

    Create Field smartguide panel: Specify field

  28. Click Finish. The field and its methods are added to the class.
The servlet is now complete.

You can see all the classes you have created for the CICS EJB Sample Application by expanding the cics.sample package in the CICSEJBSample project folder.

VAJ Workbench Projects Tab: Project list