aspose file tools*
The moose likes JSP and the fly likes How to link JSp With Javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How to link JSp With Javascript" Watch "How to link JSp With Javascript" New topic
Author

How to link JSp With Javascript

Ustaad Desraj
Ranch Hand

Joined: Jul 07, 2009
Posts: 34
I was making a Webpage.
I have to use onClick() function in <SELECT><OPTION> statement.
Purpose is when i click on any value in LIST, it must show data extracting from DB to screen

when i put onClick() in <Option> statement, then it works on Firefox but not on IE7 and IE8
When i put onChange() in <select> statement, then it woks on both, and i want this, and problem is there. kindly read further

to solve either i can use servlets, but i do not want to generate dynamic page all the time.

so i included Javascript code on, onChange():

<Select MULTIPLE SIZE=25 onchange="if(this.options[this.selectedIndex].value=='2') document.write('Hello');">

this works
but i want to link it to JSP pages, without using AJAX, because i am not able to apply AJAX on IE7 and IE8 too.

i want when i click particular option in list it will Display content acc. to it, and ya LIST is too coming from Database.

If you understand the problem , kindly solve!!!

USTAAD DESRAJ,
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61233
    
  66

No, I don't really understand the problem. But Ajax can work fine on IE, you don't need to avoid it. And using document.write() is not going to do what you think (it replaces the page).


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ustaad Desraj
Ranch Hand

Joined: Jul 07, 2009
Posts: 34
Well sir i think you are saying right i should use AJAX there.

is there any other method to use JSP with JAVASCRIPT?

see this is present code which i will elogate acc. to need




This was past code on which i was working, which was not working on IE7 and IE8




i feel now you will understand the problem better
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61233
    
  66

Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information.

Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61233
    
  66

JSP just runs on the server to create the HTML page to send to the browser. It really has nothing to do with JavaScript.

And scriptlets in your JSP? In 2010?
Ustaad Desraj
Ranch Hand

Joined: Jul 07, 2009
Posts: 34
lol , sir i m just making logic. i will make more generalized form later.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61233
    
  66

You should use good habits all the time. Not just later. Scriptlets have been discredited for over 8 years now. You should be using the JSTL and EL in your pages.

In any case, remember that the script in your pages is just template text to the JSP engine, which knows nothing about JavaScript (or HTML for that matter). All that matters is what ends up getting sent to the browser.
Ustaad Desraj
Ranch Hand

Joined: Jul 07, 2009
Posts: 34
so you want me to shift on JSTL n EL rather then using this eight year(old is gold) way? ok i will shift, but not now

sir solve this first.
i do not want to use AJAX

i want to run onClick() func directly, in select statement(calling my business func and bean classes) rather then to pass process from javascript or JAX.

pleaseeeeeeeeeeeeeeeeeeeeeeeeeeeee

ALSO ANSWER WHY onCLICK() is not working in IE7 or 8
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61233
    
  66

If I understand what you are asking, you want to execute server-side Java code as the result of a click. Is that correct?

If so, sorry, you either need to submit the page back to the server or use Ajax.

Please read this article to understand how JSP works and why you can't just mix Java code into JavaScript.
Ustaad Desraj
Ranch Hand

Joined: Jul 07, 2009
Posts: 34
If I understand what you are asking, you want to execute server-side Java code as the result of a click. Is that correct?


Yes, i want the same

Data is displayed in LISTBOX.
When someone click elements of list, request shall go to server and page acc. to that list item display on client side.


If so, sorry, you either need to submit the page back to the server or use Ajax.


1) AJAX is not working, as i told onCLICK() func in <option> tag is not working, even it is not executing JSP code ON INTERNET EXPLORER.

i am not getting how to deal with onCLICK in <select> tag, and link it with JSP or AJAX to get results from server side

2) I do not want to use servlets and generate dynamic pages for that
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61233
    
  66

I do not believe that you can capture clicks in option elements. You'll probably need to capture change events for the select element.
Ustaad Desraj
Ranch Hand

Joined: Jul 07, 2009
Posts: 34
I am able to do that but it works in Mozilla Firefox only not in Internet Explorer.

So you want me to use

onChange(), and call element by ID and send it to server through AJAX and get output and enjoy? m i rite?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to link JSp With Javascript