Solved

Run JSP-code only when page is loaded

Posted on 2006-11-14
7
875 Views
Last Modified: 2008-01-09
Hi!

I have a basic question. How can I run some JSP-code only when a page is loaded?

Say that I dynamically create a select-box inside a form like this:
<form>
    <select>
    <%
      page.connect();
      ResultSet rs=page.getInfo();
      while (rs.next()) {
          String s=rs.getString("info");        
    %>
    <option value="<%= info %>"><%= info %>
    <% }
      page.disconnect();
    %>
    </select>
    <input type="submit" value="Vis">
</form>

This code should only be run once when the page is loaded, i.e. on "onload" in JavaScript-terms.

How can I do that?

Regards,
Jesper
0
Comment
Question by:jespersahner
7 Comments
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 39 total points
ID: 17940960
You can not. jsp code runs on the server & hence will finish executing even before your page is served on the client..
0
 
LVL 12

Assisted Solution

by:jessegivy
jessegivy earned 37 total points
ID: 17941242
The above esteemed expert is correct.  The JSP code you're showing will output text to the html file and as you can see, it's mingled in with your select element.  Perhaps you could explain what you want to accomplish by waiting until the page is loaded to retrieve the data?  You could include the code after the html tag and then fill the select element by ID using javascript, but it's fairly involved and wouldn't do it without good reason.

HTML code is executed sequentially so if you went with the javascript method you'll have an empty select element added to your page then fill it with options.  Again though, Why do you want to wait, what you have looks acceptable to me?

Cheers,

Jesse
0
 

Author Comment

by:jespersahner
ID: 17941325
I think you misunderstand me or I misunderstand you :-)

The idea is, that the select-box should be created only once/initialized) and not re-created with every click on the page. I am looking for the same functionality as with JavaScript using <body onload="some function()">, calling the "some function" only when the page is loaded.

Regards,
Jesper
0
 
LVL 10

Assisted Solution

by:jaggernat
jaggernat earned 37 total points
ID: 17943923
Is your result set returning data in xml format?
 In that case , you have to remove the scriplet from jsp (which is a bad practise anyway)  and put the java code at the back, return the result(xml) from the java class and when the jsp page loads , onLoad call a javascript function which will load the xml data and traverse through the xml using xpath and populate the drop-down field.



0
 
LVL 29

Assisted Solution

by:bloodredsun
bloodredsun earned 37 total points
ID: 17955462
you could try using a static code block which will ensure that it is run once only or running it in a declaration <%! declaration(s) %>
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Read about why it is more lucrative for an IT company to participate in government projects.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 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