Request bean ver. 1.7

Java bean lets you proceed GET/POST  requests to the specified host (cgi-script/servlet). So you can obtain for example content of some page and use extracted information in your own jsp page.

Let us see some examples. Here is a simple JSP page for getting SUNW (Sun Microsystems) quote from Yahoo Finance:
 


<html>
<br><b>Quote page</b>
<%@ page language="java" import="com.cj.request.Request" %>
<jsp:useBean id="quote" class="Request" scope="page" />
<%
        String s="";
        java.util.Vector v;

        quote.setHost("http://finance.yahoo.com/q?s=SUNW");
        quote.setMethod("get");

        s=quote.action();

        quote.setTemplate("SUNW</a>,*T,%1,*T,%2,*T,%3,*T,%4,*T,%5,*T");
        v=quote.parse(s);

        if (v.size()>=6)
          s=v.elementAt(2)+" "+v.elementAt(3)+v.elementAt(4)+v.elementAt(5);
        else
          s="can not get quote";

%>
<br>Quote for SUNW:&nbsp;<%= s %>
</html>

In the above mentioned example we are getting results of get request for http://finance.yahoo.com/q?s=SUNW
Bean contains also a simple parser lets you extract the specified fields from the response.

Bean methods:

constructor:
    Request ()

methods:

    // get/set url for request
    public void setHost(String url)
    public String getHost()

    // get/set proxy host
    public void setProxyHost(String url)
    public String getProxyHost()

    // get/set proxy port
    public void setProxyPort(int port)
    public int getProxyPort()

    // get/set query string
    public void setQuery(String query)
    public String getQuery()

    // get/set method ("get" or "post")
    public void setMethod(String method)
    public String getMethod()

    // get/set encoding
    public void setEncoding(String encoding)
    public String getEncoding()

    // get/set user name/password (for protected sites)
    public void setName(String user_name)
    public String getName()

    public void setPassword(String password)
    public String getPassword()

    // get/set parameters for the POST request.
    // It is a Vector contains pairs: parameter name/value
    public void setArgs(Vector params)
    public Vector getArgs()

    // another way to add POST parameters
    public void addPair(String parameter, String value)

    // set/get template for response parsing
    public void setTemplate(String template)
    public String getTemplate()
 

    // proceed request. Method returns empty string in case any error.
    public String action()

    // parse string against the current template
    public Vector parse(String s)

    Request bean contains also a simple parser. You can set template describes key positions and parse string (response) against this template. Template is a text string contains comma separated elements. Template's elements are:

?T - zero or more HTML tags
*T - one or more HTML tags
%number - set output parameter (number is just a positive integer: %1, %2 etc.)
any_sequence_of_characters - match node

    Bean's method parse returns a Vector contains parameters described as %parameter_number.

    Let us see one example that will clarify this possibly puzzle-like description. It is a fragment of HTML page from Yahoo Finance (the above mentioned example):
 

    <TD align=left noWrap><A
      href="http://finance.yahoo.com/q?s=SUNW&amp;d=t">SUNW</a></TD>
    <TD align=middle noWrap>Feb 11</TD>
    <TD noWrap><B>94 <SUP>7</SUP>/<SUB>16</SUB></B></TD>
    <TD noWrap><FONT color=#ff0020>-<SUP>3</SUP>/<SUB>16</SUB></FONT></TD>
    <TD noWrap><FONT color=#ff0020>-0.20%</FONT></TD>
    <TD noWrap>13,126,800</TD>

Suppose our template is: "SUNW</a>, *T, %1 ,*T, %2, *T"

So the key is: SUNW</a>
After this key we can have several html tags. In our fragment they are: </TD> <TD align=middle noWrap>
As a first parameter our parsing will return: Feb 11
After this parameter we can have again several tags.
The second parameter will match to: 94

More examples:

Template is "%1"
The result of our parsing (a first parameter) is the whole content of the requested document.

Template is "<TITLE>, %1, </TITLE>"
The result of our parsing (a first parameter) is a title from the obtained document.

For custom JSP tags see Request taglib also.

 For downloading: requestBean.jar

 © Coldbeans     Comments?

Also in Coldtags: