PDA

View Full Version : AJAX call function



melvinoyh
05-30-2006, 06:12 AM
i have all my functions in a java class file....how do i call the function using ajax? does anyone know?

izzit possible to call the java function from the class file by ajax?
plz let me know if u know...
quite urgent...thx very much..

djr33
05-30-2006, 08:33 AM
link?
why?
more info?


I mean.... I don't know myself, but it's a pretty general question.

You need to call a remote function on a different page via ajax?

jscheuer1
05-30-2006, 08:43 AM
What's a class file?

Do you mean a .js file?

If so, as long as the file is available to the page, you may use its functions. You can also use the DOM to make the file available. There are scripts here on DD (and probably elsewhere) that (among other things) 'load' .js files on a page.

See:

http://www.dynamicdrive.com/dynamicindex17/indexb.html

These scripts do this by creating a script tag element, assigning the .js file as its src attribute, and attaching it to the head element of the page. Although these actions are used with scrips that also use AJAX to load content, this part (loading a script) is purely a DOM operation.

Twey
05-30-2006, 11:41 AM
What's a class file?A class file is a file containing a Java class.

melvinoyh: Is it a server-side or a client-side class? Should it be used in an applet, or a servlet?

jscheuer1
05-30-2006, 04:47 PM
A class file is a file containing a Java class.

melvinoyh: Is it a server-side or a client-side class? Should it be used in an applet, or a servlet?

Geez, I knew that. I got so used to folks saying java and meaning javascript, I thought this was the case here. After all, this is the javascript forum.

Twey
05-30-2006, 05:26 PM
Unfortunately, we haven't a Java forum.

jscheuer1
05-30-2006, 06:04 PM
Anyways, the OP may still be able to write a tag for the class using the DOM. In fact, with IE these days, any sort of object/embed is best done that way or with innerHTML or document.write (for this, all of these are best run from an external .js file) to avoid the 'click to activate this control' 'security feature' that MS recently added - not their finest hour.

Twey
05-30-2006, 06:23 PM
Anyways, the OP may still be able to write a tag for the class using the DOM.Not if it's a server-side JSP page, to which I believe s/he is referring. There is no other reason to use AJAX, or to want to simply call a function and do nothing with the result, of which I can think.
not their finest hour.Not their lowest, either :)

jscheuer1
05-30-2006, 06:26 PM
Not if it's a server-side JSP page, to which I believe s/he is referring. There is no other reason to use AJAX, or to want to simply call a function and do nothing with the result, of which I can think.Not their lowest, either :)

Well, let's not get too excited until we find out what the reason(s) is/are and what type the class is.

melvinoyh
05-31-2006, 12:49 AM
its a java class file that contain java function.
yes, its a server side JSP page that have AJAX use POST method to post data to server and call the function in the java class file to perform some processing and return back the processed info back to jsp page...
how to do so?

Twey
05-31-2006, 12:57 AM
It sounds to me as if you desperately want to return data to the page from which it was sent. This probably isn't what you mean, so I'm just going to give you a generic XMLHttpRequest POST example.
function getHttpRequest() {
if(typeof ActiveXObject == "undefined" && typeof XMLHttpRequest == "undefined") return null;
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
}
@else xmlhttp = null;
@end @*/

if (xmlhttp == null && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = null;
}
}
return xmlhttp;
}

var xht = getHttpRequest();
xht.open("POST", "page.jsp", false);
xht.send("var1=value1");
xht.send("var2=value2");
// ...
var serverSays = xht.responseText;

melvinoyh
05-31-2006, 01:01 AM
this reply is what i get from SitePoint forum

to call a function of the java of the server-side from java scriopt. In this case you can use
1) XMLHTTP object to invoke the JSP/ACTION/SERVLET of the server trhough client side.
2) pass the value to that request.
3) get the response and dispaly it on the page.

any idea on that?

melvinoyh
05-31-2006, 01:12 AM
this is the one that i wrote. im not sure whether correct or not.


//perform this function first
function calc(x) {
var cur_pect = eval("document.breakdown.b_progPect"+x).value;
var cur_qty = eval("document.breakdown.b_progQty"+x).value;
var cur_lump = eval("document.breakdown.b_progLump"+x).value;
var la_qty = eval("document.breakdown.b_laQty"+x).value;
var la_lump = eval("document.breakdown.b_laLump"+x).value;
var take_qty = <%=takeQty%>;
var take_lump = <%=takeLump%>;
var str = "cPect="+encodeURI(cur_pect)+"&cQty="+encodeURI(cur_qty)+"&cLump="+encodeURI(cur_lump)+"&lQty="+encodeURI(la_qty)+"&lLump="+encodeURI(la_lump)+"&tQty="+encodeURI(take_qty)+"&tLump="+encodeURI(take_lump);

makePOSTRequest('getcalc.jsp', str);
}


var http_request = false;
//perform this function second
function makePOSTRequest(url, str) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
//standard, cannot edit
http_request.onreadystatechange = alertContents;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(str);
}

//perform this function thirdly
function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
//alert(http_request.responseText);
result = http_request.responseText;
document.getElementById('myspan').innerHTML = result;
} else {
alert('There was a problem with the request.');
}
}
}

melvinoyh
05-31-2006, 01:34 AM
hi Twey, i wan to ask u about

xht.open("POST", "page.jsp", false);
xht.send("var1=value1");
xht.send("var2=value2");


when the var1 and var2 send to page.jsp, then at page.jsp how to get the values of this 2 variables, izzit using
<%
double pPect = request.getParameter("var1");
double pQty = request.getParameter("var2");
%>

plz correct me if im wrong. thx a lot

Twey
05-31-2006, 01:44 AM
That's right. The code I gave you will do everything you want, except print the output to the page. I did, however, put it in a variable, so you shouldn't have too hard a time of it. :)

melvinoyh
05-31-2006, 01:53 AM
thx Twey.
how about this code....do u see any syntax error or logic error?


//perform this function first
function calc(x) {
var cur_pect = eval("document.breakdown.b_progPect"+x).value;
var cur_qty = eval("document.breakdown.b_progQty"+x).value;
var cur_lump = eval("document.breakdown.b_progLump"+x).value;
var la_qty = eval("document.breakdown.b_laQty"+x).value;
var la_lump = eval("document.breakdown.b_laLump"+x).value;
var take_qty = <%=takeQty%>;
var take_lump = <%=takeLump%>;
var str = "cPect="+encodeURI(cur_pect)+"&cQty="+encodeURI(cur_qty)+"&cLump="+encodeURI(cur_lump)+"&lQty="+encodeURI(la_qty)+"&lLump="+encodeURI(la_lump)+"&tQty="+encodeURI(take_qty)+"&tLump="+encodeURI(take_lump);

makePOSTRequest('getcalc.jsp', str);
}


var http_request = false;
//perform this function second
function makePOSTRequest(url, str) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/xml');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
//standard, cannot edit
http_request.onreadystatechange = alertContents;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(str);
}

//perform this function thirdly
function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
//alert(http_request.responseText);
result = http_request.responseText;
document.getElementById('myspan').innerHTML = result;
} else {
alert('There was a problem with the request.');
}
}
}

Twey
05-31-2006, 02:24 AM
There's no need to create a new object each time:

function getHttpRequest() {
if(typeof ActiveXObject == "undefined" && typeof XMLHttpRequest == "undefined") return null;
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
}
@else xmlhttp = null;
@end @*/

if (xmlhttp == null && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = null;
}
}
return xmlhttp;
}

var http_request = getHttpRequest(),
busy = false;

