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");
s=quote.action(); quote.setTemplate("SUNW</a>,*T,%1,*T,%2,*T,%3,*T,%4,*T,%5,*T");
if (v.size()>=6)
%>
In the above mentioned example we are getting results of get
request for http://finance.yahoo.com/q?s=SUNW
Bean methods: constructor:
methods: // get/set url for request
// get/set proxy host
// get/set proxy port
// get/set query string
// get/set method ("get" or "post")
// get/set encoding
// get/set user name/password (for protected sites)
// get/set parameters for the POST request.
// another way to add POST parameters
// set/get template for response parsing
// proceed request. Method returns empty string
in case any error.
// parse string against the current template
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
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
Suppose our template is: "SUNW</a>, *T, %1 ,*T, %2, *T" So the key is: SUNW</a>
More examples: Template is "%1"
Template is "<TITLE>, %1, </TITLE>"
For custom JSP tags see Request taglib also. For downloading: requestBean.jar
|
Also in Coldtags:
|