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

JSP and JavaScript

Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
Hi guys,
I am working on a JSP page that queries a database and returns data to a screen. The page has search, next and previous buttons that allows user to move thru the record set. The direction I am going in now is that I have one page that receives parameters from the form like string to search by. That string is sent to a class that queries the database and returns data. That data is fed to a page where the form is and the whole screen is rebuilt again with new data. Is it possible for JavaScript and JSP to work together in such a way that when user presses search, the value in search field is send to JavaScript function which in turn sends it to a Java Class that queries the database. Once data is returned, instead of rebuilding the screen, I populate field in the page thru JavaScript like: document.form.field1.value = <%=data%>, or will JavaScript execute this script before data is returned from database?
thanks a lot,
Alex


All right brain, you don't like me and I don't like you, but let's just do this one thing so I can get back to killing you with beer.<br /> <br />- Homer Simpson
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Is it possible for JavaScript and JSP to work together in such a way that when user presses search, the value in search field is send to JavaScript function which in turn sends it to a Java Class that queries the database.

No. The only way to access server-side functions it to post a request back to the server. Javascript has no access to server-side objects; all it can do is trigger the new request by submitting a form or changing the document location.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451

No. The only way to access server-side functions it to post a request back to the server. Javascript has no access to server-side objects; all it can do is trigger the new request by submitting a form or changing the document location.

Actually, there may be a way but it is not real pretty. It would require an Applet (possibly not visible) that can handle the server requests and communicate with the JavaScript objects on the page. Barring something like that, though Bear is right, once the client (browser) has the response it is just static html with only the normal request/response mechanism to communicate with the server. Something we do occasionally is dynamically generate JavaScript with a JSP taglib (or normal JSP elements) so the JavaScript elements can be vastly different from response to response for the same JSP page.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
OTOH you can have the JSP generate Javascript on the fly based on the data it gets in requests sent by Javascript.
Javascripts can also launch requests that dynamically change parts of the page by modifying the innerHTML property of divs on the page.
Maybe not real pretty and cumbersome to code but it is possible.
What is not possible is to have Javascript interact with serverside code without making HTTP requests.


42
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
There is a way to get this functionality using Javascript on the client side, though it only works if the record set is not too big.
On the server side, get the entire record set. Display the first record on the page, and store all of the records in hidden html variables.
Then, set the buttons' 'onclick' attributes to call Javascript functions. These functions will move the appropriate data into visible part of the page. The 'previous' and 'next' functions should be pretty straightforward to write; the 'search' function will be a little more complex, but doable. These functions can be part of the static portion of the JSP page, avoiding the nightmare of having the JSP code dynamically generate the Javascript source.
There are also intermediate solutions. For example, you could use Javascript for the 'previous' and 'next' buttons, but reload the page on a search (using a simple HTML form). If the record set is too big, you could load only part of it, and have the 'previous' and 'next' functions reload the page when they reached the limit of the portion that was in the current set of hidden variables.
Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
If I have to generate JavaScript on-the-fly then I am at square one since I can't reuse my screens.
To store results in array and page thru them is not a solution since if I am at record 5 and someone access the same page and modifies record 6, when I advance to record 6 and read it from an array I read premodified data.
Sam Shaaban
Greenhorn

Joined: Jun 19, 2004
Posts: 1
Don't know if this will help, but some good work has been done on client / server communication using JavaScript on the client with some clever DHTML here:

http://www.ashleyit.com/rs/main.htm

... and a Java server-side modification is posted here:

http://forperfect.com/seo/remoteScripting.php

Hope that helps -

Sam
[ June 19, 2004: Message edited by: Sam Shaaban ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP and JavaScript