⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch02.html

📁 java2高级编程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<P CLASS="Body"><A NAME="pgfId-1087807"></A>The <EM CLASS="CODE">findAllMatchingItems</EM> method is a finder method, and container implementations vary in how they handle calls to <EM CLASS="CODE">finder</EM> methods. <EM CLASS="A">BEA Weblogic</EM> containers look in the bean's <A NAME="marker-1087808"></A><A NAME="marker-1087809"></A>deployment descriptor for information on a bean's <EM CLASS="CODE">finder</EM> methods. In the case of the search, the deployment descriptor maps the search string passed to <EM CLASS="CODE">AuctionItemHome.findAllMatchingItems</EM><A NAME="marker-1087810"></A> to the <EM CLASS="CODE">summary</EM> field in the underlying <EM CLASS="CODE">AuctionItems</EM> database table. This tells the Enterprise JavaBeans server to retrieve data for all auction items with a summary field that contains text that matches the search string. </P></DIV></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087811"></A><EM CLASS="B-code">AuctionServlet.searchItems</EM></H5><P CLASS="Body"><A NAME="pgfId-1087814"></A>The <A NAME="marker-1087812"></A><A NAME="marker-1087813"></A>searchItems method retrieves the text string from the browser, creates an <EM CLASS="CODE">HTML</EM> page to display the search results, and passes the search string to the <EM CLASS="CODE">BidderBean.getMatchingItemsList</EM><A NAME="marker-1087815"></A> method. <EM CLASS="CODE">BidderBean</EM> is a session bean that retrieves lists of auction items and checks the user ID and password for end users seeking to bid on auction items. The search results are returned to this method in an <EM CLASS="CODE">Enumeration</EM> variable. </P><PRE CLASS="CODE"><A NAME="pgfId-1087816"></A>private void searchItems(ServletOutputStream out, HttpServletRequest request)</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087817"></A>                          throws IOException {//Retrieve search string</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087818"></A>  String searchString=request.getParameter(&quot;searchString&quot;);//Create HTML page</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087819"></A>  String text = &quot;Click Item number for description and to place bid.&quot;;  setTitle(out, &quot;Search Results&quot;);  try {     addLine(&quot;&lt;BR&gt;&quot;+text, out);//Look up home interface for BidderBean     BidderHome bhome=(BidderHome) ctx.lookup(&quot;bidder&quot;);//Create remote interface for BidderBean Bidder bid=bhome.create();//Pass search string to BidderBean method </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087820"></A>     Enumeration enum=(Enumeration) bid.getMatchingItemsList(searchString);        if(enum != null) {          displayitems(enum, out);          addLine(&quot;&quot;, out);        }  } catch (Exception e) {    addLine(&quot;AuctionServlet Search Items error&quot;, out);    System.out.println(&quot;AuctionServlet &lt;newlist&gt;: &quot;+e);  }    out.flush();}</PRE></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087821"></A><EM CLASS="B-code">BidderBean.getMatchingItemsList</EM></H5><P CLASS="Body"><A NAME="pgfId-1087824"></A>The <EM CLASS="CODE">BidderBean.getMatchingItemsList</EM><A NAME="marker-1087822"></A> method calls the <EM CLASS="CODE">AuctionItemHome.findAllMatchingItems</EM><A NAME="marker-1087823"></A> method and passes it the search string. <EM CLASS="CODE">AuctionItemBean</EM> is an entity bean that handles auction item updates and retrievals. The search results return an <EM CLASS="CODE">Enumeration</EM>. </P><PRE CLASS="CODE"><A NAME="pgfId-1087825"></A>public Enumeration getMatchingItemsList(String searchString)                                        throws RemoteException {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087826"></A>  Enumeration enum=null;  try{//Create Home interface for AuctionItemBean    AuctionItemHome home = (AuctionItemHome) ctx.lookup(&quot;auctionitems&quot;);//Pass search string to Home interface method    enum=(Enumeration)home.findAllMatchingItems(searchString);  }catch (Exception e) {    System.out.println(&quot;getMatchingItemList: &quot;+e);    return null;  }  return enum;}</PRE></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087827"></A><EM CLASS="B-code">AuctionItemHome.findAllMatchingItems</EM></H5><P CLASS="Body"><A NAME="pgfId-1087828"></A>The <EM CLASS="CODE">AuctionItemHome.findAllMatchingItems</EM> method is not implemented in <EM CLASS="CODE">AuctionItemBean</EM>. Instead, the <EM CLASS="CODE">AuctionItemBean finder</EM>-method implementations are defined in the <EM CLASS="CODE">AuctionItemBean</EM> <A NAME="marker-1087829"></A>deployment descriptor when <EM CLASS="A">BEA Weblogic</EM> containers are used. </P><P CLASS="Body"><A NAME="pgfId-1087830"></A>When the bean is using these containers, even if the bean has finder-method implementations, they are ignored, and the deployment-descriptor settings are consulted instead. </P><PRE CLASS="CODE"><A NAME="pgfId-1087831"></A>//Declare method in Home interface  public Enumeration findAllMatchingItems(String searchString) </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087832"></A>                         throws FinderException, RemoteException;</PRE></DIV><DIV><H5 CLASS="B"><A NAME="pgfId-1087833"></A><EM CLASS="B-code">AuctionItemBean</EM> Deployment Descriptor</H5><P CLASS="Body"><A NAME="pgfId-1087835"></A><A NAME="marker-1087834"></A>When a bean's finder method is called, the container consults the deployment descriptor for that bean to find out what data the finder method needs to retrieve from the underlying database table. The container passes this information to the Enterprise JavaBeans server, which does the actual retrieval. </P><P CLASS="Body"><A NAME="pgfId-1087838"></A>The <A NAME="marker-1087836"></A><A NAME="marker-1087837"></A>deployment descriptor for <EM CLASS="CODE">AuctionItemBean</EM> provides <EM CLASS="CODE">finderDescriptors</EM> for all finder methods declared in the <EM CLASS="CODE">AuctionItemHome</EM> interface. The <EM CLASS="CODE">finderDescriptor</EM> for the <EM CLASS="CODE">findAllMatchingItems</EM> method maps the search string to the summary field in the underlying AuctionItems database table. This tells the Enterprise JavaBeans server to retrieve the data for all table rows with a summary field that matches the text in the search string. </P><PRE CLASS="CODE"><A NAME="pgfId-1087839"></A>(finderDescriptors  &quot;findAllItems()&quot;         &quot;(= 1 1)&quot;  &quot;findAllNewItems(java.sql.Date newtoday)&quot; &quot;(= startdate $newtoday)&quot;  &quot;findAllClosedItems(java.sql.Date closedtoday)&quot; &quot;(= enddate $closedtoday)&quot;  &quot;findAllMatchingItems(String searchString)&quot; &quot;(like summary $searchString)&quot;); end finderDescriptors</PRE></DIV></DIV><DIV><H4 CLASS="A"><A NAME="pgfId-1087841"></A><EM CLASS="A-code">AuctionItemBean</EM><A NAME="11998"></A></H4><PRE CLASS="CODE"><A NAME="pgfId-1087844"></A><A NAME="marker-1087842"></A><A NAME="marker-1087843"></A>package auction;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087845"></A>&nbsp;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087846"></A>import java.rmi.RemoteException;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087847"></A>import javax.ejb.*;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087848"></A>import java.util.*;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087849"></A>import java.text.NumberFormat;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087850"></A>&nbsp;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087851"></A>//Implementation of the auction bean on the server sidepublic class AuctionItemBean implements EntityBean {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087852"></A>  //one line summary of auction item</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087853"></A>  public String summary;     //username of person selling the item</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087854"></A>  public String seller;  </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087855"></A>  //auction item identification number</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087856"></A>  public int id;   //auction item description</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087857"></A>  public String description;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087858"></A>  //initial price of auction item</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087859"></A>  public double startprice=0;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087860"></A>  //Date auction started</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087861"></A>  public java.sql.Date startdate;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087862"></A>  //Date auction finishes</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087863"></A>  public java.sql.Date enddate;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087864"></A>  //current maximum bid amount and bid increment</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087865"></A>  public double maxbid,increment;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087866"></A>  //number of bids</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087867"></A>  public int bidcount;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087868"></A>  //username of highest bidder</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087869"></A>  public String highbidder;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087870"></A>  protected transient EntityContext ctx;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087871"></A>  //next available auction number</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087872"></A>  public static int counter=400000;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087873"></A>&nbsp;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087874"></A>  public String getDescription() throws RemoteException { </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087875"></A>    return description; </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087876"></A>  }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087877"></A>  public String getSummary() throws RemoteException { </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087878"></A>    return summary; </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087879"></A>  }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087880"></A>  public int getId() throws RemoteException { </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087881"></A>    return id; </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087882"></A>  }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087883"></A>  public String getSeller() throws RemoteException { </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087884"></A>    return seller; </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087885"></A>  }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087886"></A>  public double getIncrement() throws RemoteException {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087887"></A>    return increment;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087888"></A>  }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087889"></A>  public double getHighBid() throws RemoteException {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087890"></A>    return maxbid;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087891"></A>  }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087892"></A>  public int getBidCount() throws RemoteException {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087893"></A>    return bidcount;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087894"></A>  }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087895"></A>  public String getHighBidder() throws RemoteException {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087896"></A>    return highbidder;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087897"></A>  }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087898"></A>  public Date getStartDate() throws RemoteException {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087899"></A>    return startdate;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087900"></A>  }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087901"></A>  public Date getEndDate() throws RemoteException {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087902"></A>    return enddate;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087903"></A>  }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087904"></A>  public double getStartPrice() throws RemoteException {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087905"></A>    return startprice;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087906"></A>  }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087907"></A>  public boolean isAuctionStillRunning() {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087908"></A>    Date now = (Calendar.getInstance()).getTime();</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087909"></A>    if(enddate.getTime()&gt;=now.getTime()) {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087910"></A>      return true;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087911"></A>    } else {</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087912"></A>      return false;</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087913"></A>    }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087914"></A>  }</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087915"></A>  public int setHighBid(String buyer, double amount, double increment) </PRE><PR

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -