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. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.