JSP and Java beans 3 : Beans « JSP « Java






JSP and Java beans 3

/*
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<jsp:useBean id="priceFetcher" class="com.java2s.StockPriceBean" />
<html>
<head><title>Price Fetch</title></head>
<body>
<c:choose>
    <c:when test="${empty param.symbol}">
   <h2>Please submit a valid stock symbol</h2>
   <form method="POST" action ='<c:out value="${pageContext.request.contextPath}" />/priceFetch.jsp'>
   <table border="0"><tr><td valign="top">

   Stock symbol: </td>  <td valign="top"><input type="text" name="symbol" size="10"></td></tr><tr><td valign="top"><input type="submit" value="Submit Info"></td></tr></table></form>
   </c:when>
   <c:otherwise>
   <h2>Here is the latest value of <c:out value="${param.symbol}" /></h2>
       <jsp:setProperty name="priceFetcher" property="symbol" value="<%= request.getParameter(\"symbol\") %>" />
       <jsp:getProperty name="priceFetcher" property="latestPrice"/>
   </c:otherwise>
 </c:choose> 

</body>
</html>
*/
package com.java2s;  

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.net.URL;
import java.net.MalformedURLException;

import javax.swing.text.html.HTMLEditorKit.ParserCallback;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.parser.ParserDelegator;

public class StockPriceBean {

    /**  
     *   The URL base for requesting a stock price; it looks like
     *   "http://finance.yahoo.com/q?d=t&s="
     */
     private static final String urlBase =  "http://finance.yahoo.com/q?d=t&s=";
    
    /**  
     *   The character stream of HTML that is parsed for the stock price 
     *    returned by java.net.URL.openStream()
     *   
     *   see java.net.URL
     *   @see java.io.BufferedReader
     */
    private BufferedReader webPageStream = null;
     
    /**  
     *   The java.net.URL object that represents the stock Web page
     *   
     */
     private URL stockSite = null;
    
    /**  
     *   The ParserDelegator object for which ParserDelegator.parse() is
     *   called for the Web page
     *
     *   @see javax.swing.text.html.parser.ParserDelegator
     */
     private ParserDelegator htmlParser = null;
    
    /**  
     *   The MyParserCallback object (inner class); this object is an
     *   argument to the ParserDelegator.parse() method
     *
     *   @see javax.swing.text.html.HTMLEditorKit.ParserCallback
     */
     private MyParserCallback callback = null;

    /**  
     *   This String holds the HTML text as the Web page is parsed.
     *   
     *   @see MyParserCallback
     */
     private String htmlText = "";
   private String symbol = "";
     private float stockVal = 0f;

  //A JavaBean has to have a no-args constructor (we explicitly show this 
  //constructor as a reminder; the compiler would have generated a default
  //constructor with no arguments automatically
  public StockPriceBean() {}
  
  //Setter or mutator method for the stock symbol
  public void setSymbol(String symbol){
  
      this.symbol = symbol;
  }
   
  class MyParserCallback extends ParserCallback {

      //bread crumbs that lead us to the stock price
      private boolean lastTradeFlag = false; 
      private boolean boldFlag = false;
  
    public MyParserCallback(){
    
      //Reset the enclosing class' instance variable
    if (stockVal != 0)
          stockVal = 0f;
    
   }
        
    public void handleStartTag(javax.swing.text.html.HTML.Tag t,
      MutableAttributeSet a,int pos) {
        
        if (lastTradeFlag && (t == javax.swing.text.html.HTML.Tag.B )){
            
            boldFlag = true;
       }
        
    }//handleStartTag

    public void handleText(char[] data,int pos){
              
        htmlText  = new String(data);
    
    //System.out.println(htmlText);
      
        if (htmlText.indexOf("No such ticker symbol.") != -1){
             
          throw new IllegalStateException(
      "Invalid ticker symbol in handleText() method.");
                
        }  else if (htmlText.equals("Last Trade:")){
                    
            lastTradeFlag = true;
                    
        } else if (boldFlag){
                
            try{
                
                stockVal = new Float(htmlText).floatValue();

            } catch (NumberFormatException ne) {
                    
                try{
                        
                    // tease out any commas in the number using 
                    //NumberFormat
                        
                    java.text.NumberFormat nf = java.text.NumberFormat.
                      getInstance();
                    
                    Double f = (Double) nf.parse(htmlText);
                    
                    stockVal =  (float) f.doubleValue();
                     
                } catch (java.text.ParseException pe){
                        
                     throw new IllegalStateException(
                "The extracted text " + htmlText +
                         " cannot be parsed as a number!");
                        
                 }//try
            }//try
            
            lastTradeFlag = false;
            boldFlag = false;
      
         }//if
                
      } //handleText

  }//MyParserCallback

  public float getLatestPrice() throws IOException,MalformedURLException {

      stockSite = new URL(urlBase + symbol);
       
      webPageStream = new BufferedReader(new InputStreamReader(stockSite.
       openStream()));
     
      htmlParser = new ParserDelegator();
     
      callback = new MyParserCallback();//ParserCallback
     
      synchronized(htmlParser){  
  
          htmlParser.parse(webPageStream,callback,true);

       }//synchronized
     
    //reset symbol
    symbol = "";

     return stockVal;

  }//getLatestPrice

}//StockPriceBean
           
       








Related examples in the same category

1.Calling a Private Method
2.Jsp Form And Bean
3.Get Set Properties JSTL
4.Getting a Property Value
5.Using a Constructor
6.Using Bean Counter JSP
7.Using a Java Bean Jsp
8.Using Package Jsp
9.Using UseBean in Jsp
10.Set Property Value
11.Jsp Using Bean Scope Session
12.Bean property display
13.Beans with scriptlet
14.EL and Complex JavaBeans
15.JSP with Java bean
16.JSP form and Java beans
17.JSP email valid check
18.JSP Standard Actions: set property
19.JSP and Java beans (JavaBeans) 1JSP and Java beans (JavaBeans) 1
20.JSP and Java beans (JavaBeans) 2JSP and Java beans (JavaBeans) 2