Solved

Run JSP-code only when page is loaded

Posted on 2006-11-14
7
876 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
caughtSpeeding java challenge 16 141
web application structure 18 135
Why my table column Id is not passed to java object? 4 46
iterator/ListIterator approach 17 68
After hours on line I found a solution which pointed to the inherited Active Directory permissions . You have to give/allow permissions to the "Exchange trusted subsystem" for the user in the Active Directory...
How many times a day do you open, acknowledge, or close an IT incident? What’s your process? Do you have a process depending on the incident, systems involved, and other factors? New Relic Alerts gives you options for how you interact with notifica…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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