JSP and Java beans 3 : Beans « JSP « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » JSP » BeansScreenshots 
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 = (Doublenf.parse(htmlText);
                    
                    stockVal =  (floatf.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
w_w_w_.__ja_v___a_2s__.c_o__m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.