Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Can I use javabean in a javascript function ?

Posted on 2003-12-04
9
Medium Priority
?
360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 15

Accepted Solution

by:
jimmack earned 200 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

610 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