EJB CICS sample application task guide: Development
sga030
The tasks involved in creating the servlet are:
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.
/**
* Service method.
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
response.sendRedirect("/cicssample/index.html");
}
/**
* 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);
}
}
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.