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 Jigsaw, by Tony Loton"
Vol. 6, Issue 5, p. 72

	


Listing 1: Writing User Options as Applet Parameters
 
//-- write the options out as an applet -- 

out.println( 
 "<applet code=com.lotontech.applets.OptionsApplet 
  height=30, width=600>"); 

out.println("<param name=user value="+user+"></param>"); 

for (int opNum=0; opNum<options.size(); opNum++) 
{ 
  String thisOption=(String) options.elementAt(opNum); 

  out.println("<param name=option"+(opNum+1) 
   +" value="+thisOption+"></param>"); 
} 

out.println("</applet>"); 
  

Listing 2: Options Applet Java Code
 
public class OptionsApplet extends Applet 
 implements ActionListener 
{ 
 public void init() 
 { 
  // -- get the user from parameter -- 
  String user=getParameter("user"); 

  // -- GUI would be initialized here -- 

  int opNum=0; 
  boolean finished=false; 
  while (!finished) 
  { 
   opNum++; 

   String thisOption=getParameter 
    ("option"+opNum); 

   if (thisOption==null) finished=true; 
   else 
   { 
    Button newButton=new Button(thisOption); 
    this.add(newButton); 
    newButton.addActionListener(this); 
   } 

  } 

 } 

 // -- ActionListener Method -- 
 public void actionPerformed(ActionEvent event) 
 { 

  if (event.getSource() instanceof Button) 
  { 
   Button sourceButton=(Button)event.getSource(); 

   // -- tell browser to launch selected app. -- 

   AppletContext theBrowser 
    =this.getAppletContext(); 

   try 
   { 
     URL documentBase=getCodeBase(); 

     URL newURL=new 
      URL(documentBase,sourceButton.getLabel()); 

     theBrowser.showDocument(newURL,"main"); 
   } 
   catch (Exception ex) {ex.printStackTrace();} 
  } 

 } 

} 

Listing 3: TaskServlet
 
public class TaskServlet extends HttpServlet 
{ 
 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"); 

  // -- check for null user omitted - 

  String getdata=req.getParameter("getdata"); 
  if (getdata==null) getdata="false"; 

  if (!getdata.equals("true")) 
  { 
   // -- download HTML with task applet tag 
  } 
  else 
  { 
   // --task applet calling me again for data -- 

   Vector tasks=new Vector(); 

   // -- get connection from datasource -- 
   InitialContext ic = new InitialContext(); 

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

   Connection con = ds.getConnection(); 

   // -- select user tasks from the database -- 
   Statement st=con.createStatement(); 

   ResultSet results=st.executeQuery( 
    "SELECT username, task , priority 
    FROM usertasks WHERE username='"+user+"'"); 

   while (results.next()) 
   { 
    Task thisTask=new Task(results.getString(2) 
     , results.getString(3)); 

    tasks.addElement(thisTask); 
   } 

   // -- return serialized vector to the applet -- 
   ObjectOutputStream taskStream = new 
    ObjectOutputStream(res.getOutputStream()); 

   taskStream.writeObject(tasks); 
  } 
 } 
} 
  

Listing 4: TaskApplet
 
public class TaskApplet extends Applet 
{ 
 public void init() 
 { 
  // -- get the user from parameter -- 
  String user=getParameter("user"); 

  // -- set up the GUI Lists -- 

  this.setBackground(new Color(0,0,128)); 
  setLayout(new GridLayout(1,2)); 

  java.awt.List highList=new java.awt.List(); 
  highList.add("HIGH PRIORITY"); 
  highList.add(""); 
  add(highList); 

  java.awt.List lowList=new java.awt.List(); 
  lowList.add("LOW PRIORITY"); 
  lowList.add(""); 
  add(lowList); 

  try 
  { 
   // -- get user tasks from task servlet -- 

   URL servletURL=new URL(this.getCodeBase() 
    ,"getTasks?getdata=true"); 

   ObjectInputStream taskStream= 
    new ObjectInputStream(servletURL.openStream()); 

   Vector tasks=(Vector) taskStream.readObject(); 

   // -- and display them in 2 lists -- 

   for (int taskNum=0; taskNum<tasks.size(); 
   taskNum++) 
   { 
    Task thisTask=(Task) tasks.elementAt(taskNum); 

    if (thisTask.priority.equals("high")) 
     highList.add(thisTask.taskName); 
    else if (thisTask.priority.equals("low")) 
     lowList.add(thisTask.taskName); 
   } 

  } 
  catch (Exception ex) 
  { 
   // -- send error to Java Console -- 
   ex.printStackTrace(); 
  } 

 } 
} 

  
 
 

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.