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
 

"Seperating Presentation from Business Logic"
Vol 5 Issue 7, p.22

	

Listing 1 : 
  
public class LoginServlet extends HttpServlet { 

 public void doGet (HttpServletRequest req, HttpServletResponse res) 
      throws ServletException, IOException 

{ 
String   inputsurname = req.getParameter("surname"); 
String   inputfirstname = req.getParameter("firstname"); 
String  hostToConnectTo = req.getParameter("host"); 
String  tmpPort = req.getParameter("port"); 
int  portToConnectTo = Integer.valueOf(tmpPort).intValue();  . 
. 
  
  
  
  

Appendix A  Sample Demo Code 

Servlet Code Example - LoginServlet.java 

/* 
 * LoginServlet.java  7/27/99 
 * Barry Tait 
 * 
 * Processes the client request by logging into Accounting application - ACCT 
 * and retrieving client infomation. ACCT takes a surname and firstname as input 
 * from the client. 
 * 
 * It then logs into a Stockbroking application - DMOI, using the account 
 * number retrieved from ACCT. It retrieves portfolio details from DMOI then 
 * calls a JavaServer Page (JSP) which creates the response. 
 * 
 */ 

import java.io.*; 
import java.util.Vector; 
import javax.servlet.*; 
import javax.servlet.http.*; 
import peerlogic.clientapis.emulator.*; 
import acct.*; 
import dmoi.*; 

public class LoginServlet extends HttpServlet { 

    public void doGet (HttpServletRequest req, HttpServletResponse res) 
      throws ServletException, IOException 

    { 
 String  inputsurname = req.getParameter("surname"); 
 String  inputfirstname = req.getParameter("firstname"); 
 String hostToConnectTo = req.getParameter("host"); 
 String tmpPort = req.getParameter("port"); 
 int portToConnectTo = Integer.valueOf(tmpPort).intValue(); 

 // 
 // Create session object 
 // 
 HttpSession session = req.getSession(true); 

 // 
 // Put variables into session 
 // 
 session.putValue("host", hostToConnectTo); 
 session.putValue("port", tmpPort); 

 // 
 // Instantiate New PeerLogic PATH3270 Beans 
 // 
 KixEmulator newEmulator = new KixEmulator(); 
 KixEmulator newEmulator2 = new KixEmulator(); 
   AcctDetails getAccountDetails = new AcctDetails(); 
 DmoiLogin getDmoiDetails = new DmoiLogin(); 

 newEmulator.setTN3270Port(portToConnectTo); 
   newEmulator.setTN3270Host(hostToConnectTo); 
 newEmulator2.setTN3270Port(portToConnectTo); 
   newEmulator2.setTN3270Host(hostToConnectTo); 

// 
 // Assign terminal Bean to each data access Bean 
 // 
 getAccountDetails.setKixEmulator(newEmulator); 
 getDmoiDetails.setKixEmulator(newEmulator2); 
 // 
 // Set variables from form 
 // 
 getAccountDetails.setSurname(inputsurname); 
 getAccountDetails.setFirstName(inputfirstname); 

  // 
 // Login to system and execute the "acct" transaction. 
 // As input enter a surname and firstname. Capture details if user 
 // exists and display JSP. If there's an error display loginError.jsp. 
 // 
  
 try { 
  getAccountDetails.performWork(); 

  // 
  // Put AcctDetails bean - getAccountDetails, into session so 
// that the JSP can access it. This bean will contain the retrieved client 
// information from the accounting application - ACCT. 
  // 
  session.putValue("resultsBean", getAccountDetails); 

  // 
// Get Account No. (acctID) from getAccountDetails Bean - this is used to 
// drive the DmoiLogin Bean - getDmoiDetails 
  // 
  String acctID = getAccountDetails.getDetails_acctID(); 

  // 
  // Use the acctID for login to the DMOI application 
  // 
  getDmoiDetails.setUserID(acctID); 

  // 
  // Login to DMOI and add the portfolio details to the session 
  // 

         try { 
          getDmoiDetails.performWork(); 

   // 
   // Put getDmoiDetails Bean into session 
   // 
   session.putValue("dmoiResultsBean", getDmoiDetails); 
  
         } 

         catch ( IllegalStateException e ) { 
  
         } 
  

  // 
    // Call the JSP - login.jsp for display 
  // 
  getServletContext().getRequestDispatcher("/jspDemo/login.jsp").forward 
  (req, res); 

 } 

 catch ( IllegalStateException e ) { 
  
// 
  // Call JSP error page if user details are not found 
  // 
getServletContext().getRequestDispatcher("/jspDemo/loginError.jsp"). 
forward(req, res); 
 } 
  
  } 
} 

 JSP Code Example - Login.jsp 

<HTML> 
<HEAD> 
<TITLE>UT Screen Bean Servlet/JSP test page</TITLE> 

<!- Include the JavaScript files needed for client-side validation --> 
<%@ include file="/javascript/checkBuyFields.js" %> 
<%@ include file="/javascript/checkSellFields.js" %> 

</HEAD> 
<BODY> 
<P><font size=1 face="verdana, arial" color=red> Display Page:</font>  <font size=1 face="verdana, arial">Type=JSP name=login.jsp</font> 
</P> 

<!- Import Java Vector class  needed for formatting of table --> 
<%@ page import="java.util.Vector" %> 

<!-- Retrieve Beans from session --> 
<jsp:useBean id="resultsBean" scope="session" class="acct.AcctDetails" /> 
<jsp:setProperty name="resultsBean" property="*"/> 
<jsp:useBean id="dmoiResultsBean" scope="session" class="dmoi.DmoiLogin" /> 
<jsp:setProperty name="dmoiResultsBean" property="*"/> 

<CENTER> 

<!-- Main HTML body --> 
<TABLE WIDTH=670 BORDER=0 CELLPADDING=4 CELLSPACING=4> 
<TR> 
<TD WIDTH=400 ALIGN=left VALIGN=top> 

<P> 
<font size=2 face=verdana> 
<B>Account Details: 
<font color=red> 

<!-- Retrieve data from Bean methods --> 
<%= resultsBean.getDetails_Title() %> 
<%= resultsBean.getDetails_FirstName() %> 
<%= resultsBean.getDetails_Initial() %> 
<%= resultsBean.getDetails_Surname()%> 
</font></B> 
</font> 
</P> 
<BR> 

<!-- Acct details table --> 
<TABLE WIDTH=400 BORDER=1 CELLPADDING=4 CELLSPACING=2> 
<TR> 
<TD BGCOLOR="#eeeeee" WIDTH=120 ALIGN=left> 
<font face="verdana, arial" size=2><b>Account No.:</b></font></TD> 
<TD WIDTH=280 ALIGN=left><font face="verdana, arial" size=2> 
<%= resultsBean.getDetails_acctID() %> 
</font></TD></TR> 

<TR><TD BGCOLOR="#eeeeee" WIDTH=120 ALIGN=left> 
<font face="verdana, arial" size=2><b>Address:</b></font></TD> 
<TD WIDTH=280 ALIGN=left><font face="verdana, arial" size=2> 
<%= resultsBean.getDetails_Address() %> 
</font></TD></TR> 

<TR><TD BGCOLOR="#eeeeee" WIDTH=120 ALIGN=left> 
<font face="verdana, arial" size=2><b>Account Limit:</b></font></TD> 
<TD WIDTH=280 ALIGN=left><font face="verdana, arial" size=2> 
<%= resultsBean.getDetails_Limit() %> 
</font></TD></TR> 
</TABLE> 

<BR> 
<BR> 

<P><font size=2 face=verdana><B>Current Portfolio:<font color=red> 
$ <% if (dmoiResultsBean.getTotalValue() != null) { %> 
  <%= dmoiResultsBean.getTotalValue() %> 
   </font></B></font></P> 
  
   <TABLE WIDTH=400 BORDER=1 CELLPADDING=4 CELLSPACING=2> 
   <TR> 
 <TD BGCOLOR="#eeeeee" WIDTH=100 ALIGN=center> 
 <font face="verdana, arial" size=2><b>Stock</b></font></TD> 
 <TD BGCOLOR="#eeeeee" WIDTH=100 ALIGN=center> 
 <font face="verdana, arial" size=2><b>Volume</b></font></TD> 
 <TD BGCOLOR="#eeeeee" WIDTH=100 ALIGN=center> 
 <font face="verdana, arial" size=2><b>Price</b></font></TD> 
 <TD BGCOLOR="#eeeeee" WIDTH=100 ALIGN=center> 
 <font face="verdana, arial" size=2><b>Total</b></font></TD> 
   </TR> 
  
   <% 
   Vector stocks = dmoiResultsBean.getV_stock(); 
   Vector holdings = dmoiResultsBean.getV_holding(); 
   Vector prices = dmoiResultsBean.getV_price(); 
   Vector values = dmoiResultsBean.getV_value(); 

   for (int i = 0; i < stocks.size(); i++) { %> 
  
    <!-- Test for end of array --> 
  
    <% if (((String)stocks.elementAt(i)).substring(0,1).equals(" ")) {%> 
  <TR> 
 <TD WIDTH=100 ALIGN=center> 
 <% String stockName = ((String)stocks.elementAt(i)); %> 
<font face="verdana, arial" size=2><%= stockName %></font></TD> 
 <TD WIDTH=100 ALIGN=center> 
 <% String holdingAmount = ((String)holdings.elementAt(i)); %> 
 <font face="verdana, arial" size=2><%= holdingAmount %></font></TD> 
 <TD WIDTH=100 ALIGN=center> 
 <% String priceAmount = ((String)prices.elementAt(i)); %> 
 <font face="verdana, arial" size=2><%= priceAmount %></font></TD> 
 <TD WIDTH=100 ALIGN=center> 
 <% String valueAmount = ((String)values.elementAt(i)); %> 
 <font face="verdana, arial" size=2><%= valueAmount %></font></TD> 
 </TR> 
   <% } %> 
  <% } %> 
  </TABLE> 

<% } 

else { 
 out.println("0"); 
} %> 

</TD> 
<TD WIDTH=90> </TD> 

<!-- Buy and Sell Tables --> 
<TD WIDTH=180 ALIGN=center VALIGN=top> 
<P><font size=2 face="verdana"><B>Transaction Options:</B></font></P> 

<TABLE WIDTH=180 BORDER=1 CELLPADDING=2 CELLSPACING=2><TR> 
<TD BGCOLOR=black ALIGN=center> 
<font face="verdana, arial" size=2 color=white><b>Buy Stock</b></font></TD></TR> 
<TR><TD ALIGN=center> 
<FORM Action="/servlet/BuyServlet" onSubmit="return checkBuyFields(this)" METHOD=GET> 
<font face="verdana, arial" size=2> 
<SELECT NAME="buyStock" SIZE="1"> 
<OPTION Value="" SELECTED>Please Select</OPTION> 
<OPTION Value=stock1 SELECTED>Stock 1</OPTION> 
<OPTION Value=stock2 SELECTED>Stock 2</OPTION> 
<OPTION Value=stock3 SELECTED>Stock 3</OPTION> 
<OPTION Value=stock4 SELECTED>Stock 4</OPTION> 
<OPTION Value=stock5 SELECTED>Stock 5</OPTION> 
<OPTION Value=stock6 SELECTED>Stock 6</OPTION> 
<OPTION Value=stock7 SELECTED>Stock 7</OPTION> 
<OPTION Value=stock8 SELECTED>Stock 8</OPTION> 
<OPTION Value=stock9 SELECTED>Stock 9</OPTION> 
</SELECT> 
</font> 
</TD></TR> 
<TR><TD ALIGN=center> 
<font face="verdana, arial" size=2>Volume:</font> 
<INPUT NAME="buyVolume" SIZE="5"></TD></TR> 
<TR><TD BGCOLOR="#eeeeee" ALIGN="center"><INPUT Type="submit" Value="  Submit  "> 
</TD></TR></TABLE> 
</FORM> 

<BR> 

<TABLE WIDTH=180 BORDER=1 CELLPADDING=2 CELLSPACING=2><TR> 
<TD BGCOLOR=black ALIGN=center> 
<font face="verdana, arial" size=2 color=white><b>Sell Stock</b></font></TD></TR> 
<TR><TD ALIGN=center> 
<FORM Action="/servlet/SellServlet" onSubmit="return checkSellFields(this)" METHOD=GET> 
<font face="verdana, arial" size=2> 
<SELECT NAME="sellStock" SIZE="1"> 
<OPTION Value="" SELECTED>Please Select</OPTION> 
<OPTION Value=stock1 SELECTED>Stock 1</OPTION> 
<OPTION Value=stock2 SELECTED>Stock 2</OPTION> 
<OPTION Value=stock3 SELECTED>Stock 3</OPTION> 
<OPTION Value=stock4 SELECTED>Stock 4</OPTION> 
<OPTION Value=stock5 SELECTED>Stock 5</OPTION> 
<OPTION Value=stock6 SELECTED>Stock 6</OPTION> 
<OPTION Value=stock7 SELECTED>Stock 7</OPTION> 
<OPTION Value=stock8 SELECTED>Stock 8</OPTION> 
<OPTION Value=stock9 SELECTED>Stock 9</OPTION> 
</SELECT> 
</font> 
</TD></TR> 
<TR><TD ALIGN=center> 
<font face="verdana, arial" size=2>Volume:</font> 
<INPUT NAME="sellVolume" SIZE="5"></TD></TR> 
</TD></TR> 
<TR><TD BGCOLOR="#eeeeee" ALIGN=center><INPUT Type="submit" Value="  Submit  "> 
</TD> 
</TR> 
</TABLE> 
</FORM> 
<BR> 

<!-- close main table --> 
</TD> 
</TR> 
</TABLE> 
<P ALIGN=center> 
<FORM Action="/servlet/LogoffServlet" METHOD=GET> 
<INPUT Type="hidden" name="logoff" value="true"> 
<INPUT Type="submit" Value="     Click here to Logoff    "> 
</FORM> 
</P> 
</CENTER> 
</BODY> 
</HTML> 
  
  
 
 

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.