Custom JSP taglib. Allows you to proceed GET/POST requests to the specified host (cgi-script/servlet). So you can for example obtain contents of some page and use the extracted information in your own jsp page. Taglib is very useful for mashup development for example. Let us see one example. Here is a simple JSP page
for getting SUNW (Sun Microsystems) quote from Yahoo Finance:
<html> <%@ taglib uri="taglib.tld" prefix="ask" %> <ask:HttpRequest host="http://finance.yahoo.com/q?s=SUNW" method="get" template="SUNW,*T,%1,*T,%2,*T,%3,*T,/,*T,%4,*T"> <br>Date is: <i>%1</i> <br>Quote is: <i>%2 %3 / %4</i> </ask:HttpRequest> </html> So you can request the data, parse them according your template (pattern) and use extracted results within your page. Requested data could be cached, so the next tag's usage will get data from cache instead of raising request. Tags are: HttpRequest. Body tag lets you request and parse web pages. Attributes are: 1) host - sets requested URL
Template is a text string contains commas separated elements. Template's elements are: ?T - zero or more HTML tags
Args is a text string contains commas separated elements - parameter names and their values. ifParamExists Nested tag lets you check the extracted value. If the described parameter exists tag's body will be evaluated. Attributes are: 1) num - describes a number for the parameter getValue Nested tag lets you obtain the extracted value. Attributes are: 1) num - describes a number for the parameter
Templates Let us see the above mentioned example again. Page requested from
Yahoo looks like so:
<html> ... <TD align=left noWrap><A href="http://finance.yahoo.com/q?s=SUNW&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> ... </html> Note: actual HTML code could be changed, so it is an illustration only. Our template is: SUNW, *T, %1, *T, %2, *T, %3, *T, /, *T, %4, *T
Within your JSP page you can use output parameters from the parsed request:
%1 %2 etc. They will be replaced with actual values extracted from the
requested page. It is just a text substitute. So you can use it anywhere. E.g.:
HTML fragment: <td nowrap>%1</td> JavaScript: var s='%1'; etc. Let us see more examples:
<html> <%@ taglib uri="taglib.tld" prefix="ask" %> <ask:HttpRequest host="some_host" method="get" template="%1"> <br>What we get is: <br> %1 </ask:HttpRequest> </html> Here the whole output will be inserted into your page. You may use getValue tag just for printing extracted values. E.g.:
<html> <%@ taglib uri="taglib.tld" prefix="ask" %> <ask:HttpRequest host="some_host" method="get" template="%1"> <br>What we get is: <br> <ask:getValue num="1"/> </ask:HttpRequest> </html> You may use getValue tag with parameter id. In this case tag prints nothing but
creates a variable available within the body of your tag. So you
may use extracted values in your Java scriptlets. E.g.:
<html> <%@ taglib uri="taglib.tld" prefix="ask" %> <ask:HttpRequest host="some_host" method="get" template="%1"> <br>What we get is: <br> <ask:getValue num="1" id="A"/> <% String query="UPDATE table T set column=\""+A+"\" where userId=5"; %> </ask:HttpRequest> </html> Once more:
<html> <%@ taglib uri="taglib.tld" prefix="ask" %> <ask:HttpRequest host="some_host" method="get" template="<TITLE>, %1, </TITLE>"> <br>Title of that document is: %1 </ask:HttpRequest> </html> Here we extract a title from the obtained document. Args Just a list of pairs - parameter name and the appropriate value. E.g.
suppose your requested page looks like so:
<html> <br>Login form <form method="post" action="some_host/cgi/login"> <br>Username:<input type="text" name="User"> <br>Password:<input type="password" name="Pwd"> </form> </html> With Request taglib you can proceed it so:
<html> <%@ taglib uri="taglib.tld" prefix="ask" %> <ask:HttpRequest host="some_host/cgi/login" method="post" args="User,scott,Pwd,tiger" template="%1"> <br>What we get is: %1 </ask:HttpRequest> </html> Here the value for parameter User is scott and for parameter Pwd is tiger. for downloading: Library: request.jar Description: taglib.tld See also Coldtags suite - the largest collection of custom JSP tags.
|
Also in Coldtags:
|