• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP and JavaScript

 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64716
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic