Solved

Can I use javabean in a javascript function ?

Posted on 2003-12-04
9
351 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
 

Author Comment

by:richardluo
ID: 9883743
Thanks Jim for your help !
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now