Solved

Can I use javabean in a javascript function ?

Posted on 2003-12-04
9
354 Views
Last Modified: 2011-04-14
Hi,

  I want to access javabean from javascript function, I don't know if it's possible. please give a sample code if it's possible.


Thxs
0
Comment
Question by:richardluo
  • 5
  • 4
9 Comments
 
LVL 15

Accepted Solution

by:
jimmack earned 50 total points
ID: 9879174
From : http://www.jguru.com/faq/view.jsp?EID=49595

JavaScript is code nested between SCRIPT tags within an html page. This code is interpreted and executed by the browser on the client PC. There is no way to directly access a JavaBean property within a JSP through JavaScript. One indirect way could be for the JSP to generate JavaScript that contains variables or constants containing values representing JavaBean properties that the JavaScript code could interpret. JavaScript code would, however, not be able to modify these properties.
0
 

Author Comment

by:richardluo
ID: 9879582
Thanks jimmack for the comments. One more question, how to pass a javascript variable to a jsp function. Here are a part of my code:

function test(){
.....
var catId = document.form1.List1.options[document.form1.List1.selectedIndex].value;
 <% CategoryBean cb = Utilites.getCategoryWithID(application,catId);%>
.....
}

I need function getCategoryWithID to know variable catId which is javascript variable now. Compiler generate an error for catId.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9881220
Sorry to say that I don't think you can do that.  The problem is that the test() function will execute in the clients browser (locally), but the JSP you're including has already been compiled and executed by the time the page is loaded into the browser.

If you need to call the getCategoryWithID() method on a button press (for example), then the onclick needs to access the server again (so that getCategoryWithID() can get the results on the server and prepare the new page).

Unfortunately it's the same problem whichever way round you do it, JavaScript execute on the client and JSP/Servlets execute on the server.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:richardluo
ID: 9883743
Thanks Jim for your help !
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9885797
;-)
0
 

Author Comment

by:richardluo
ID: 9886243
Hi Jim,

  One more basic question, I have the following javascript code:

function test()
{
          <% int index=-1;%>
          for (var i=0; i< 10;i++)
          {
             <% index=index + 1;%>
             alert("<%= index%>");
                  }
}

When I run this function, index shown in alert window is alway "0", it suppose to be: 0,1,2,3..., something wrong with this code ?

Thanks in advance.

Richard
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9886285
Yes.  The problem is with the way you've mixed the JSP and JavaScript.  When you load the page into your browser, have a look at the source.

You'll probably find it isn't what you were hoping for.  The JSP is compiled and executed before the HTML (including the JavaScript) is sent to the browser, so you'll probably see:

function test()
{
    for (var i = 0; i < 10; i++)
    {
        alert("0");
    }
}

That's because the JSP lines were translated (they don't get included in the HTML), so index was set to -1, then incremented and this value was put into the alert string.
0
 

Author Comment

by:richardluo
ID: 9886299
Thanks a lot Jim ! You have a nice weekend !

Richard
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9886311
You too :-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question