Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Run JSP-code only when page is loaded

Posted on 2006-11-14
7
Medium Priority
?
880 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 156 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 148 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 148 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 148 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Files go missing when using DFS (Distributed File System) Replication and how to recover them and fix it.
The online market is growing at an unprecedented rate and retail eCommerce sales are expected to reach $4 trillion by 2020. Yet, the profit is not just there for the taking because you have to set yourself apart from the competition.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

670 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