function makePOSTRequest(url, str) {
if(!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
if(busy) return false;
busy = true;
http_request.onreadystatechange = alertContents;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(str);
}

function alertContents() {
if (http_request.readyState == 4 && Math.floor(http_request.status / 100) == 2) {
document.getElementById('myspan').innerHTML = http_request.responseText;
busy = false;
}
}And the calc() function is just an example of how not to code Javascript:
function calc(x) {
var cur_pect = document.forms['breakdown'].elements["b_progPect"+x].value,
cur_qty = document.forms['breakdown'].elements["b_progQty"+x].value,
cur_lump = document.forms['breakdown'].elements["b_progLump"+x].value,
la_qty = document.forms['breakdown'].elements["b_laQty"+x].value,
la_lump = document.forms['breakdown'].elements["b_laLump"+x].value,
take_qty = <%=takeQty%>,
take_lump = <%=takeLump%>;
var str = "cPect="+encodeURI(cur_pect)+"&cQty="+encodeURI(cur_qty)+"&cLump="+encodeURI(cur_lump)+"&lQty="+encodeURI(la_qty)+"&lLump="+encodeURI(la_lump)+"&tQty="+encodeURI(take_qty)+"&tLump="+encodeURI(take_lump);

makePOSTRequest('getcalc.jsp', str);
}

melvinoyh
05-31-2006, 02:59 AM
thx for help....thx a lot..really big help.
i got 1 page of short code wan u to check for me, that is 'getcalc.jsp' page.
later i post it. :)

melvinoyh
05-31-2006, 03:33 AM
code for getcalc.jsp


<%@page import = "java.sql.*,java.util.*,java.text.SimpleDateFormat,java.text.*"%>
<jsp:useBean id = "db" class="bgoc.dbConn.Conn" scope="request" />
<jsp:useBean id = "clm" class="common.claim" scope="request" />
<%
double pPect = Double.parseDouble(request.getParameter("cPect"));
double pQty = Double.parseDouble(request.getParameter("cQty"));
double pLump = Double.parseDouble(request.getParameter("cLump"));
double tQty = Double.parseDouble(request.getParameter("tQty"));
double tLump = Double.parseDouble(request.getParameter("tLump"));
double lQty = Double.parseDouble(request.getParameter("lQty"));
double lLump = Double.parseDouble(request.getParameter("lLump"));
double cQty = 0;

cQty = clm.calcQty(pPect,pQty,pLump,tQty,tLump,lQty,lLump);
out.println(cQty);
%>

izzit correct or not?

Twey
05-31-2006, 12:37 PM
All pretty much fine. There's no need to import java.text.SimpleDateFormat if you're about to import java.text.*, though :)

melvinoyh
06-01-2006, 01:04 AM
thx a lot Twey

melvinoyh
06-15-2006, 01:00 AM
function getHttpRequest() {
if(typeof ActiveXObject == "undefined" && typeof XMLHttpRequest == "undefined") return null;
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
}
@else xmlhttp = null;
@end @*/

if (xmlhttp == null && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = null;
}
}
return xmlhttp;
}

var http_request = getHttpRequest(),
busy = false;

function makePOSTRequest(url, str) {
if(!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
if(busy) return false;
busy = true;
http_request.onreadystatechange = alertContents;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(str);
}

function alertContents() {
if (http_request.readyState == 4 && Math.floor(http_request.status / 100) == 2) {
document.getElementById('myspan').innerHTML = http_request.responseText;
busy = false;
}
}

hi anyone, may i know what is the meaning of busy = false; in the above statement?

jscheuer1
06-15-2006, 04:15 AM
hi anyone, may i know what is the meaning of busy = false; in the above statement?

Busy is an arbitrarily chosen variable name. Well chosen, in this case, because when the code sets it to true, presumably the process being worked with is occupied with a task and when set to false, available.

I can't vouch for how accurately this is carried out by the code but, that is its obvious intent. More specifically, busy=false simply means that the variable 'busy' has a boolean value of false, as opposed to true. Boolean variables can only be one or the other when properly coded.

melvinoyh
06-15-2006, 06:33 AM
i see, understand.