HttpRequest taglib ver. 2.1

    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
2) method - sets type of the request (post or get). Default value is get.
3) template - sets parsing specifications
4) args - Optional parameter. Sets parameters for post request.
5) name - Optional parameter: user name (for protected sites)
6) password - Optional parameter: password (for protected sites)
7) ttl - Optional parameter: timeout for cache (in seconds). Default value is 0 (no cache)
8) scope - Optional parameter: scope for cache. Possible values are application or session. Default value is application (cache will be used if you set ttl parameter).
9) timeout Optional parameter. Describes a timeout for your request (in milliseconds).
10) proxyHost Optional parameter. Describes proxy settings for your system.
11) proxyPort Optional parameter. Describes proxy settings for your system.
12) encoding Optional parameter. Describes an encoding for the requested data.

Template is a text string contains commas 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

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
2) id - Optional parameter: variable name. This page scope variable will be visible in the body of your tag.

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&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>
    ...
</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
So our parser detects SUNW, skips HTML tags (</a></TD><TD align=middle noWrap>), associates the first parameter (%1) with Feb 11, skips HTML tags  (</TD> <TD noWrap><B>) etc.

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

 © Coldbeans      Comments?

See also Coldtags suite - the largest collection of custom JSP tags.

Also in Coldtags: