Solved

Run JSP-code only when page is loaded

Posted on 2006-11-14
7
871 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
A Short Story about the Best File Recovery Software – Acronis True Image 2017
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now