?
Solved

Can I use javabean in a javascript function ?

Posted on 2003-12-04
9
Medium Priority
?
357 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Suggested Courses

770 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