开发者

Calling a Java function with a JavaScript variable inside a JavaScript function

I need help. I need to call a Java function "getLocCountByWhId()" in a Java class; this Java function is being called within a JavaScript in a for-loop. I need to pass in a JavaScript variable as a parameter into this Java function "getLocCountByWhId()". I have been struggling for a week and reading numerous website to get some guidelines but I have not been able to resolve the problem. Thank you in advance for your help. The code is listing below:

<script language="JavaScript">
<!--
function onCreatePO()
{
    <%long jspAllocId = alloc.getId();%>;
    var recItemId = ""; // Local variable for item id.
    var recWhId = ""; // Local variable for warehouse id.                           

    for (var i=0, j=document.what_if_summary.elements.length; i<j; i++)
    {      
       var recStr = document.what_if_summary.elements[i].value; 
       var splitStr = new Array(); 
       splitStr = recStr.split('^');
       recItemId = splitStr[1]; // Get the field value for Item_ID.
       recWhId = splitStr[2]; // Get the field value for Warehouse_ID.

// Get a database connection with global 'conn' object and retrieve store count.
       <%AfsGetVDSCountByWarehouseBean.setConnection(conn);%>; 

// The below assignment from JavaScript variable to JSP variable do not work 
// because of a second JavaScript ta开发者_如何转开发g. How can I get around this ?

//<% String jspItemId = "<script>document.writeln(recItemId)</script>"; %>
//<% String jspWhId = "<script>document.writeln(recWhId)</script>"; %>

   currentStoreCount = <%= AfsGetVDSCountByWarehouseBean.getLocCountByWhId(jspAllocId, jspItemId, jspWhId)%>;
   }
}

//-->
</script>

// I get this example of assigning Javascript to JSP variable but I got double tag problem.
<script>
var v="Roseindia";
</script>
<% String st="<script>document.writeln(v)</script>";
out.println("value="+st); %>


What you are trying to do is not possible, and (sorry!) reflects a basic misunderstanding as to how server-side code such as JSP (or ASP, PHP, etc) works.

A JSP page is basically a Java servlet running on the web server that dynamically generates a web page and returns it to the browser. The resulting web page may contain HTML, CSS, JavaScript, etc., but to the Java code all of that is just text. The Java/JSP code cannot understand or interact with the JavaScript because the JavaScript doesn't run on the web server, it is just more text to be sent as part of the response back to the browser.

When the browser gets the response it will display the web page and execute any JavaScript.

Further reading (I wouldn't put too much faith in what you read at the RoseIndia site):

http://www.ibm.com/developerworks/java/tutorials/j-introjsp/

http://www.oracle.com/technetwork/articles/javase/servlets-jsp-140445.html

http://java.sun.com/developer/onlineTraining/JSPIntro/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