JSP JavaScript integration taglib ver. 1.4


    Custom JSP tag helps you implement some programming tricks in Ajax style. This taglib lets you integrate JSP and JavaScript. Right from your JavaScript code you will be able to call server-side stuff (JSP, servlet) with the same idea - update the part of your screen without the reloading the whole page.

More precisely tag  jscall  lets you create a GET HTTP request to your server-side. Technically this tag defines JavaScript function that will implement HTTP request. Because you are calling from JavaScript your server side must return JavaScript too. It could be just a chunk of JavaScript code added to your page (e.g. some set of functions you will call later) or a real JavaScript call.

For example from our JavaScript code (main page) we are going to call a JSP file that will update the label for the clicked button on the main page:
 


<%@ taglib uri="taglib.tld" prefix="j" %>

<j:jscall name="f" url="test.jsp"/>


<form>
  <input name="b1" type="button" value="Test" onClick="f('Tested')">
</form>

here tag tag jscall defines a function f. Any call for this function will produce GET request to file test.jsp. Arguments will be passed in the query string. So your test.jsp file looks so:
 


<%
  ...
  some business logic here
  ...
%>
 document.forms[0].b1.value='<%=request.getQueryString()%>';

Note: you do not need <script> brackets in this file!

JSP file performs your business logic and returns some JavaScript code. As you see JavaScript code in test.jsp changes the button (label) on the main page according to the parameter provided in the original JavaScript call:

So our JavaScript call actually executes some Java code, located in JSP file.

In the general case created JavaScript function (in our example it is f) will threat own arguments as a set of pairs: name and value in order to provide query string.

And of course our called JSP file can simply load additional JavaScript functions. E.g. in file test.jsp we can create some function
 


function ff() { ... }

and in the above mentioned example call it:
 


<%@ taglib uri="taglib.tld" prefix="j" %>

<j:jscall name="f" url="test.jsp"/>


<form>
  <input name="b1" type="button" value="Test" onClick="f();ff()">
</form>

Here the first call f() (this function has been defined in the custom JSP tag) loads new JavaScript code and the second call ff() uses this new code.

See also Linked lists description for the practical example.

Tags are:

jscall

Defines JavaScript function that will call server side element (JSP, servlet). Parameters are:

1) name Describes a name for your JavaScript function
2) url Describes an URI (path) for your JSP (servlet).
 

for downloading:

    Library: jscalltag.jar    Description: taglib.tld

 © Coldbeans      Comments?

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

Also in Coldtags: