HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Fitting the Pieces into the Enterprise Java Puzzle"
Vol. 6, Issue 8, p. 26

	


Listing 1:  TransferServlet "doGet" Method

public void doGet(HttpServletRequest req
, HttpServletResponse res) throws IOException
{
 // -- get the current http session --
 HttpSession session=req.getSession(true);


 String user=(String)
  session.getAttribute("user");


 if (user==null)
  { /* -- write error message -- */ }
 else
 {
  // -- display the transfer form
  res.setContentType("text/html");
  PrintWriter out = res.getWriter();


  displayForm(out,user,null);
 }
}



Listing 2: TransferServlet "doPost" Method

public void doPost(HttpServletRequest req
 , HttpServletResponse res) throws IOException
{
 // -- get the current http session --
 HttpSession session=req.getSession(true);
 String user=(String) session.getAttribute("user");


 if (user==null)
  { /* -- write error message -- */ }
 else
 {
  // -- handle the transfer form submission --


  res.setContentType("text/html");
  PrintWriter out = res.getWriter();


  String toEngineer=
   req.getParameter("toEngineer");


  if (toEngineer==null)
  {
   displayForm(out,user
    ,"You must specify an engineer");
  }
  else
  {
   out.println("<html>");


   out.println("<head><title>TransferServlet
    </title></head>");


   try
   {
    Context initial = new InitialContext();


    TaskManagerHome taskManagerHome =
     (TaskManagerHome) PortableRemoteObject
     .narrow(initial.lookup("TaskManager")
     ,TaskManagerHome.class);


    TaskManager taskManager=
     taskManagerHome.create();


    taskManager.transferTasks(user,toEngineer);


    out.println("Transfer to "+toEngineer
     +" succeeded. ");
   }
   catch (TransferException ex1)
   {
    out.println("Transfer to "+toEngineer
     +" failed with an application error. ");
   }
   catch (Exception ex2)
   {
    out.println("Transfer to "+toEngineer
     +" failed with a system error. ");
   }


   out.println("</body>");
   out.println("</html>");
  }
 }
}



Listing 3: TaskManager Implementation

public class TaskManagerEJB
 implements SessionBean
{
 public void ejbCreate() throws CreateException
  {}


 public void transferTasks(String fromEngineer
  , String toEngineer) throws TransferException
 {
  try
  {
   InitialContext ic = new InitialContext();


   DataSource ds = (DataSource)
    ic.lookup("java:comp/env/jdbc/LOTONtech");


   Connection con = ds.getConnection();
   Statement st=con.createStatement();


   st.executeQuery(
   "UPDATE usertasks SET username='"+toEngineer
   +"' WHERE username='"+fromEngineer+"'");


  }
  catch (Exception e) { /* handle the error */ }
 }


 public TaskManagerEJB() {}
 public void ejbRemove() {}
 public void ejbActivate() {}
 public void ejbPassivate() {}
 public void setSessionContext(SessionContext sc) {}
}


 Listing 4: Engineer Entity Bean
 
public interface Engineer extends javax.ejb.EJBObject
{
 public int getTaskCount() throws java.rmi.RemoteException;
 public String[] getTasks() throws java.rmi.RemoteException;
 public void deleteTasks() throws java.rmi.RemoteException;
 public void assignTask(String task, boolean sometimesFail)
  throws java.rmi.RemoteException;
 public String getKey() throws java.rmi.RemoteException;
}


 Listing 5: transferTasks Method with Entity Beans

public void transferTasks
 (String fromEngineerID, String toEngineerID)
 throws java.rmi.RemoteException
{
 try
 {
  EngineerHome engineerHome= (IEngineerHome)
  ctx.lookup( "EngineerHome" );


  Engineer fromEngineer=engineerHome
   .findByPrimaryKey(fromEngineerID);


  Engineer toEngineer=engineerHome
   .findByPrimaryKey(toEngineerID);


  String[] tasks=fromEngineer
   .getTasks();


  fromEngineer.deleteTasks();


  for (int i=0; i<tasks.length; i++)
  {
   // Sometimes fail this, to roll back the transaction
   toEngineer.assignTask(tasks[i], true);
  }
 }
 catch (Exception e)
 {
  System.out.println("TaskManager Exception: "+e);
 }
}

  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.