JSP and JavaScript
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ]
| Hey! Wanna see my flashlight? It looks like this tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |













